OLD | NEW |
(Empty) | |
| 1 <p> |
| 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.tabs.onCreated</code> event |
| 7 to be notified whenever there's a new tab: |
| 8 </p> |
| 9 |
| 10 <pre> |
| 11 chrome.tabs.onCreated.<b>addListener(function(</b>tab<b>) {</b> |
| 12 appendToLog('tabs.onCreated --' |
| 13 + ' window: ' + tab.windowId |
| 14 + ' tab: ' + tab.id |
| 15 + ' index: ' + tab.index |
| 16 + ' url: ' + tab.url); |
| 17 <b>});</b> |
| 18 </pre> |
| 19 |
| 20 <p> |
| 21 As the example shows, |
| 22 you register for notification using <code>addListener()</code>. |
| 23 The argument to <code>addListener()</code> |
| 24 is always a function that you define to handle the event, |
| 25 but the parameters to the function depend on |
| 26 which event you're handling. |
| 27 Checking the documentation for |
| 28 <a href="tabs.html#event-onCreated"><code>chrome.tabs.onCreated</code></a>, |
| 29 you can see that the function has a single parameter: |
| 30 a <a href="tabs.html#type-tabs.Tab">Tab</a> object |
| 31 that has details about the newly created tab. |
| 32 </p> |
| 33 |
| 34 |
| 35 <div class="doc-family extensions"> |
| 36 <h2 id="declarative">Declarative Event Handlers</h2> |
| 37 |
| 38 <p> |
| 39 The declarative event handlers provide a means to define rules consisting of |
| 40 declarative conditions and actions. Conditions are evaluated in the browser |
| 41 rather than the JavaScript engine which reduces roundtrip latencies and allows |
| 42 for very high efficiency. |
| 43 </p> |
| 44 |
| 45 <p>Declarative event handlers are used for example in the <a |
| 46 href="declarativeWebRequest.html">Declarative Web Request API</a> and possibly |
| 47 further extension APIs in the future. This page describes the underlying |
| 48 concepts of all declarative event handlers. |
| 49 </p> |
| 50 |
| 51 <h3 id="rules">Rules</h3> |
| 52 |
| 53 <p>The simplest possible rule consists of one or more conditions and one or more |
| 54 actions:</p> |
| 55 <pre> |
| 56 var rule = { |
| 57 conditions: [ /* my conditions */ ], |
| 58 actions: [ /* my actions */ ] |
| 59 }; |
| 60 </pre> |
| 61 |
| 62 <p>If any of the conditions is fulfilled, all actions are executed.</p> |
| 63 |
| 64 <p>In addition to conditions and actions you may give each rule an identifier, |
| 65 which simplifies unregistering previously registered rules, and a priority to |
| 66 define precedences among rules. Priorities are only considered if rules conflict |
| 67 each other or need to be executed in a specific order.</p> |
| 68 |
| 69 <pre> |
| 70 var rule = { |
| 71 id: "my rule", // optional, will be generated if not set. |
| 72 priority: 100, // optional, defaults to 100. |
| 73 conditions: [ /* my conditions */ ], |
| 74 actions: [ /* my actions */ ] |
| 75 }; |
| 76 </pre> |
| 77 |
| 78 <h3 id="eventobjects">Event objects</h3> |
| 79 |
| 80 <p> |
| 81 <a href="events.html">Event objects</a> may support rules. These event objects |
| 82 don't call a callback function when events happer but test whether any |
| 83 registered rule has at least one fulfilled condition and execute the actions |
| 84 associated with this rule. Event objects supporting the declarative API have |
| 85 three relevant methods: <a href="#method-addRules"><code>addRules()</code></a>, |
| 86 <a href="#method-removeRules"><code>removeRules()</code></a>, and |
| 87 <a href="#method-getRules"><code>getRules()</code></a>. |
| 88 </p> |
| 89 |
| 90 <h3 id="addingrules">Adding rules</h3> |
| 91 |
| 92 <p> |
| 93 To add rules call the <code>addRules()</code> function of the event object. It |
| 94 takes an array of rule instances as its first parameter and a callback function |
| 95 that is called on completion. |
| 96 </p> |
| 97 |
| 98 <pre> |
| 99 var rule_list = [rule1, rule2, ...]; |
| 100 function addRules(rule_list, function callback(details) {...}); |
| 101 </pre> |
| 102 |
| 103 <p> |
| 104 If the rules were inserted successfully, the <code>details</code> parameter |
| 105 contains an array of inserted rules appearing in the same order as in the passed |
| 106 <code>rule_list</code> where the optional parameters <code>id</code> and |
| 107 <code>priority</code> were filled with the generated values. If any rule is |
| 108 invalid, e.g., because it contained an invalid condition or action, none of the |
| 109 rules are added and the <a |
| 110 href="extension.html#property-lastError">lastError</a> variable is set when |
| 111 the callback function is called. Each rule in <code>rule_list</code> must |
| 112 contain a unique identifier that is not currently used by another rule or an |
| 113 empty identifier. |
| 114 </p> |
| 115 |
| 116 <h3 id="removingrules">Removing rules</h3> |
| 117 |
| 118 <p> |
| 119 To remove rules call the <code>removeRules()</code> function. It accepts an |
| 120 optional array of rule identifiers as its first parameter and a callback |
| 121 function as its second parameter. |
| 122 </p> |
| 123 |
| 124 <pre> |
| 125 var rule_ids = ["id1", "id2", ...]; |
| 126 function removeRules(rule_ids, function callback() {...}); |
| 127 </pre> |
| 128 |
| 129 <p> |
| 130 If <code>rule_ids</code> is an array of identifiers, all rules having |
| 131 identifiers listed in the array are removed. If <code>rule_ids</code> lists an |
| 132 identifier, that is unknown, this identifier is silently ignored. If |
| 133 <code>rule_ids</code> is <code>undefined</code>, all registered rules of this |
| 134 extension are removed. The <code>callback()</code> function is called when the |
| 135 rules were removed. |
| 136 </p> |
| 137 |
| 138 <h3 id="retrievingrules">Retrieving rules</h3> |
| 139 |
| 140 <p> |
| 141 To retrieve a list of currently registered rules, call the |
| 142 <code>getRules()</code> function. It accepts an optional array of rule |
| 143 identifiers with the same semantics as <code>removeRules</code> and a callback |
| 144 function. |
| 145 <p> |
| 146 |
| 147 <pre> |
| 148 var rule_ids = ["id1", "id2", ...]; |
| 149 function getRules(rule_ids, function callback(details) {...}); |
| 150 </pre> |
| 151 |
| 152 <p> |
| 153 The <code>details</code> parameter passed to the <code>calback()</code> function |
| 154 refers to an array of rules including filled optional parameters. |
| 155 </p> |
| 156 </div> |
OLD | NEW |