Some common event types are defined in chrome.events, 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 Chrome.events, events do not have any properties, they contain only the following common methods:
Method name |
return value Type |
Comments |
AddListener (function () {...}) |
No |
Registers the listener for the event and gives the event handler function |
RemoveListener (function () {...}) |
No |
To delete an event listener |
Haslistener (function () {...}) |
Boolean |
Determine if a listener is registered on the event |
Haslisteners () |
Boolean |
Determine if there is a listener registered on the event, no callback function |
Addrules (Array of Rule rules, function (array of Rule rules) {...}) |
No |
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 () {...}) |
No |
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) {...}) |
No |
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 ' google.com '},// filter Condition 15 'google.com.au'} ]}// 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 Chrome.events.Rule object are as follows:
Property name |
Type |
must-choose/ options available |
Comments |
Id |
String |
Options available |
Identifier |
Tags |
Array of string |
Options available |
Tags for rules, multiple rules can be grouped by tags |
Conditions |
Array of any |
Must-Choose |
Conditions that trigger the processing behavior |
Actions |
Array of any |
Must-Choose |
The processing behavior that is triggered when either condition matches |
Priority |
Integer |
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 chrome.events.UrlFilter 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: Chrome.events APIs available in the extension