OLD | NEW |
1 <h1>Message Passing</h1> | 1 <h1>Message Passing</h1> |
2 | 2 |
3 | 3 |
4 <p> | 4 <p> |
5 Since content scripts run in the context of a web page and not the extension, | 5 Since content scripts run in the context of a web page and not the extension, |
6 they often need some way of communicating with the rest of the extension. For | 6 they often need some way of communicating with the rest of the extension. For |
7 example, an RSS reader extension might use content scripts to detect the | 7 example, an RSS reader extension might use content scripts to detect the |
8 presence of an RSS feed on a page, then notify the background page in order to | 8 presence of an RSS feed on a page, then notify the background page in order to |
9 display a page action icon for that page. | 9 display a page action icon for that page. |
10 | 10 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 port.postMessage({question: "Madame who?"}); | 147 port.postMessage({question: "Madame who?"}); |
148 else if (msg.answer == "Madame... Bovary") | 148 else if (msg.answer == "Madame... Bovary") |
149 port.postMessage({question: "I don't get it."}); | 149 port.postMessage({question: "I don't get it."}); |
150 }); | 150 }); |
151 }); | 151 }); |
152 </pre> | 152 </pre> |
153 | 153 |
154 <p> | 154 <p> |
155 You may want to find out when a connection is closed, for example if you are | 155 You may want to find out when a connection is closed, for example if you are |
156 maintaining separate state for each open port. For this you can listen to the | 156 maintaining separate state for each open port. For this you can listen to the |
157 $ref:runtime.Port | 157 $ref:runtime.Port.onDisconnect |
158 event. This event is fired either when the other side of the channel manually | 158 event. This event is fired either when the other side of the channel manually |
159 calls | 159 calls |
160 $ref:runtime.Port, or when the page | 160 $ref:runtime.Port.disconnect, or when the page |
161 containing the port is unloaded (for example if the tab is navigated). | 161 containing the port is unloaded (for example if the tab is navigated). |
162 onDisconnect is guaranteed to be fired only once for any given port. | 162 onDisconnect is guaranteed to be fired only once for any given port. |
163 | 163 |
164 | 164 |
165 <h2 id="external">Cross-extension messaging</h2> | 165 <h2 id="external">Cross-extension messaging</h2> |
166 <p> | 166 <p> |
167 In addition to sending messages between different components in your | 167 In addition to sending messages between different components in your |
168 extension, you can use the messaging API to communicate with other extensions. | 168 extension, you can use the messaging API to communicate with other extensions. |
169 This lets you expose a public API that other extensions can take advantage of. | 169 This lets you expose a public API that other extensions can take advantage of. |
170 | 170 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/messaging/">examples/api/messaging</a> | 265 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/messaging/">examples/api/messaging</a> |
266 directory. | 266 directory. |
267 Also see the | 267 Also see the |
268 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/howto/contentscript_xhr">contentscript_xhr</a> example, | 268 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/howto/contentscript_xhr">contentscript_xhr</a> example, |
269 in which a content script and its parent extension exchange messages, | 269 in which a content script and its parent extension exchange messages, |
270 so that the parent extension can perform | 270 so that the parent extension can perform |
271 cross-site requests on behalf of the content script. | 271 cross-site requests on behalf of the content script. |
272 For more examples and for help in viewing the source code, see | 272 For more examples and for help in viewing the source code, see |
273 <a href="samples.html">Samples</a>. | 273 <a href="samples.html">Samples</a>. |
274 </p> | 274 </p> |
OLD | NEW |