Chrome Extensions Development Series 17: APIs available in the extension

Source: Internet
Author: User

Some common event types are defined in, which can be used by the Chrome browser Extender to emit corresponding event objects.

For the event of concern, the listener is first registered through AddListener () on the corresponding event, as in the following example:

1 Chrome.alarms.onAlarm.addListener (function (alarm) {2         appendtolog ('  Alarms.onalarm--'3                      ' + alarm.name4                      " + alarm.scheduledtime); 5       });

AddListener () always takes a parameter, which is the event handler function. Once an event occurs, the listener invokes the event handler function.

Event handlers can also have parameters, and the number and type of specific parameters are related to the events being monitored.

In, events do not have any properties, they contain only the following common methods:

Method name

return value



AddListener (function () {...})


Registers the listener for the event and gives the event handler function

RemoveListener (function () {...})


To delete an event listener

Haslistener (function () {...})


Determine if a listener is registered on the event

Haslisteners ()


Determine if there is a listener registered on the event, no callback function

Addrules (Array of Rule rules,

function (array of Rule rules) {...})


Registering rules for events

If there is an invalid rule in the array of the first parameter, no rule is registered

Removerules (array of string ruleidentifiers,

function () {...})


Deletes all rules registered on the event.

If there is a first argument (which can not), only the registered, in-scope rules are deleted

GetRules (array of string ruleidentifiers,

function (array of Rule rules) {...})


Returns all rules registered on the event.

If there is a first argument (which can not), only the registered, in-scope rules are returned

There is a scenario where the event is handled only when certain conditions are met, that is, the event that occurs is not processed. For this scenario, you can either attach a filter condition to the event handler or take a new event rule mechanism. discussed separately below.

1) Filter Events

Filtering events is the event handler function that is invoked only when it meets the filtering criteria for the event being monitored. There is a "or" relationship between multiple filtering conditions. Examples are as follows:

1 Chrome.webNavigation.onCommitted.addListener (2      function (e) {3         / / ... 4       ' '},// filter Condition 15                 ''} ]}// filter Condition 26 );

2) declarative event handlers (event rules)

A declarative event handler is a process rule defined for an event. The event object that registers the rule, when the event object occurs, no longer calls the event handler, but instead detects whether the condition of the registered rule has a match, and if the match executes the rule's behavior.

Typically, rules must contain declarative conditions and handling behavior. Examples are as follows:

1 varRule = {2Id:"My rule",//The identifier is optional and is not generated automatically3Priority -,//priority is optional, default is4Conditions: [/*My conditions*/ ],5Actions: [/*My actions*/ ]6};

A rule can have more than one condition, and the relationship between the conditions is "or", meaning that any one condition satisfies the matching rule.

A rule can have more than one processing behavior, and the behavior is a "with" relationship, that is, all the behaviors are executed as a whole, when the conditions match.

The properties of the object are as follows:

Property name


must-choose/ options available




Options available



Array of string

Options available

Tags for rules, multiple rules can be grouped by tags


Array of any


Conditions that trigger the processing behavior


Array of any


The processing behavior that is triggered when either condition matches



Options available

Priority of the rule, default 100

A rule is valid throughout the browser's session, so the rule is usually installed in the Chrome.runtime.onInstalled event handler that installs the Chrome extension. The Chrome.runtime.onInstalled event is also triggered when the Chrome extension is upgraded, so be aware that in this event handler, you first determine if there are any rules that have already been installed, and if so, first remove the rules that have already been installed, and then install the rules.

The checking of declarative conditions is done directly by the browser, without the need for a JavaScript engine, thus reducing latency.

Declarative event handlers are often used for Chrome.declarativewebrequest APIs (currently not stable) and Chrome.declarativecontent APIs.

Best practices for Event rules:

    • Bulk register rules in the form of arrays, rather than registering rules individually;
    • When the condition of a rule is a URL, the URL is divided into substrings to match, not an expression of the entire URL, and (for URL filtering, there is a dedicated available, a little bit here.) )
    • If several conditions correspond to the same processing behavior, the conditions are put into one rule

Chrome Extensions Development Series 17: APIs available in the extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.