| OLD | NEW |
| 1 <div id="pageData-title" class="pageData">Message Passing</div> | 1 <div id="pageData-title" 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 else | 77 else |
| 78 sendResponse({}); // snub them. | 78 sendResponse({}); // snub them. |
| 79 }); | 79 }); |
| 80 </pre> | 80 </pre> |
| 81 | 81 |
| 82 | 82 |
| 83 <h2 id="connect">Long-lived connections</h2> | 83 <h2 id="connect">Long-lived connections</h2> |
| 84 <p> | 84 <p> |
| 85 Sometimes it's useful to have a conversation that lasts longer than a single | 85 Sometimes it's useful to have a conversation that lasts longer than a single |
| 86 request and response. In this case, you can open a long-lived channel from | 86 request and response. In this case, you can open a long-lived channel from |
| 87 your content script to an extension page, or vice versa, using | 87 your content script to an extension page, or vice versa, using |
| 88 <a href="extension.html#method-connect">chrome.extension.connect()</a> | 88 <a href="extension.html#method-connect">chrome.extension.connect()</a> |
| 89 or | 89 or |
| 90 <a href="tabs.html#method-connect">chrome.tabs.connect()</a> respectively. The | 90 <a href="tabs.html#method-connect">chrome.tabs.connect()</a> respectively. The |
| 91 channel can optionally have a name, allowing you to distinguish between | 91 channel can optionally have a name, allowing you to distinguish between |
| 92 different types of connections. | 92 different types of connections. |
| 93 | 93 |
| 94 <p> | 94 <p> |
| 95 One use case might be an automatic form fill extension. The content script | 95 One use case might be an automatic form fill extension. The content script |
| 96 could open a channel to the extension page for a particular login, and send a | 96 could open a channel to the extension page for a particular login, and send a |
| 97 message to the extension for each input element on the page to request the | 97 message to the extension for each input element on the page to request the |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 port.postMessage({question: "I don't get it."}); | 148 port.postMessage({question: "I don't get it."}); |
| 149 }); | 149 }); |
| 150 }); | 150 }); |
| 151 </pre> | 151 </pre> |
| 152 | 152 |
| 153 <p> | 153 <p> |
| 154 You may want to find out when a connection is closed, for example if you are | 154 You may want to find out when a connection is closed, for example if you are |
| 155 maintaining separate state for each open port. For this you can listen to the | 155 maintaining separate state for each open port. For this you can listen to the |
| 156 <a href="extension.html#type-Port">Port.onDisconnect</a> | 156 <a href="extension.html#type-Port">Port.onDisconnect</a> |
| 157 event. This event is fired either when the other side of the channel manually | 157 event. This event is fired either when the other side of the channel manually |
| 158 calls | 158 calls |
| 159 <a href="extension.html#type-Port">Port.disconnect()</a>, or when the page | 159 <a href="extension.html#type-Port">Port.disconnect()</a>, or when the page |
| 160 containing the port is unloaded (for example if the tab is navigated). | 160 containing the port is unloaded (for example if the tab is navigated). |
| 161 onDisconnect is guaranteed to be fired only once for any given port. | 161 onDisconnect is guaranteed to be fired only once for any given port. |
| 162 | 162 |
| 163 | 163 |
| 164 <h2 id="external">Cross-extension messaging</h2> | 164 <h2 id="external">Cross-extension messaging</h2> |
| 165 <p> | 165 <p> |
| 166 In addition to sending messages between different components in your | 166 In addition to sending messages between different components in your |
| 167 extension, you can use the messaging API to communicate with other extensions. | 167 extension, you can use the messaging API to communicate with other extensions. |
| 168 This lets you expose a public API that other extensions can take advantage of. | 168 This lets you expose a public API that other extensions can take advantage of. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 chrome.extension.sendRequest(laserExtensionId, {getTargetData: true}, | 208 chrome.extension.sendRequest(laserExtensionId, {getTargetData: true}, |
| 209 function(response) { | 209 function(response) { |
| 210 if (targetInRange(response.targetData)) | 210 if (targetInRange(response.targetData)) |
| 211 chrome.extension.sendRequest(laserExtensionId, {activateLasers: true}); | 211 chrome.extension.sendRequest(laserExtensionId, {activateLasers: true}); |
| 212 }); | 212 }); |
| 213 | 213 |
| 214 // Start a long-running conversation: | 214 // Start a long-running conversation: |
| 215 var port = chrome.extension.connectExternal(laserExtensionId); | 215 var port = chrome.extension.connectExternal(laserExtensionId); |
| 216 port.postMessage(...); | 216 port.postMessage(...); |
| 217 </pre> | 217 </pre> |
| OLD | NEW |