Developers › Dev.Opera

RuleOptions.resources

May 16, 2012 · by Opera Software

This functionality is currently in production, and only available to test in an experimental build of Opera desktop. To download the experimental build, see Desktop build with new extension APIs: Screenshot, Resource Loader and URL Filter.

Description:

A bit-masked value indicating the types of resources to apply a given URL Filter rule to. The available resource types are as follows:

RESOURCE_DOCUMENT
Top-level documents.
RESOURCE_FONT
Font resources referenced in CSS properties or SVG elements
RESOURCE_IMAGE
Image resources referenced by <img> elements, the background attribute on various elements, or CSS properties.
RESOURCE_MEDIA
Media resources referenced by <audio>, <video> or <source> elements.
RESOURCE_OBJECT
Generic object resources referenced by <object> elements.
RESOURCE_OBJECT_SUBREQUEST
A request made by a plugin loaded by an HTML <embed> or <object> element.
RESOURCE_OTHER
Any resource not covered by the other resource types.
RESOURCE_REFRESH
An HTML <meta> element with an http-equiv attribute whose value is "refresh" and a content attribute whose value contains a timeout value and optionally a URL.
RESOURCE_SCRIPT
Script resources referenced by <script> elements.
RESOURCE_STYLESHEET
Stylesheet resources referenced either by <link> elements or @import directives within a stylesheet.
RESOURCE_SUBDOCUMENT
Resources referenced by an <frame> or <iframe> element.
RESOURCE_XMLHTTPREQUEST
A request from an XMLHttpRequest object.

This property is passed within a RuleOptions object as a parameter for URL Filter methods.

Syntax:

unsigned long resources

Examples:

Example 1

The following example blocks content from google.com whenever it appears as the source of a frame or iframe, e.g. a Google+ widget.

<!-- 
  The configuration file ('config.xml').
-->
<?xml version='1.0' encoding='utf-8'?>
<widget xmlns="http://www.w3.org/ns/widgets">
    ...
    <feature name="opera:urlfilter"/>
</widget>
//
// The background process (e.g. index.html)
//

// Check that the URL Filter API exists
if (typeof opera.extension.urlfilter != 'undefined') {
  // Assign the URLFilter object to a variable for efficiency
  var filter = opera.extension.urlfilter;
  
  // Set the options for the filter rule
  var ruleOptions = {
      resources: filter.RESOURCE_SUBDOCUMENT
  }
  
  filter.block.add('||google.com/*', ruleOptions);
}

Example 2

This next example uses a custom function to make it easier to combine multiple resource types. In this case, it blocks all scripts, stylesheets and images from the google.com domain.

<!-- 
  The configuration file ('config.xml').
-->
<?xml version='1.0' encoding='utf-8'?>
<widget xmlns="http://www.w3.org/ns/widgets">
    ...
    <feature name="opera:urlfilter"/>
</widget>
//
// The background process (e.g. index.html)
//

// Check that the URL Filter API exists
if (typeof opera.extension.urlfilter != 'undefined') {
  // Function for mapping filter resources
  function types(types) {
    var urlfilter = opera.extension.urlfilter;
    var contentType = 0;
    var map = {
      "other": urlfilter.RESOURCE_OTHER, // 1
      "script": urlfilter.RESOURCE_SCRIPT, // 2
      "image": urlfilter.RESOURCE_IMAGE, // 4
      "stylesheet": urlfilter.RESOURCE_STYLESHEET, // 8
      "object": urlfilter.RESOURCE_OBJECT, // 16
      "subdocument": urlfilter.RESOURCE_SUBDOCUMENT, // 32
      "document": urlfilter.RESOURCE_DOCUMENT, // 64
      "refresh": urlfilter.RESOURCE_REFRESH, // 128
      "xmlhttprequest": urlfilter.RESOURCE_XMLHTTPREQUEST, // 2048
      "objectsubrequest": urlfilter.RESOURCE_OBJECT_SUBREQUEST, // 4096
      "media": urlfilter.RESOURCE_MEDIA, // 16384
      "font": urlfilter.RESOURCE_FONT // 32768
    };

    for (var i = 0, len = arguments.length; i < len; i++) {
      contentType |= map[arguments[i]];
    }
    
    return contentType;
  }
  
  // Set the options for the filter rule
  ruleOptions = {resources: types("script", "stylesheet", "image")};
  /*
   * This is equivalent to
   * ruleOptions = {
   *   resources: opera.extension.urlfilter.RESOURCE_SCRIPT |
   *     opera.extension.urlfilter.RESOURCE_STYLESHEET |
   *     opera.extension.urlfilter.RESOURCE_IMAGE
   * };
   */
  
  opera.extension.urlfilter.block.add('||google.com/*', ruleOptions);
}

Note the use of || which is a hostname mark. This indicates that the subsequent characters should begin matching from any host name in the URL. For example, ||example.com* matches http://example.com/, https://www.example.com/, or similar. However, it will not match http://badexample.com/.

About this article

Published inDev.Opera
DateMay 16, 2012
AuthorOpera Software
Tagsextensions API, URL filter
OriginalInternet Archive