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