Developers › Dev.Opera

opera.extension.urlfilter.allow.add()

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:

Adds a rule, with accompanying options, to the virtual list of allowed URLs (whitelist).

Note that allowed rules have priority over blocked rules by default.

Parameters:

  • rule: The rule (e.g. domain) to allow.
  • options: Options to specify conditions for the given rule.

Syntax:

void add (DOMString rule, RuleOptions options)

Examples:

Example 1

The following example blocks any content from facebook.com, but only when accessed from the cnn.com domain, e.g. a "Like" or "Recommend" button.

<!-- 
  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') {
  // Set the options for the filter rule
  var ruleOptions = {
    excludeDomains: ['cnn.com']
  }
  
  // Assign the URLFilter object to a variable for efficiency
  var filter = opera.extension.urlfilter;
  
  filter.allow.add('||facebook.com/*', ruleOptions);
  filter.block.add('||facebook.com/*');
}

Example 2

This second example shows how we can convert whitelisting in Adblock Plus syntax to the Opera URL Filter syntax. The whitelisting rule will allow scripts from the ||ads.cnn.com/js.ng/*&cnn_intl_subsection=download URL and is taken from this list: https://easylist-downloads.adblockplus.org/easylist.txt

<!-- 
  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;
  
  filter.block.add('||ads.*');
  
  // The following is the same as this Adblock syntax for whitelisting:
  // @@||ads.cnn.com/js.ng/*&cnn_intl_subsection=download$script
  filter.allow.add("||ads.cnn.com/js.ng/*&cnn_intl_subsection=download", {resources: filter.RESOURCE_SCRIPT});
}

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