URL filter API

By Opera Software

opera.extension.urlfilter.block.add()
Adds a rule to the virtual list of blocked URLs.
opera.extension.urlfilter.block.remove()
Removes a rule from the virtual list of blocked URLs.
opera.extension.urlfilter.allow.add()
Adds a rule to the virtual list of allowed URLs.
RuleOptions.excludeDomains
Domains on which to not apply a filter rule.
RuleOptions.includeDomains
Domains on which to apply a filter rule.
RuleOptions.resources
Resource types to apply a filter rule to.
RuleOptions.thirdParty
Specifies whether a filter rule should apply to third-party domains.
URL Filter syntax
Special characters that can be used when filtering.

Overview

The URL Filter API for Opera extensions defines a DOM interface that allows extensions to add temporary rules to Opera's native content blocker. Rules added through this API are associated with an extension and apply as long as the extension is enabled. Once an extension is disabled or the browser is shut down, the temporary rules are discarded.

To enable the URL filter, the opera:urlfilter feature needs to be added as a feature element to the extension's config.xml file.

An in-depth tutorial is available at Dev.Opera: Site blocking with Opera's URL Filter API

Example

Block example.com/images, example.com/css and any subdirectories, whatever the protocol. Note that www.example.com and other sub-domains are not affected.

<!-- 
  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') {
  // Create a list of rules to block
  var rules = ['*://example.com/images/*', '*://example.com/css/*'];

  // Assign the URLFilter object to a variable for efficiency
  var filter = opera.extension.urlfilter;

  // Loop through the array of rules and add each one to the "block" list
  for (var i = 0, len = rules.length; i < len; i++) {
    filter.block.add(rules[i]);
  }
}

This article is licensed under a Creative Commons Attribution 3.0 Unported license.

Comments

  • photo

    justvovus

    Thursday, May 10, 2012

    Hi
    Do you plan to implement opera.extension.urlfilter.block.get() or opera.extension.urlfilter.block.list() method?

    Now extension should to keep the list of blocked urls in WebSQL DB, because there is no way to unlock URL if you don't know what exactly locked. One extension can add url to urlfilter and this url doesn't appears anywhere, not in urlfilter.ini, not in some other list.

    Am I correct?
  • photo

    Dither

    Monday, June 18, 2012

    As I've understood it extensions should manage URL-filters database by themselves now and load it to Opera on each extension start. So one should duplicate all operations with opera.extension.urlfilter to that database and thus it will always be equal to Opera's internal block-list (it's not "connected" to urlfilter.ini and won't show in "Blocked content"). There is, regrettably, no way now to query what filters are applied on a current page from that internal database. Such query can be done with JS by matching page's urls (fixed) against our copy of URL-filters db but that is so very slow and unreliable that doesn't need to be mentioned. There was a word of events in the experimental API that will announce blocked URLs and they'll probably solve this problem but static queries (to list active rules for certain tab, to test URL against internal db and to find rule(s) that contain some substing) should be also a good thing.
  • photo

    Martin Kadlec

    Monday, July 30, 2012

    The documentation is missing some info about events or when script is blocked or not blocked because of white-list. The events are called: oncontentblocked and oncontentunblocked
  • photo

    Brandon

    Saturday, October 6, 2012

    Nice, so does this mean we could see improved adblock extensions closer to the quality of those found in chrome/firefox? I had been sticking with urlfilter.ini just because it seemed to work better than all the opera adblock extensions I tried.
  • photo

    Janghou

    Tuesday, November 27, 2012

    opera.extension.urlfilter.allow.remove() is also missing in the documentation.
    It does the same as block.remove but then for the Allow RuleList.
    You need it if you want to reset a RuleList entry
No new comments accepted.