2 An
<code>Event
</code> is an object
3 that allows you to be notified
4 when something interesting happens.
5 Here's an example of using the
6 <code>chrome.alarms.onAlarm
</code> event
7 to be notified whenever an alarm has elapsed:
11 chrome.alarms.onAlarm.
<b>addListener(function(
</b>alarm
<b>) {
</b>
12 appendToLog('alarms.onAlarm --'
13 + ' name: ' + alarm.name
14 + ' scheduledTime: ' + alarm.scheduledTime);
20 you register for notification using
<code>addListener()
</code>.
21 The argument to
<code>addListener()
</code>
22 is always a function that you define to handle the event,
23 but the parameters to the function depend on
24 which event you're handling.
25 Checking the documentation for
26 $(ref:alarms.onAlarm),
27 you can see that the function has a single parameter:
28 an $(ref:alarms.Alarm) object
29 that has details about the elapsed alarm.
32 Example APIs using Events:
41 Most
<a href=
"api_index">chrome APIs
</a> do.
44 <div class=
"doc-family extensions">
45 <h2 id=
"declarative">Declarative Event Handlers
</h2>
48 The declarative event handlers provide a means to define rules consisting of
49 declarative conditions and actions. Conditions are evaluated in the browser
50 rather than the JavaScript engine which reduces roundtrip latencies and allows
51 for very high efficiency.
54 <p>Declarative event handlers are used for example in the
<a
55 href=
"declarativeWebRequest">Declarative Web Request API
</a> and
<a
56 href=
"declarativeContent">Declarative Content API
</a>. This page describes
57 the underlying concepts of all declarative event handlers.
60 <h3 id=
"rules">Rules
</h3>
62 <p>The simplest possible rule consists of one or more conditions and one or more
66 conditions: [ /* my conditions */ ],
67 actions: [ /* my actions */ ]
71 <p>If any of the conditions is fulfilled, all actions are executed.
</p>
73 <p>In addition to conditions and actions you may give each rule an identifier,
74 which simplifies unregistering previously registered rules, and a priority to
75 define precedences among rules. Priorities are only considered if rules conflict
76 each other or need to be executed in a specific order. Actions are executed in
77 descending order of the priority of their rules.
</p>
81 id:
"my rule", // optional, will be generated if not set.
82 priority:
100, // optional, defaults to
100.
83 conditions: [ /* my conditions */ ],
84 actions: [ /* my actions */ ]
88 <h3 id=
"eventobjects">Event objects
</h3>
91 <a href=
"events">Event objects
</a> may support rules. These event objects
92 don't call a callback function when events happen but test whether any
93 registered rule has at least one fulfilled condition and execute the actions
94 associated with this rule. Event objects supporting the declarative API have
95 three relevant methods: $(ref:events.Event.addRules),
96 $(ref:events.Event.removeRules), and
97 $(ref:events.Event.getRules).
100 <h3 id=
"addingrules">Adding rules
</h3>
103 To add rules call the
<code>addRules()
</code> function of the event object. It
104 takes an array of rule instances as its first parameter and a callback function
105 that is called on completion.
109 var rule_list = [rule1, rule2, ...];
110 function addRules(rule_list, function callback(details) {...});
114 If the rules were inserted successfully, the
<code>details
</code> parameter
115 contains an array of inserted rules appearing in the same order as in the passed
116 <code>rule_list
</code> where the optional parameters
<code>id
</code> and
117 <code>priority
</code> were filled with the generated values. If any rule is
118 invalid, e.g., because it contained an invalid condition or action, none of the
119 rules are added and the
120 $(ref:runtime.lastError) variable is set when
121 the callback function is called. Each rule in
<code>rule_list
</code> must
122 contain a unique identifier that is not currently used by another rule or an
127 <strong>Note:
</strong> Rules are persistent across browsing sessions. Therefore,
128 you should install rules during extension installation time using the
129 <code>$(ref:runtime.onInstalled)
</code>
130 event. Note that this event is also triggered when an extension is updated.
131 Therefore, you should first clear previously installed rules and then register
135 <h3 id=
"removingrules">Removing rules
</h3>
138 To remove rules call the
<code>removeRules()
</code> function. It accepts an
139 optional array of rule identifiers as its first parameter and a callback
140 function as its second parameter.
144 var rule_ids = [
"id1",
"id2", ...];
145 function removeRules(rule_ids, function callback() {...});
149 If
<code>rule_ids
</code> is an array of identifiers, all rules having
150 identifiers listed in the array are removed. If
<code>rule_ids
</code> lists an
151 identifier, that is unknown, this identifier is silently ignored. If
152 <code>rule_ids
</code> is
<code>undefined
</code>, all registered rules of this
153 extension are removed. The
<code>callback()
</code> function is called when the
157 <h3 id=
"retrievingrules">Retrieving rules
</h3>
160 To retrieve a list of currently registered rules, call the
161 <code>getRules()
</code> function. It accepts an optional array of rule
162 identifiers with the same semantics as
<code>removeRules
</code> and a callback
167 var rule_ids = [
"id1",
"id2", ...];
168 function getRules(rule_ids, function callback(details) {...});
172 The
<code>details
</code> parameter passed to the
<code>callback()
</code> function
173 refers to an array of rules including filled optional parameters.
176 <h3 id=
"performance">Performance
</h3>
179 To achieve maximum performance, you should keep the following guidelines in
182 <li><p>Register and unregister rules in bulk. After each
183 registration or unregistration, Chrome needs to update internal data
184 structures. This update is an expensive operation.
</p>
189 chrome.declarativeWebRequest.onRequest.addRules([rule1]);
190 chrome.declarativeWebRequest.onRequest.addRules([rule2]);
</pre>
191 <p>prefer to write
</p>
195 chrome.declarativeWebRequest.onRequest.addRules([rule1, rule2]);
</pre>
196 <li>Prefer substring matching over matching using regular expressions in a
197 $(ref:events.UrlFilter). Substring based matching is extremely fast.
200 var match = new chrome.declarativeWebRequest.RequestMatcher({
201 url: {urlMatches:
"example.com/[^?]*foo" } });
</pre>
202 <p>prefer to write
</p>
204 var match = new chrome.declarativeWebRequest.RequestMatcher({
205 url: {hostSuffix:
"example.com", pathContains:
"foo"} });
</pre>
206 <li>If you have many rules that all share the same actions, you may merge
207 the rules into one because rules trigger their actions as soon as a single
208 condition is fulfilled. This speeds up the matching and reduces memory
209 consumption for duplicate action sets.
212 var condition1 = new chrome.declarativeWebRequest.RequestMatcher({
213 url: { hostSuffix: 'example.com' } });
214 var condition2 = new chrome.declarativeWebRequest.RequestMatcher({
215 url: { hostSuffix: 'foobar.com' } });
216 var rule1 = { conditions: [condition1],
217 actions: [new chrome.declarativeWebRequest.CancelRequest()]};
218 var rule2 = { conditions: [condition2],
219 actions: [new chrome.declarativeWebRequest.CancelRequest()]};
220 chrome.declarativeWebRequest.onRequest.addRules([rule1, rule2]);
</pre>
221 <p>prefer to write
</p>
223 var rule = { conditions: [condition1, condition2],
224 actions: [new chrome.declarativeWebRequest.CancelRequest()]};
225 chrome.declarativeWebRequest.onRequest.addRules([rule]);
</pre>
232 <div class=
"doc-family extensions">
233 <h2 id=
"filtered">Filtered events
</h2>
235 <p>Filtered events are a mechanism that allows listeners to specify a subset of
236 events that they are interested in. A listener that makes use of a filter won't
237 be invoked for events that don't pass the filter, which makes the listening
238 code more declarative and efficient - an
<a href=
"event_pages">event
239 page
</a> page need not be woken up to handle events it doesn't care
242 <p>Filtered events are intended to allow a transition from manual filtering
246 chrome.webNavigation.onCommitted.addListener(function(e) {
247 if (hasHostSuffix(e.url, 'google.com') ||
248 hasHostSuffix(e.url, 'google.com.au')) {
257 chrome.webNavigation.onCommitted.addListener(function(e) {
259 }, {url: [{hostSuffix: 'google.com'},
260 {hostSuffix: 'google.com.au'}]});
263 <p>Events support specific filters that are meaningful to that event. The list
264 of filters that an event supports will be listed in the documentation for that
265 event in the
"filters" section.
</p>
267 <p>When matching URLs (as in the example above), event filters support the same
268 URL matching capabilities as expressible with a
269 $(ref:events.UrlFilter), except for scheme and port