Developers › Dev.Opera

URL Filter syntax

May 22, 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:

Enables special characters to be used for more flexible filtering. Note that all pattern matching is case sensitive.

Syntax:

Wildcard

* matches any character.

Separator wildcard

|| matches the beginning of any hostname.

Hostname marker

^ matches a single character from the following list:

! " # $ & ' ( ) * + , / : ; < = > ? @ [ \ ] ^ ` { | } ~

In other words, || matches any character in the ASCII subset that is not a letter, a digit, or one of the following:

_ - . %

For example, ||example.com^ will match match http://example.com:8080/, http://example.com/, https://subdomain.example.com/, etc. but not http://example.com.evil.com because ^ doesn't match . (period).

Example:

This 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});
}

About this article

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