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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) { | 56 chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) { |
57 console.log(response.farewell); | 57 console.log(response.farewell); |
58 }); | 58 }); |
59 }); | 59 }); |
60 </pre> | 60 </pre> |
61 | 61 |
62 <p> | 62 <p> |
63 On the receiving end, you need to set up an | 63 On the receiving end, you need to set up an |
64 <a href="extension.html#event-onRequest">chrome.extension.onRequest</a> | 64 <a href="extension.html#event-onRequest">chrome.extension.onRequest</a> |
65 event listener to handle the message. This looks the same from a content | 65 event listener to handle the message. This looks the same from a content |
66 script or extension page. The request will remain open until you call | 66 script or extension page. |
67 sendResponse, so it is good practice to call sendResponse with an empty | |
68 object to allow the request to be cleaned up. | |
69 <pre> | 67 <pre> |
70 chrome.extension.onRequest.addListener( | 68 chrome.extension.onRequest.addListener( |
71 function(request, sender, sendResponse) { | 69 function(request, sender, sendResponse) { |
72 console.log(sender.tab ? | 70 console.log(sender.tab ? |
73 "from a content script:" + sender.tab.url : | 71 "from a content script:" + sender.tab.url : |
74 "from the extension"); | 72 "from the extension"); |
75 if (request.greeting == "hello") | 73 if (request.greeting == "hello") |
76 sendResponse({farewell: "goodbye"}); | 74 sendResponse({farewell: "goodbye"}); |
77 else | |
78 sendResponse({}); // snub them. | |
79 }); | 75 }); |
80 </pre> | 76 </pre> |
81 | 77 |
82 <p class="note"> | 78 <p class="note"> |
83 <b>Note:</b> If multiple pages are listening for onRequest events, only the | 79 <b>Note:</b> If multiple pages are listening for onRequest events, only the |
84 first to call sendResponse() for a particular event will succeed in sending the | 80 first to call sendResponse() for a particular event will succeed in sending the |
85 response. All other responses to that event will be ignored. | 81 response. All other responses to that event will be ignored. |
86 </p> | 82 </p> |
87 | 83 |
88 | 84 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 case, except you use the | 174 case, except you use the |
179 <a href="extension.html#event-onRequestExternal">chrome.extension.onRequestExter
nal</a> | 175 <a href="extension.html#event-onRequestExternal">chrome.extension.onRequestExter
nal</a> |
180 or | 176 or |
181 <a href="extension.html#event-onConnectExternal">chrome.extension.onConnectExter
nal</a> | 177 <a href="extension.html#event-onConnectExternal">chrome.extension.onConnectExter
nal</a> |
182 methods. Here's an example of each: | 178 methods. Here's an example of each: |
183 <pre> | 179 <pre> |
184 // For simple requests: | 180 // For simple requests: |
185 chrome.extension.onRequestExternal.addListener( | 181 chrome.extension.onRequestExternal.addListener( |
186 function(request, sender, sendResponse) { | 182 function(request, sender, sendResponse) { |
187 if (sender.id == blacklistedExtension) | 183 if (sender.id == blacklistedExtension) |
188 sendResponse({}); // don't allow this extension access | 184 return; // don't allow this extension access |
189 else if (request.getTargetData) | 185 else if (request.getTargetData) |
190 sendResponse({targetData: targetData}); | 186 sendResponse({targetData: targetData}); |
191 else if (request.activateLasers) { | 187 else if (request.activateLasers) { |
192 var success = activateLasers(); | 188 var success = activateLasers(); |
193 sendResponse({activateLasers: success}); | 189 sendResponse({activateLasers: success}); |
194 } | 190 } |
195 }); | 191 }); |
196 | 192 |
197 // For long-lived connections: | 193 // For long-lived connections: |
198 chrome.extension.onConnectExternal.addListener(function(port) { | 194 chrome.extension.onConnectExternal.addListener(function(port) { |
(...skipping 71 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> | 266 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/messaging/">examples/api/messaging</a> |
271 directory. | 267 directory. |
272 Also see the | 268 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, | 269 <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, | 270 in which a content script and its parent extension exchange messages, |
275 so that the parent extension can perform | 271 so that the parent extension can perform |
276 cross-site requests on behalf of the content script. | 272 cross-site requests on behalf of the content script. |
277 For more examples and for help in viewing the source code, see | 273 For more examples and for help in viewing the source code, see |
278 <a href="samples.html">Samples</a>. | 274 <a href="samples.html">Samples</a>. |
279 </p> | 275 </p> |
OLD | NEW |