OLD | NEW |
1 <p> | 1 <p> |
2 An <code>Event</code> is an object | 2 An <code>Event</code> is an object |
3 that allows you to be notified | 3 that allows you to be notified |
4 when something interesting happens. | 4 when something interesting happens. |
5 Here's an example of using the | 5 Here's an example of using the |
6 <code>chrome.tabs.onCreated</code> event | 6 <code>chrome.tabs.onCreated</code> event |
7 to be notified whenever there's a new tab: | 7 to be notified whenever there's a new tab: |
8 </p> | 8 </p> |
9 | 9 |
10 <pre> | 10 <pre> |
(...skipping 28 matching lines...) Expand all Loading... |
39 You can invoke the following methods on any <code>Event</code> object: | 39 You can invoke the following methods on any <code>Event</code> object: |
40 </p> | 40 </p> |
41 | 41 |
42 <pre> | 42 <pre> |
43 void addListener(function callback(...)) | 43 void addListener(function callback(...)) |
44 void removeListener(function callback(...)) | 44 void removeListener(function callback(...)) |
45 bool hasListener(function callback(...)) | 45 bool hasListener(function callback(...)) |
46 </pre> | 46 </pre> |
47 | 47 |
48 <!-- [PENDING: explain removeListener and hasListener] --> | 48 <!-- [PENDING: explain removeListener and hasListener] --> |
| 49 |
| 50 |
| 51 <h2 id="declarative">Declarative Event Handlers</h2> |
| 52 |
| 53 <p> |
| 54 The declarative event handlers provide a means to define rules consisting of |
| 55 declarative conditions and actions. Conditions are evaluated in the browser |
| 56 rather than the JavaScript engine which reduces roundtrip latencies and allows |
| 57 for very high efficiency. |
| 58 </p> |
| 59 |
| 60 <p>Declarative event handlers are used for example in the <a |
| 61 href="declarativeWebRequest.html">Declarative Web Request API</a> and possibly |
| 62 further extension APIs in the future. This page describes the underlying |
| 63 concepts of all declarative event handlers. |
| 64 </p> |
| 65 |
| 66 <h3 id="rules">Rules</h3> |
| 67 |
| 68 <p>The simplest possible rule consists of one or more conditions and one or more |
| 69 actions:</p> |
| 70 <pre> |
| 71 var rule = { |
| 72 conditions: [ /* my conditions */ ], |
| 73 actions: [ /* my actions */ ] |
| 74 }; |
| 75 </pre> |
| 76 |
| 77 <p>If any of the conditions is fulfilled, all actions are executed.</p> |
| 78 |
| 79 <p>In addition to conditions and actions you may give each rule an identifier, |
| 80 which simplifies unregistering previously registered rules, and a priority to |
| 81 define precedences among rules. Priorities are only considered if rules conflict |
| 82 each other or need to be executed in a specific order.</p> |
| 83 |
| 84 <pre> |
| 85 var rule = { |
| 86 id: "my rule", // optional, will be generated if not set. |
| 87 priority: 100, // optional, defaults to 100. |
| 88 conditions: [ /* my conditions */ ], |
| 89 actions: [ /* my actions */ ] |
| 90 }; |
| 91 </pre> |
| 92 |
| 93 <h3 id="eventobjects">Event objects</h3> |
| 94 |
| 95 <p> |
| 96 <a href="events.html">Event objects</a> may support rules. These event objects |
| 97 don't call a callback function when events happer but test whether any |
| 98 registered rule has at least one fulfilled condition and execute the actions |
| 99 associated with this rule. Event objects supporting the declarative API have |
| 100 three relevant methods: <a href="#method-addRules"><code>addRules()</code></a>, |
| 101 <a href="#method-removeRules"><code>removeRules()</code></a>, and |
| 102 <a href="#method-getRules"><code>getRules()</code></a>. |
| 103 </p> |
| 104 |
| 105 <h3 id="addingrules">Adding rules</h3> |
| 106 |
| 107 <p> |
| 108 To add rules call the <code>addRules()</code> function of the event object. It |
| 109 takes an array of rule instances as its first parameter and a callback function |
| 110 that is called on completion. |
| 111 </p> |
| 112 |
| 113 <pre> |
| 114 var rule_list = [rule1, rule2, ...]; |
| 115 function addRules(rule_list, function callback(details) {...}); |
| 116 </pre> |
| 117 |
| 118 <p> |
| 119 If the rules were inserted successfully, the <code>details</code> parameter |
| 120 contains an array of inserted rules appearing in the same order as in the passed |
| 121 <code>rule_list</code> where the optional parameters <code>id</code> and |
| 122 <code>priority</code> were filled with the generated values. If any rule is |
| 123 invalid, e.g., because it contained an invalid condition or action, none of the |
| 124 rules are added and the <a |
| 125 href="extension.html#property-lastError">lastError</a> variable is set when |
| 126 the callback function is called. Each rule in <code>rule_list</code> must |
| 127 contain a unique identifier that is not currently used by another rule or an |
| 128 empty identifier. |
| 129 </p> |
| 130 |
| 131 <h3 id="removingrules">Removing rules</h3> |
| 132 |
| 133 <p> |
| 134 To remove rules call the <code>removeRules()</code> function. It accepts an |
| 135 optional array of rule identifiers as its first parameter and a callback |
| 136 function as its second parameter. |
| 137 </p> |
| 138 |
| 139 <pre> |
| 140 var rule_ids = ["id1", "id2", ...]; |
| 141 function removeRules(rule_ids, function callback() {...}); |
| 142 </pre> |
| 143 |
| 144 <p> |
| 145 If <code>rule_ids</code> is an array of identifiers, all rules having |
| 146 identifiers listed in the array are removed. If <code>rule_ids</code> lists an |
| 147 identifier, that is unknown, this identifier is silently ignored. If |
| 148 <code>rule_ids</code> is <code>undefined</code>, all registered rules of this |
| 149 extension are removed. The <code>callback()</code> function is called when the |
| 150 rules were removed. |
| 151 </p> |
| 152 |
| 153 <h3 id="retrievingrules">Retrieving rules</h3> |
| 154 |
| 155 <p> |
| 156 To retrieve a list of currently registered rules, call the |
| 157 <code>getRules()</code> function. It accepts an optional array of rule |
| 158 identifiers with the same semantics as <code>removeRules</code> and a callback |
| 159 function. |
| 160 <p> |
| 161 |
| 162 <pre> |
| 163 var rule_ids = ["id1", "id2", ...]; |
| 164 function getRules(rule_ids, function callback(details) {...}); |
| 165 </pre> |
| 166 |
| 167 <p> |
| 168 The <code>details</code> parameter passed to the <code>calback()</code> function |
| 169 refers to an array of rules including filled optional parameters. |
| 170 </p> |
OLD | NEW |