| OLD | NEW |
| 1 <!-- BEGIN AUTHORED CONTENT --> | 1 |
| 2 |
| 2 <h2 id="notes">Notes</h2> | 3 <h2 id="notes">Notes</h2> |
| 4 |
| 3 <p> | 5 <p> |
| 4 Use the <code>chrome.declarativeWebRequest</code> module to intercept, block, or | 6 Use the <code>chrome.declarativeWebRequest</code> module to intercept, block, or |
| 5 modify requests in-flight. It is significantly faster than the <a | 7 modify requests in-flight. It is significantly faster than the <a |
| 6 href="webRequest.html"><code>chrome.webRequest</code> API</a> because you can | 8 href="webRequest.html"><code>chrome.webRequest</code> API</a> because you can |
| 7 register rules that are evaluated in the browser rather than the | 9 register rules that are evaluated in the browser rather than the |
| 8 JavaScript engine which reduces roundtrip latencies and allows for very high | 10 JavaScript engine which reduces roundtrip latencies and allows for very high |
| 9 efficiency. | 11 efficiency. |
| 10 </p> | 12 </p> |
| 13 |
| 11 <h2 id="manifest">Manifest</h2> | 14 <h2 id="manifest">Manifest</h2> |
| 15 |
| 12 <p> | 16 <p> |
| 13 You must declare the "declarative" and the "declarativeWebRequest" permission in | 17 You must declare the "declarative" and the "declarativeWebRequest" permission in |
| 14 the <a href="manifest.html">extension manifest</a> to use this API, | 18 the <a href="manifest.html">extension manifest</a> to use this API, |
| 15 along with <a href="manifest.html#permissions">host permissions</a> for any | 19 along with <a href="manifest.html#permissions">host permissions</a> for any |
| 16 hosts whose network requests you want to access. | 20 hosts whose network requests you want to access. |
| 17 </p> | 21 </p> |
| 22 |
| 18 <pre>{ | 23 <pre>{ |
| 19 "name": "My extension", | 24 "name": "My extension", |
| 20 ... | 25 ... |
| 21 <b> "permissions": [ | 26 <b> "permissions": [ |
| 22 "declarative", | |
| 23 "declarativeWebRequest", | 27 "declarativeWebRequest", |
| 24 "*://*.google.com" | 28 "*://*.google.com" |
| 25 ]</b>, | 29 ]</b>, |
| 26 ... | 30 ... |
| 27 }</pre> | 31 }</pre> |
| 32 |
| 28 <h2 id="rules">Rules</h2> | 33 <h2 id="rules">Rules</h2> |
| 34 |
| 29 <p> | 35 <p> |
| 30 The Declarative Web Request API follows the concepts of the <a | 36 The Declarative Web Request API follows the concepts of the <a |
| 31 href="events.html#declarative">Declarative API</a>. You can register rules to | 37 href="events.html#declarative">Declarative API</a>. You can register rules to |
| 32 the <code>chrome.declarativeWebRequest.onRequest</code> event object. | 38 the <code>chrome.declarativeWebRequest.onRequest</code> event object. |
| 33 </p> | 39 </p> |
| 40 |
| 34 <p> | 41 <p> |
| 35 The Declarative Web Request API supports a single type of match criteria, the | 42 The Declarative Web Request API supports a single type of match criteria, the |
| 36 <code>RequestMatcher</code>. The <code>RequestMatcher</code> matches network | 43 <code>RequestMatcher</code>. The <code>RequestMatcher</code> matches network |
| 37 requests if and only if all listed criteria are met. The following | 44 requests if and only if all listed criteria are met. The following |
| 38 <code>RequestMatcher</code> would match a network request when the user enters | 45 <code>RequestMatcher</code> would match a network request when the user enters |
| 39 "http://www.example.com" in the URL bar: | 46 "http://www.example.com" in the URL bar: |
| 40 </p> | 47 </p> |
| 48 |
| 41 <pre> | 49 <pre> |
| 42 var matcher = new chrome.declarativeWebRequest.RequestMatcher({ | 50 var matcher = new chrome.declarativeWebRequest.RequestMatcher({ |
| 43 url: { hostSuffix: 'example.com', schemes: ['http'] }, | 51 url: { hostSuffix: 'example.com', schemes: ['http'] }, |
| 44 resourceType: 'main_frame' | 52 resourceType: 'main_frame' |
| 45 }); | 53 }); |
| 46 </pre> | 54 </pre> |
| 55 |
| 47 <p> | 56 <p> |
| 48 Requests to "https://www.example.com" would be rejected by the | 57 Requests to "https://www.example.com" would be rejected by the |
| 49 <code>RequestMatcher</code> due to the scheme. Also all requests for an embedded | 58 <code>RequestMatcher</code> due to the scheme. Also all requests for an embedded |
| 50 iframe would be rejected due to the <code>resourceType</code>. | 59 iframe would be rejected due to the <code>resourceType</code>. |
| 51 </p> | 60 </p> |
| 61 |
| 52 <p class="note"> | 62 <p class="note"> |
| 53 <strong>Note:</strong> All conditions and actions are created via a constructor | 63 <strong>Note:</strong> All conditions and actions are created via a constructor |
| 54 as shown in the example above. | 64 as shown in the example above. |
| 55 <p> | 65 <p> |
| 66 |
| 56 <p> | 67 <p> |
| 57 In order to cancel all requests to "example.com", you can define a rule as | 68 In order to cancel all requests to "example.com", you can define a rule as |
| 58 follows: | 69 follows: |
| 59 </p> | 70 </p> |
| 60 <pre> | 71 <pre> |
| 61 var rule = { | 72 var rule = { |
| 62 conditions: [ | 73 conditions: [ |
| 63 new chrome.declarativeWebRequest.RequestMatcher({ | 74 new chrome.declarativeWebRequest.RequestMatcher({ |
| 64 url: { hostSuffix: 'example.com' } }) | 75 url: { hostSuffix: 'example.com' } }) |
| 65 ], | 76 ], |
| 66 actions: [ | 77 actions: [ |
| 67 new chrome.declarativeWebRequest.CancelRequest() | 78 new chrome.declarativeWebRequest.CancelRequest() |
| 68 ]}; | 79 ]}; |
| 69 </pre> | 80 </pre> |
| 81 |
| 70 <p> | 82 <p> |
| 71 In order to cancel all requests to "example.com" and "foobar.com", you can add a | 83 In order to cancel all requests to "example.com" and "foobar.com", you can add a |
| 72 second condition, as each condition is sufficient to trigger all specified | 84 second condition, as each condition is sufficient to trigger all specified |
| 73 actions: | 85 actions: |
| 74 </p> | 86 </p> |
| 75 <pre> | 87 <pre> |
| 76 var rule2 = { | 88 var rule2 = { |
| 77 conditions: [ | 89 conditions: [ |
| 78 new chrome.declarativeWebRequest.RequestMatcher({ | 90 new chrome.declarativeWebRequest.RequestMatcher({ |
| 79 url: { hostSuffix: 'example.com' } }), | 91 url: { hostSuffix: 'example.com' } }), |
| 80 new chrome.declarativeWebRequest.RequestMatcher({ | 92 new chrome.declarativeWebRequest.RequestMatcher({ |
| 81 url: { hostSuffix: 'foobar.com' } }) | 93 url: { hostSuffix: 'foobar.com' } }) |
| 82 ], | 94 ], |
| 83 actions: [ | 95 actions: [ |
| 84 new chrome.declarativeWebRequest.CancelRequest() | 96 new chrome.declarativeWebRequest.CancelRequest() |
| 85 ]}; | 97 ]}; |
| 86 </pre> | 98 </pre> |
| 99 |
| 87 <p> | 100 <p> |
| 88 Register rules as follows: | 101 Register rules as follows: |
| 89 </p> | 102 </p> |
| 90 <pre> | 103 <pre> |
| 91 chrome.declarativeWebRequest.onRequest.addRules([rule2]); | 104 chrome.declarativeWebRequest.onRequest.addRules([rule2]); |
| 92 </pre> | 105 </pre> |
| 106 |
| 93 <p class="note"> | 107 <p class="note"> |
| 94 <strong>Note:</strong> You should always register or unregister rules in bulk ra
ther than | 108 <strong>Note:</strong> You should always register or unregister rules in bulk ra
ther than |
| 95 individually because each of these operations recreates internal data | 109 individually because each of these operations recreates internal data |
| 96 structures. This re-creation is computationally expensive but facilitates a | 110 structures. This re-creation is computationally expensive but facilitates a |
| 97 very fast URL matching algorithm for hundreds of thousands of URLs. | 111 very fast URL matching algorithm for hundreds of thousands of URLs. |
| 98 </p> | 112 </p> |
| 113 |
| 99 <h2 id="TODO">Todo</h2> | 114 <h2 id="TODO">Todo</h2> |
| 100 <ul> | 115 <ul> |
| 101 <li>Explain precedences, once we can ignore rules based on their priority | 116 <li>Explain precedences, once we can ignore rules based on their priority |
| 102 (e.g. how can I cancel all requests except for a specific whitelist?) | 117 (e.g. how can I cancel all requests except for a specific whitelist?) |
| 103 <li>Explain when conditions can be evaluated, when actions can be executed, | 118 <li>Explain when conditions can be evaluated, when actions can be executed, |
| 104 and when rules can be executed (e.g. you cannot cancel a request when you | 119 and when rules can be executed (e.g. you cannot cancel a request when you |
| 105 have received the response already) | 120 have received the response already) |
| 106 </ul> | 121 </ul> |
| 107 <!-- END AUTHORED CONTENT --> | 122 |
| 123 |
| OLD | NEW |