OLD | NEW |
1 <div id="pageData-name" class="pageData">Message Passing</div> | 1 <div id="pageData-name" class="pageData">Message Passing</div> |
2 <div id="pageData-showTOC" class="pageData">true</div> | 2 <div id="pageData-showTOC" class="pageData">true</div> |
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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 messages: | 115 messages: |
116 <pre> | 116 <pre> |
117 contentscript.js | 117 contentscript.js |
118 ================ | 118 ================ |
119 var port = chrome.extension.connect({name: "knockknock"}); | 119 var port = chrome.extension.connect({name: "knockknock"}); |
120 port.postMessage({joke: "Knock knock"}); | 120 port.postMessage({joke: "Knock knock"}); |
121 port.onMessage.addListener(function(msg) { | 121 port.onMessage.addListener(function(msg) { |
122 if (msg.question == "Who's there?") | 122 if (msg.question == "Who's there?") |
123 port.postMessage({answer: "Madame"}); | 123 port.postMessage({answer: "Madame"}); |
124 else if (msg.question == "Madame who?") | 124 else if (msg.question == "Madame who?") |
125 port.postMessage({answer: "Madame... Bovary"); | 125 port.postMessage({answer: "Madame... Bovary"}); |
126 }); | 126 }); |
127 </pre> | 127 </pre> |
128 | 128 |
129 <p> | 129 <p> |
130 Sending a request from the extension to a content script looks very similar, | 130 Sending a request from the extension to a content script looks very similar, |
131 except that you need to specify which tab to connect to. Simply replace the | 131 except that you need to specify which tab to connect to. Simply replace the |
132 call to connect in the above example with | 132 call to connect in the above example with |
133 <a href="tabs.html#method-connect">chrome.tabs.connect(tabId, {name: | 133 <a href="tabs.html#method-connect">chrome.tabs.connect(tabId, {name: |
134 "knockknock"})</a>. | 134 "knockknock"})</a>. |
135 | 135 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/messaging/">examples/api/messaging</a> | 270 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/messaging/">examples/api/messaging</a> |
271 directory. | 271 directory. |
272 Also see the | 272 Also see the |
273 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/howto/contentscript_xhr">contentscript_xhr</a> example, | 273 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/howto/contentscript_xhr">contentscript_xhr</a> example, |
274 in which a content script and its parent extension exchange messages, | 274 in which a content script and its parent extension exchange messages, |
275 so that the parent extension can perform | 275 so that the parent extension can perform |
276 cross-site requests on behalf of the content script. | 276 cross-site requests on behalf of the content script. |
277 For more examples and for help in viewing the source code, see | 277 For more examples and for help in viewing the source code, see |
278 <a href="samples.html">Samples</a>. | 278 <a href="samples.html">Samples</a>. |
279 </p> | 279 </p> |
OLD | NEW |