| 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 29 matching lines...) Expand all Loading... |
| 40 <pre data-filename="contentscript.js"> | 40 <pre data-filename="contentscript.js"> |
| 41 chrome.runtime.sendMessage({greeting: "hello"}, function(response) { | 41 chrome.runtime.sendMessage({greeting: "hello"}, function(response) { |
| 42 console.log(response.farewell); | 42 console.log(response.farewell); |
| 43 }); | 43 }); |
| 44 </pre> | 44 </pre> |
| 45 | 45 |
| 46 <p> | 46 <p> |
| 47 Sending a request from the extension to a content script looks very similar, | 47 Sending a request from the extension to a content script looks very similar, |
| 48 except that you need to specify which tab to send it to. This example | 48 except that you need to specify which tab to send it to. This example |
| 49 demonstrates sending a message to the content script in the selected tab. | 49 demonstrates sending a message to the content script in the selected tab. |
| 50 <pre data-filename="background.html"> | 50 <pre data-filename="background"> |
| 51 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { | 51 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { |
| 52 chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) { | 52 chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) { |
| 53 console.log(response.farewell); | 53 console.log(response.farewell); |
| 54 }); | 54 }); |
| 55 }); | 55 }); |
| 56 </pre> | 56 </pre> |
| 57 | 57 |
| 58 <p> | 58 <p> |
| 59 On the receiving end, you need to set up an | 59 On the receiving end, you need to set up an |
| 60 $(ref:runtime.onMessage) | 60 $(ref:runtime.onMessage) |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/messaging/">examples/api/messaging</a> | 451 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/messaging/">examples/api/messaging</a> |
| 452 directory. | 452 directory. |
| 453 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/nativeMessaging/">examples/api/nativeMessaging</a> | 453 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/nativeMessaging/">examples/api/nativeMessaging</a> |
| 454 contains an example application that uses native messaging. | 454 contains an example application that uses native messaging. |
| 455 Also see the | 455 Also see the |
| 456 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/howto/contentscript_xhr">contentscript_xhr</a> example, | 456 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/howto/contentscript_xhr">contentscript_xhr</a> example, |
| 457 in which a content script and its parent extension exchange messages, | 457 in which a content script and its parent extension exchange messages, |
| 458 so that the parent extension can perform | 458 so that the parent extension can perform |
| 459 cross-site requests on behalf of the content script. | 459 cross-site requests on behalf of the content script. |
| 460 For more examples and for help in viewing the source code, see | 460 For more examples and for help in viewing the source code, see |
| 461 <a href="samples.html">Samples</a>. | 461 <a href="samples">Samples</a>. |
| 462 </p> | 462 </p> |
| OLD | NEW |