diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-06 19:48:41 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-06 19:48:41 +0000 |
commit | 4acbedb1fe0f43802c64027cf7cbf81441297a95 (patch) | |
tree | 3a0158bbc585005d53c8728ecd95a74963af3ac9 /chrome/common | |
parent | 9a248ef87e5f7f569ff32e9b2c6df3ed3622c71f (diff) | |
download | chromium_src-4acbedb1fe0f43802c64027cf7cbf81441297a95.zip chromium_src-4acbedb1fe0f43802c64027cf7cbf81441297a95.tar.gz chromium_src-4acbedb1fe0f43802c64027cf7cbf81441297a95.tar.bz2 |
Revamp the docs for message passing, including the new sendRequest API.
BUG=26830
Review URL: http://codereview.chromium.org/375006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31265 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
36 files changed, 973 insertions, 141 deletions
diff --git a/chrome/common/extensions/api/extension_api.json b/chrome/common/extensions/api/extension_api.json index b6f00e3..baf1660 100755 --- a/chrome/common/extensions/api/extension_api.json +++ b/chrome/common/extensions/api/extension_api.json @@ -69,7 +69,14 @@ { "type": "function", "name": "responseCallback", - "optional": true + "optional": true, + "parameters": [ + { + "name": "response", + "type": "any", + "description": "The JSON response object sent by the handler of the request." + } + ] } ] }, @@ -461,7 +468,14 @@ { "type": "function", "name": "responseCallback", - "optional": true + "optional": true, + "parameters": [ + { + "name": "response", + "type": "any", + "description": "The JSON response object sent by the handler of the request." + } + ] } ] }, diff --git a/chrome/common/extensions/docs/api_index.html b/chrome/common/extensions/docs/api_index.html index 19a4131..4021cff 100644 --- a/chrome/common/extensions/docs/api_index.html +++ b/chrome/common/extensions/docs/api_index.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/api_other.html b/chrome/common/extensions/docs/api_other.html index 935500d..44109037 100644 --- a/chrome/common/extensions/docs/api_other.html +++ b/chrome/common/extensions/docs/api_other.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/autoupdate.html b/chrome/common/extensions/docs/autoupdate.html index 9b083d3..3250c73 100644 --- a/chrome/common/extensions/docs/autoupdate.html +++ b/chrome/common/extensions/docs/autoupdate.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/background_pages.html b/chrome/common/extensions/docs/background_pages.html index 2acdad4..13e9639 100644 --- a/chrome/common/extensions/docs/background_pages.html +++ b/chrome/common/extensions/docs/background_pages.html @@ -121,6 +121,7 @@ <ul> <li class="leftNavSelected">Background Pages</li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/bookmarks.html b/chrome/common/extensions/docs/bookmarks.html index 36afd0b..4903bd7 100644 --- a/chrome/common/extensions/docs/bookmarks.html +++ b/chrome/common/extensions/docs/bookmarks.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/browserAction.html b/chrome/common/extensions/docs/browserAction.html index be78639..a01ca9c 100644 --- a/chrome/common/extensions/docs/browserAction.html +++ b/chrome/common/extensions/docs/browserAction.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/content_scripts.html b/chrome/common/extensions/docs/content_scripts.html index 9c8bf16..93cb4d1 100644 --- a/chrome/common/extensions/docs/content_scripts.html +++ b/chrome/common/extensions/docs/content_scripts.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li class="leftNavSelected">Content Scripts</li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> @@ -175,20 +176,13 @@ </li> </ol> </li><li jsinstance="2"> - <a href="#messaging">Messaging</a> - <ol> - <li jsinstance="*0" style="display: none; "> - <a>h3Name</a> - </li> - </ol> - </li><li jsinstance="3"> <a href="#host-page-communication">Communication with the embedding page</a> <ol> <li jsinstance="*0" style="display: none; "> <a>h3Name</a> </li> </ol> - </li><li jsinstance="*4"> + </li><li jsinstance="*3"> <a href="#extension-files">Referring to extension files</a> <ol> <li jsinstance="*0" style="display: none; "> @@ -289,7 +283,7 @@ These limitations aren't as bad as they sound. Content scripts can <em>indirectly</em> use the chrome.* APIs, get access to extension data, and request extension actions -by exchanging <a href="#messaging">messages</a> +by exchanging <a href="messaging.html">messages</a> with their parent extension. Content scripts can also <a href="#host-page-communication">communicate with web pages</a> @@ -404,68 +398,8 @@ document.getElementById("button").onclick = function() { </p><p>Another important benefit of isolated worlds is that they completely separate the JavaScript on the page from the JavaScript in extensions. This allows us to offer extra functionality to content scripts that should not be accessible from web pages without worrying about web pages accessing it. -</p><h2 id="messaging">Messaging</h2> - -<p>Content scripts can communicate with their parent extension using message passing. A message channel can be opened by either the content script or an extension page. Each side of the channel has a <a href="extension.html#type-Port">Port</a> object which can be used to send messages to the other side. The messages can contain any valid JSON object (null, boolean, number, string, array, or object). - -</p><p>The content script opens a channel to the extension using the <a href="extension.html#method-connect">chrome.extension.connect()</a> method. The parent extension can also open a channel to a content script in a given tab by calling <a href="tabs.html#method-connect">chrome.tabs.connect(tabId)</a>. In either case, the <a href="extension.html#event-onConnect">onConnect</a> event is fired in the targeted page(s), and a connection is established. - -</p><p>When a channel is opened from a content script to an extension, the event is -fired in all views in the extension. Any view can receive the event. - -</p><p>For example, suppose you want to write a simple login manager. You want a toolstrip button that lights up when a content script detects a "login" element in a page, and fills in the login info when you press the button. - -</p><p>Your content script would look like this: - -</p><pre>contentscript.js -================ -var e = document.getElementById("login"); - -// Create a short-lived named channel to the extension and send a single -// message through it. -var port = chrome.extension.connect({name: "notifyChannel"}); -port.postMessage({found: (e != undefined)}); - -// Also listen for new channels from the extension for when the button is -// pressed. -chrome.extension.onConnect.addListener(function(port) { - console.assert(port.name == "buttonClickedChannel"); - port.onMessage.addListener(function(msg) { - if (msg.buttonClicked) { - e.value = msg.passwordValue; - } - }); -}); -</pre> - -<p>with a toolstrip that looks like: - -</p><pre>toolstrip.html -============== -<div class="toolstrip-button" id="btn" onclick="onClick()"> - Fill Password -</div> -<script> -// Listen for notifications from the content script. -chrome.extension.onConnect.addListener(function(port) { - console.assert(port.name == "notifyChannel"); - port.onMessage.addListener(function(msg) { - // Color our button based on whether a login element was found. - var color = msg.found ? "blue" : "grey"; - document.getElementById("btn").style.backgroundColor = color; - }); -}); -function onClick() { - // Send our password to the current tab when clicked. - chrome.tabs.getSelected(null, function(tab) { - var port = chrome.tabs.connect(tab.id, {name: "buttonClickedChannel"}); - port.postMessage({buttonClicked: true}); - }); -} -</script> -</pre> -<h2 id="host-page-communication">Communication with the embedding page</h2> +</p><h2 id="host-page-communication">Communication with the embedding page</h2> <p>Although the execution environments of content scripts and the pages that host them are isolated from each other, they share access to the page's DOM. If the page wishes to communicate with the content script (or with the extension via the content script), it must do so through the shared DOM.</p> diff --git a/chrome/common/extensions/docs/devguide.html b/chrome/common/extensions/docs/devguide.html index 3a58f42..4a4a488 100644 --- a/chrome/common/extensions/docs/devguide.html +++ b/chrome/common/extensions/docs/devguide.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/devtools.html b/chrome/common/extensions/docs/devtools.html index 98fd74b..b39c0eb 100644 --- a/chrome/common/extensions/docs/devtools.html +++ b/chrome/common/extensions/docs/devtools.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/events.html b/chrome/common/extensions/docs/events.html index 8983491..b0620b2 100644 --- a/chrome/common/extensions/docs/events.html +++ b/chrome/common/extensions/docs/events.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/extension.html b/chrome/common/extensions/docs/extension.html index 2174eae..bd0c963 100644 --- a/chrome/common/extensions/docs/extension.html +++ b/chrome/common/extensions/docs/extension.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> @@ -1357,11 +1358,49 @@ For details, see </p> <!-- Note: intentionally longer 80 columns --> - <pre>function(<span>null</span>) <span class="subdued">{...}</span>);</pre> + <pre>function(<span>any response</span>) <span class="subdued">{...}</span>);</pre> <dl> - <div style="display: none; "> + <div jsinstance="*0"> <div> - </div> + <dt> + <var>response</var> + <em> + + <!-- TYPE --> + <div style="display:inline"> + ( + <span class="optional" style="display: none; ">optional</span> + <span id="typeTemplate"> + <span style="display: none; "> + <a> Type</a> + </span> + <span> + <span style="display: none; "> + array of <span><span></span></span> + </span> + <span>any</span> + </span> + </span> + ) + </div> + + </em> + </dt> + <dd class="todo" style="display: none; "> + Undocumented. + </dd> + <dd>The JSON response object sent by the handler of the request.</dd> + + <!-- OBJECT PROPERTIES --> + <dd style="display: none; "> + <dl> + <div> + <div> + </div> + </div> + </dl> + </dd> + </div> </div> </dl> </div> diff --git a/chrome/common/extensions/docs/getstarted.html b/chrome/common/extensions/docs/getstarted.html index 05b22d4..0ed3bfc 100644 --- a/chrome/common/extensions/docs/getstarted.html +++ b/chrome/common/extensions/docs/getstarted.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/hosting.html b/chrome/common/extensions/docs/hosting.html index 3068556..68521e2 100644 --- a/chrome/common/extensions/docs/hosting.html +++ b/chrome/common/extensions/docs/hosting.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/i18n.html b/chrome/common/extensions/docs/i18n.html index fb458bb..ec45a96 100644 --- a/chrome/common/extensions/docs/i18n.html +++ b/chrome/common/extensions/docs/i18n.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/index.html b/chrome/common/extensions/docs/index.html index f82273d..76fcc2d 100644 --- a/chrome/common/extensions/docs/index.html +++ b/chrome/common/extensions/docs/index.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/manifest.html b/chrome/common/extensions/docs/manifest.html index 145be1f..41170b0 100644 --- a/chrome/common/extensions/docs/manifest.html +++ b/chrome/common/extensions/docs/manifest.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/match_patterns.html b/chrome/common/extensions/docs/match_patterns.html index 307506a..4ee01d4 100644 --- a/chrome/common/extensions/docs/match_patterns.html +++ b/chrome/common/extensions/docs/match_patterns.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/messaging.html b/chrome/common/extensions/docs/messaging.html new file mode 100644 index 0000000..1d17866 --- /dev/null +++ b/chrome/common/extensions/docs/messaging.html @@ -0,0 +1,622 @@ +<!DOCTYPE html><!-- This page is a placeholder for generated extensions api doc. Note: + 1) The <head> information in this page is significant, should be uniform + across api docs and should be edited only with knowledge of the + templating mechanism. + 3) All <body>.innerHTML is genereated as an rendering step. If viewed in a + browser, it will be re-generated from the template, json schema and + authored overview content. + 4) The <body>.innerHTML is also generated by an offline step so that this + page may easily be indexed by search engines. +--><html xmlns="http://www.w3.org/1999/xhtml"><head> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> + <link href="css/ApiRefStyles.css" rel="stylesheet" type="text/css"> + <link href="css/print.css" rel="stylesheet" type="text/css" media="print"> + <script type="text/javascript" src="../../../third_party/jstemplate/jstemplate_compiled.js"> + </script> + <script type="text/javascript" src="js/api_page_generator.js"></script> + <script type="text/javascript" src="js/bootstrap.js"></script> + <title>Message Passing</title></head><body> <div id="container"> + <!-- SUBTEMPLATES: DO NOT MOVE FROM THIS LOCATION --> + <!-- In particular, sub-templates that recurse, must be used by allowing + jstemplate to make a copy of the template in this section which + are not operated on by way of the jsskip="true" --> + <div style="display:none"> + + <!-- VALUE --> + <div id="valueTemplate"> + <dt> + <var>paramName</var> + <em> + + <!-- TYPE --> + <div style="display:inline"> + ( + <span class="optional">optional</span> + <span id="typeTemplate"> + <span> + <a> Type</a> + </span> + <span> + <span> + array of <span><span></span></span> + </span> + <span>paramType</span> + </span> + </span> + ) + </div> + + </em> + </dt> + <dd class="todo"> + Undocumented. + </dd> + <dd> + Description of this parameter from the json schema. + </dd> + + <!-- OBJECT PROPERTIES --> + <dd> + <dl> + <div> + <div> + </div> + </div> + </dl> + </dd> + </div> <!-- /VALUE --> + + </div> <!-- /SUBTEMPLATES --> + + <a name="top"> </a> + <!-- API HEADER --> + <div id="pageHeader"> + <div id="searchbox"> + <form action="http://www.google.com/cse" id="cse-search-box"> + <div> + <input type="hidden" name="cx" value="002967670403910741006:61_cvzfqtno"> + <input type="hidden" name="ie" value="UTF-8"> + <input type="text" name="q" size="31"> + <input type="submit" name="sa" value="Search"> + </div> + </form> + + <script type="text/javascript" src="http://www.google.com/jsapi"></script> + <script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script> + <script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script> + <script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script> + </div> + <div id="pageTitle"> + <h1>Message Passing</h1> + </div> + </div> <!-- /pageHeader --> + + <div id="pageContent"> + <!-- SIDENAV --> + <div id="leftNav"> + <ul> + <li> <a href="index.html">Home</a></li> + <li> <a href="getstarted.html">Getting Started</a></li> + <li> <a href="overview.html">Overview</a></li> + <li> <a href="devguide.html"><div>Developer's Guide</div></a> + <ul> + <li> Browser UI + <ul> + <li><a href="browserAction.html">Browser Actions</a></li> + <li><a href="ntp.html">New Tab Pages</a></li> + <li><a href="options.html">Options Pages</a></li> + <li><a href="pageAction.html">Page Actions</a></li> + <li><a href="themes.html">Themes</a></li> + </ul> + </li> + <li> Browser Interaction + <ul> + <li><a href="bookmarks.html">Bookmarks</a></li> + <li><a href="events.html">Events</a></li> + <li><a href="tabs.html">Tabs</a></li> + <li><a href="windows.html">Windows</a></li> + </ul> + </li> + <li> Implementation + <ul> + <li><a href="background_pages.html">Background Pages</a></li> + <li><a href="content_scripts.html">Content Scripts</a></li> + <li class="leftNavSelected">Message Passing</li> + <li><a href="xhr.html">Cross-Origin XHR</a></li> + <li><a href="npapi.html">NPAPI Plugins</a></li> + </ul> + </li> + <li> Finishing + <ul> + <li><a href="autoupdate.html">Autoupdating</a></li> + <li><a href="packaging.html">Packaging</a></li> + </ul> + </li> + </ul> + </li> + <li><a href="tutorials.html"><div>Tutorials</div></a> + <ul> + <li><a href="tut_debugging.html">Debugging</a></li> + </ul> + </li> + <li>Reference + <ul> + <li> Formats + <ul> + <li><a href="manifest.html">Manifest Files</a></li> + <li><a href="match_patterns.html">Match Patterns</a></li> + <!-- <li>Packages (.crx)</li> --> + </ul> + </li> + <li> <a href="api_index.html">chrome.* APIs</a> </li> + <li> <a href="api_other.html">Other APIs</a> </li> + </ul> + </li> + <li><a href="http://dev.chromium.org/developers/design-documents/extensions/samples">Samples</a></li> + </ul> + </div> + + <div id="mainColumn"> + <!-- TABLE OF CONTENTS --> + <div id="toc"> + <p>Contents</p> + <ol> + <li jsinstance="0"> + <a href="#simple">Simple one-time requests</a> + <ol> + <li jsinstance="*0" style="display: none; "> + <a>h3Name</a> + </li> + </ol> + </li><li jsinstance="1"> + <a href="#connect">Long-lived connections</a> + <ol> + <li jsinstance="*0" style="display: none; "> + <a>h3Name</a> + </li> + </ol> + </li><li jsinstance="*2"> + <a href="#external">Cross-extension messaging</a> + <ol> + <li jsinstance="*0" style="display: none; "> + <a>h3Name</a> + </li> + </ol> + </li> + <div style="display: none; "> + <li> + <a href="#apiReference">API reference</a> + <ol> + <li> + <a href="#properties">Properties</a> + <ol> + <li> + <a href="#property-anchor">propertyName</a> + </li> + </ol> + </li> + <li> + <a href="#methods">Methods</a> + <ol> + <li> + <a href="#method-anchor">methodName</a> + </li> + </ol> + </li> + <li> + <a href="#events">Events</a> + <ol> + <li> + <a href="#event-anchor">eventName</a> + </li> + </ol> + </li> + <li> + <a href="#types">Types</a> + <ol> + <li> + <a href="#id-anchor">id</a> + </li> + </ol> + </li> + </ol> + </li> + </div> + </ol> + </div> + <!-- /TABLE OF CONTENTS --> + + <!-- STATIC CONTENT PLACEHOLDER --> + <div id="static"><div id="pageData-title" class="pageData">Message Passing</div> +<div id="pageData-showTOC" class="pageData">true</div> + +<p> +Since content scripts run in the context of a web page and not the extension, +they often need some way of communicating with the rest of the extension. For +example, an RSS reader extension might use content scripts to detect the +presence of an RSS feed on a page, then notify the background page in order to +display a page action icon for that page. + +</p><p> +Communication between extensions and their content scripts works by using +message passing. Either side can listen for messages sent from the other end, +and respond on the same channel. A message can contain any valid JSON object +(null, boolean, number, string, array, or object). There is a simple API for +<a href="#simple">one-time requests</a> +and a more complex API that allows you to have +<a href="#connect">long-lived connections</a> +for exchanging multiple messages with a shared context. It is also possible to +send a message to another extension if you know its ID, which is covered in +the +<a href="#external">cross-extension messages</a> +section. + + +</p><h2 id="simple">Simple one-time requests</h2> +<p> +If you only need to send a single message to another part of your extension +(and optionally get a response back), you should use the simplified +<a href="extension.html#method-sendRequest">chrome.extension.sendRequest()</a> +or +<a href="tabs.html#method-sendRequest">chrome.tabs.sendRequest()</a> +methods. This lets you send a one-time JSON-serializable message from a +content script to extension, or vice versa, respectively. An optional +callback parameter allows you handle the response from the other side, if +there is one. + +</p><p> +Sending a request from a content script looks like this: +</p><pre>contentscript.js +================ +chrome.extension.sendRequest({greeting: "hello"}, function(response) { + console.log(response.farewell); +}); +</pre> + +<p> +Sending a request from the extension to a content script looks very similar, +except that you need to specify which tab to send it to. This example +demonstrates sending a message to the content script in the selected tab. +</p><pre>background.html +=============== +chrome.tabs.getSelected(null, function(tab) { + chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) { + console.log(response.farewell); + }); +}); +</pre> + +<p> +On the receiving end, you need to set up an +<a href="extension.html#event-onRequest">chrome.extension.onRequest</a> +event listener to handle the message. This looks the same from a content +script or extension page. The request will remain open until you call +sendResponse, so it is good practice to call sendResponse with an empty +object to allow the request to be cleaned up. +</p><pre>chrome.extension.onRequest.addListener( + function(request, sender, sendResponse) { + console.log(sender.tab ? + "from a content script:" + sender.tab.url : + "from the extension"); + if (request.greeting == "hello") + sendResponse({farewell: "goodbye"}); + else + sendResponse({}); // snub them. + }); +</pre> + + +<h2 id="connect">Long-lived connections</h2> +<p> +Sometimes it's useful to have a conversation that lasts longer than a single +request and response. In this case, you can open a long-lived channel from +your content script to an extension page, or vice versa, using +<a href="extension.html#method-connect">chrome.extension.connect()</a> +or +<a href="tabs.html#method-connect">chrome.tabs.connect()</a> respectively. The +channel can optionally have a name, allowing you to distinguish between +different types of connections. + +</p><p> +One use case might be an automatic form fill extension. The content script +could open a channel to the extension page for a particular login, and send a +message to the extension for each input element on the page to request the +form data to fill in. The shared connection allows the extension to keep +shared state linking the several messages coming from the content script. + +</p><p> +When establishing a connection, each end is given a +<a href="extension.html#type-Port">Port</a> +object which is used for sending and receiving messages through that +connection. + +</p><p> +Here is how you open a channel from a content script, and send and listen for +messages: +</p><pre>contentscript.js +================ +var port = chrome.extension.connect({name: "knockknock"}); +port.postMessage({joke: "Knock knock"}); +port.onMessage.addListener(function(msg) { + if (msg.question == "Who's there?") + port.postMessage({answer: "Madame"}); + else if (msg.question == "Madame who?") + port.postMessage({answer: "Madame... Bovary"); +}); +</pre> + +<p> +Sending a request from the extension to a content script looks very similar, +except that you need to specify which tab to connect to. Simply replace the +call to connect in the above example with +<a href="tabs.html#method-connect">chrome.tabs.connect(tabId, {name: +"knockknock"})</a>. + +</p><p> +In order to handle incoming connections, you need to set up a +<a href="extension.html#event-onConnect">chrome.extension.onConnect</a> +event listener. This looks the same from a content script or an extension +page. When another part of your extension calls "connect()", this event is +fired, along with the +<a href="extension.html#type-Port">Port</a> +object you can use to send and receive messages through the connection. Here's +what it looks like to respond to incoming connections: +</p><pre>chrome.extension.onConnect.addListener(function(port) { + console.assert(port.name == "knockknock"); + port.onMessage.addListener(function(msg) { + if (msg.joke == "Knock knock") + port.postMessage({question: "Who's there?"}); + else if (msg.answer == "Madame") + port.postMessage({question: "Madame who?"}); + else if (msg.answer == "Madame... Bovary") + port.postMessage({question: "I don't get it."}); + }); +}); +</pre> + +<p> +You may want to find out when a connection is closed, for example if you are +maintaining separate state for each open port. For this you can listen to the +<a href="extension.html#type-Port">Port.onDisconnect</a> +event. This event is fired either when the other side of the channel manually +calls +<a href="extension.html#type-Port">Port.disconnect()</a>, or when the page +containing the port is unloaded (for example if the tab is navigated). +onDisconnect is guaranteed to be fired only once for any given port. + + +</p><h2 id="external">Cross-extension messaging</h2> +<p> +In addition to sending messages between different components in your +extension, you can use the messaging API to communicate with other extensions. +This lets you expose a public API that other extensions can take advantage of. + +</p><p> +Listening for incoming requests and connections is similar to the internal +case, except you use the +<a href="extension.html#event-onRequestExternal">chrome.extension.onRequestExternal</a> +or +<a href="extension.html#event-onConnectExternal">chrome.extension.onConnectExternal</a> +methods. Here's an example of each: +</p><pre>// For simple requests: +chrome.extension.onRequestExternal.addListener( + function(request, sender, sendResponse) { + if (sender.id == blacklistedExtension) + sendResponse({}); // don't allow this extension access + else if (request.getTargetData) + sendResponse({targetData: targetData}); + else if (request.activateLasers) { + var success = activateLasers(); + sendResponse({activateLasers: success}); + } + }); + +// For long-lived connections: +chrome.extension.onConnectExternal.addListener(function(port) { + port.onMessage.addListener(function(msg) { + // See other examples for sample onMessage handlers. + }); +}); +</pre> + +<p> +Likewise, sending a message to another extension is similar to sending one +within your extension. The only difference is that you must pass the ID of the +extension you want to communicate with. For example: +</p><pre>// The ID of the extension we want to talk to. +var laserExtensionId = "abcdefghijklmnoabcdefhijklmnoabc"; + +// Make a simple request: +chrome.extension.sendRequest(laserExtensionId, {getTargetData: true}, + function(response) { + if (targetInRange(response.targetData)) + chrome.extension.sendRequest(laserExtensionId, {activateLasers: true}); + }); + +// Start a long-running conversation: +var port = chrome.extension.connectExternal(laserExtensionId); +port.postMessage(...); +</pre> +</div> + + <!-- API PAGE --> + <div class="apiPage" style="display: none; "> + <a name="apiReference"></a> + <h2>API reference: chrome.apiname </h2> + + <!-- PROPERTIES --> + <div class="apiGroup"> + <a name="properties"></a> + <h3 id="properties">Properties</h3> + + <div> + <a></a> + <h4>getLastError</h4> + <div class="summary"> + <!-- Note: intentionally longer 80 columns --> + <span>chrome.extension</span><span>lastError</span> + </div> + <div> + </div> + </div> + + </div> <!-- /apiGroup --> + + <!-- METHODS --> + <div class="apiGroup" id="methods"> + <a name="methods"></a> + <h3>Methods</h3> + + <!-- iterates over all functions --> + <div class="apiItem"> + <a></a> <!-- method-anchor --> + <h4>method name</h4> + + <div class="summary"><span>void</span> + <!-- Note: intentionally longer 80 columns --> + <span>chrome.module.methodName</span>(<span><span>, </span><span></span> + <var><span></span></var></span>)</div> + + <div class="description"> + <p class="todo">Undocumented.</p> + <p> + A description from the json schema def of the function goes here. + </p> + + <!-- PARAMETERS --> + <h4>Parameters</h4> + <dl> + <div> + <div> + </div> + </div> + </dl> + + <!-- RETURNS --> + <h4>Returns</h4> + <dl> + <div> + <div> + </div> + </div> + </dl> + + <!-- CALLBACK --> + <div> + <div> + <h4>Callback function</h4> + <p> + If you specify the <em>callback</em> parameter, + it should specify a function that looks like this: + </p> + + <!-- Note: intentionally longer 80 columns --> + <pre>function(<span>Type param1, Type param2</span>) <span class="subdued">{...}</span>);</pre> + <dl> + <div> + <div> + </div> + </div> + </dl> + </div> + </div> + + </div> <!-- /description --> + + </div> <!-- /apiItem --> + + </div> <!-- /apiGroup --> + + <!-- EVENTS --> + <div class="apiGroup"> + <a name="events"></a> + <h3 id="events">Events</h3> + + <!-- iterates over all events --> + <div class="apiItem"> + <a></a> + <h4>event name</h4> + + <div class="summary"> + <!-- Note: intentionally longer 80 columns --> + <span class="subdued">chrome.bookmarks</span><span>onEvent</span><span class="subdued">.addListener</span>(function(<span>Type param1, Type param2</span>) <span class="subdued">{...}</span>); + </div> + + <div class="description"> + <p class="todo">Undocumented.</p> + <p> + A description from the json schema def of the event goes here. + </p> + + <!-- PARAMETERS --> + <h4>Parameters</h4> + <dl> + <div> + <div> + </div> + </div> + </dl> + + </div> <!-- /decription --> + + </div> <!-- /apiItem --> + + </div> <!-- /apiGroup --> + + <!-- TYPES --> + <div class="apiGroup"> + <a name="types.sort(sortByName)"></a> + <h3 id="types">Types</h3> + + <!-- iterates over all types --> + <div class="apiItem"> + <a></a> + <h4>type name</h4> + + <div> + </div> + + </div> <!-- /apiItem --> + + </div> <!-- /apiGroup --> + + </div> <!-- /apiPage --> + </div> <!-- /mainColumn --> + </div> <!-- /pageContent --> + <div id="pageFooter" --=""> + <p> + Except as otherwise <a href="http://code.google.com/policies.html#restrictions">noted</a>, + the content of this page is licensed under the <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons + Attribution 3.0 License</a>, and code samples are licensed under the + <a rel="license" href="http://code.google.com/google_bsd_license.html">BSD License</a>. + </p> + <p> + ©2009 Google + </p> + +<!-- begin analytics --> +<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script> +<script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script> + +<script type="text/javascript"> + // chrome doc tracking + try { + var engdocs = _gat._getTracker("YT-10763712-2"); + engdocs._trackPageview(); + } catch(err) {} + + // code.google.com site-wide tracking + try { + _uacct="UA-18071-1"; + _uanchor=1; + _uff=0; + urchinTracker(); + } + catch(e) {/* urchinTracker not available. */} +</script> +<!-- end analytics --> + + </div> <!-- /pageFooter --> + </div> <!-- /container --> +</body></html> + diff --git a/chrome/common/extensions/docs/npapi.html b/chrome/common/extensions/docs/npapi.html index e17e794..9299f62 100644 --- a/chrome/common/extensions/docs/npapi.html +++ b/chrome/common/extensions/docs/npapi.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li class="leftNavSelected">NPAPI Plugins</li> </ul> diff --git a/chrome/common/extensions/docs/options.html b/chrome/common/extensions/docs/options.html index 6d9212d..5ef07ff 100644 --- a/chrome/common/extensions/docs/options.html +++ b/chrome/common/extensions/docs/options.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/override.html b/chrome/common/extensions/docs/override.html index 145a5a7..5a6b4de 100644 --- a/chrome/common/extensions/docs/override.html +++ b/chrome/common/extensions/docs/override.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/overview.html b/chrome/common/extensions/docs/overview.html index 7ab3dc4..aa7f735 100644 --- a/chrome/common/extensions/docs/overview.html +++ b/chrome/common/extensions/docs/overview.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/packaging.html b/chrome/common/extensions/docs/packaging.html index d2369f5..61b67ef 100644 --- a/chrome/common/extensions/docs/packaging.html +++ b/chrome/common/extensions/docs/packaging.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/pageAction.html b/chrome/common/extensions/docs/pageAction.html index f9c6575..b69cef6 100644 --- a/chrome/common/extensions/docs/pageAction.html +++ b/chrome/common/extensions/docs/pageAction.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/static/content_scripts.html b/chrome/common/extensions/docs/static/content_scripts.html index 9f1edf1..83a7e8e 100644 --- a/chrome/common/extensions/docs/static/content_scripts.html +++ b/chrome/common/extensions/docs/static/content_scripts.html @@ -47,7 +47,7 @@ These limitations aren't as bad as they sound. Content scripts can <em>indirectly</em> use the chrome.* APIs, get access to extension data, and request extension actions -by exchanging <a href="#messaging">messages</a> +by exchanging <a href="messaging.html">messages</a> with their parent extension. Content scripts can also <a href="#host-page-communication">communicate with web pages</a> @@ -162,68 +162,6 @@ document.getElementById("button").onclick = function() { <p>Another important benefit of isolated worlds is that they completely separate the JavaScript on the page from the JavaScript in extensions. This allows us to offer extra functionality to content scripts that should not be accessible from web pages without worrying about web pages accessing it. -<h2 id="messaging">Messaging</h2> - -<p>Content scripts can communicate with their parent extension using message passing. A message channel can be opened by either the content script or an extension page. Each side of the channel has a <a href="extension.html#type-Port">Port</a> object which can be used to send messages to the other side. The messages can contain any valid JSON object (null, boolean, number, string, array, or object). - -<p>The content script opens a channel to the extension using the <a href="extension.html#method-connect">chrome.extension.connect()</a> method. The parent extension can also open a channel to a content script in a given tab by calling <a href="tabs.html#method-connect">chrome.tabs.connect(tabId)</a>. In either case, the <a href="extension.html#event-onConnect">onConnect</a> event is fired in the targeted page(s), and a connection is established. - -<p>When a channel is opened from a content script to an extension, the event is -fired in all views in the extension. Any view can receive the event. - -<p>For example, suppose you want to write a simple login manager. You want a toolstrip button that lights up when a content script detects a "login" element in a page, and fills in the login info when you press the button. - -<p>Your content script would look like this: - -<pre> -contentscript.js -================ -var e = document.getElementById("login"); - -// Create a short-lived named channel to the extension and send a single -// message through it. -var port = chrome.extension.connect({name: "notifyChannel"}); -port.postMessage({found: (e != undefined)}); - -// Also listen for new channels from the extension for when the button is -// pressed. -chrome.extension.onConnect.addListener(function(port) { - console.assert(port.name == "buttonClickedChannel"); - port.onMessage.addListener(function(msg) { - if (msg.buttonClicked) { - e.value = msg.passwordValue; - } - }); -}); -</pre> - -<p>with a toolstrip that looks like: - -<pre> -toolstrip.html -============== -<div class="toolstrip-button" id="btn" onclick="onClick()"> - Fill Password -</div> -<script> -// Listen for notifications from the content script. -chrome.extension.onConnect.addListener(function(port) { - console.assert(port.name == "notifyChannel"); - port.onMessage.addListener(function(msg) { - // Color our button based on whether a login element was found. - var color = msg.found ? "blue" : "grey"; - document.getElementById("btn").style.backgroundColor = color; - }); -}); -function onClick() { - // Send our password to the current tab when clicked. - chrome.tabs.getSelected(null, function(tab) { - var port = chrome.tabs.connect(tab.id, {name: "buttonClickedChannel"}); - port.postMessage({buttonClicked: true}); - }); -} -</script> -</pre> <h2 id="host-page-communication">Communication with the embedding page</h2> diff --git a/chrome/common/extensions/docs/static/messaging.html b/chrome/common/extensions/docs/static/messaging.html new file mode 100644 index 0000000..b850fad --- /dev/null +++ b/chrome/common/extensions/docs/static/messaging.html @@ -0,0 +1,217 @@ +<div id="pageData-title" class="pageData">Message Passing</div> +<div id="pageData-showTOC" class="pageData">true</div> + +<p> +Since content scripts run in the context of a web page and not the extension, +they often need some way of communicating with the rest of the extension. For +example, an RSS reader extension might use content scripts to detect the +presence of an RSS feed on a page, then notify the background page in order to +display a page action icon for that page. + +<p> +Communication between extensions and their content scripts works by using +message passing. Either side can listen for messages sent from the other end, +and respond on the same channel. A message can contain any valid JSON object +(null, boolean, number, string, array, or object). There is a simple API for +<a href="#simple">one-time requests</a> +and a more complex API that allows you to have +<a href="#connect">long-lived connections</a> +for exchanging multiple messages with a shared context. It is also possible to +send a message to another extension if you know its ID, which is covered in +the +<a href="#external">cross-extension messages</a> +section. + + +<h2 id="simple">Simple one-time requests</h2> +<p> +If you only need to send a single message to another part of your extension +(and optionally get a response back), you should use the simplified +<a href="extension.html#method-sendRequest">chrome.extension.sendRequest()</a> +or +<a href="tabs.html#method-sendRequest">chrome.tabs.sendRequest()</a> +methods. This lets you send a one-time JSON-serializable message from a +content script to extension, or vice versa, respectively. An optional +callback parameter allows you handle the response from the other side, if +there is one. + +<p> +Sending a request from a content script looks like this: +<pre> +contentscript.js +================ +chrome.extension.sendRequest({greeting: "hello"}, function(response) { + console.log(response.farewell); +}); +</pre> + +<p> +Sending a request from the extension to a content script looks very similar, +except that you need to specify which tab to send it to. This example +demonstrates sending a message to the content script in the selected tab. +<pre> +background.html +=============== +chrome.tabs.getSelected(null, function(tab) { + chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) { + console.log(response.farewell); + }); +}); +</pre> + +<p> +On the receiving end, you need to set up an +<a href="extension.html#event-onRequest">chrome.extension.onRequest</a> +event listener to handle the message. This looks the same from a content +script or extension page. The request will remain open until you call +sendResponse, so it is good practice to call sendResponse with an empty +object to allow the request to be cleaned up. +<pre> +chrome.extension.onRequest.addListener( + function(request, sender, sendResponse) { + console.log(sender.tab ? + "from a content script:" + sender.tab.url : + "from the extension"); + if (request.greeting == "hello") + sendResponse({farewell: "goodbye"}); + else + sendResponse({}); // snub them. + }); +</pre> + + +<h2 id="connect">Long-lived connections</h2> +<p> +Sometimes it's useful to have a conversation that lasts longer than a single +request and response. In this case, you can open a long-lived channel from +your content script to an extension page, or vice versa, using +<a href="extension.html#method-connect">chrome.extension.connect()</a> +or +<a href="tabs.html#method-connect">chrome.tabs.connect()</a> respectively. The +channel can optionally have a name, allowing you to distinguish between +different types of connections. + +<p> +One use case might be an automatic form fill extension. The content script +could open a channel to the extension page for a particular login, and send a +message to the extension for each input element on the page to request the +form data to fill in. The shared connection allows the extension to keep +shared state linking the several messages coming from the content script. + +<p> +When establishing a connection, each end is given a +<a href="extension.html#type-Port">Port</a> +object which is used for sending and receiving messages through that +connection. + +<p> +Here is how you open a channel from a content script, and send and listen for +messages: +<pre> +contentscript.js +================ +var port = chrome.extension.connect({name: "knockknock"}); +port.postMessage({joke: "Knock knock"}); +port.onMessage.addListener(function(msg) { + if (msg.question == "Who's there?") + port.postMessage({answer: "Madame"}); + else if (msg.question == "Madame who?") + port.postMessage({answer: "Madame... Bovary"); +}); +</pre> + +<p> +Sending a request from the extension to a content script looks very similar, +except that you need to specify which tab to connect to. Simply replace the +call to connect in the above example with +<a href="tabs.html#method-connect">chrome.tabs.connect(tabId, {name: +"knockknock"})</a>. + +<p> +In order to handle incoming connections, you need to set up a +<a href="extension.html#event-onConnect">chrome.extension.onConnect</a> +event listener. This looks the same from a content script or an extension +page. When another part of your extension calls "connect()", this event is +fired, along with the +<a href="extension.html#type-Port">Port</a> +object you can use to send and receive messages through the connection. Here's +what it looks like to respond to incoming connections: +<pre> +chrome.extension.onConnect.addListener(function(port) { + console.assert(port.name == "knockknock"); + port.onMessage.addListener(function(msg) { + if (msg.joke == "Knock knock") + port.postMessage({question: "Who's there?"}); + else if (msg.answer == "Madame") + port.postMessage({question: "Madame who?"}); + else if (msg.answer == "Madame... Bovary") + port.postMessage({question: "I don't get it."}); + }); +}); +</pre> + +<p> +You may want to find out when a connection is closed, for example if you are +maintaining separate state for each open port. For this you can listen to the +<a href="extension.html#type-Port">Port.onDisconnect</a> +event. This event is fired either when the other side of the channel manually +calls +<a href="extension.html#type-Port">Port.disconnect()</a>, or when the page +containing the port is unloaded (for example if the tab is navigated). +onDisconnect is guaranteed to be fired only once for any given port. + + +<h2 id="external">Cross-extension messaging</h2> +<p> +In addition to sending messages between different components in your +extension, you can use the messaging API to communicate with other extensions. +This lets you expose a public API that other extensions can take advantage of. + +<p> +Listening for incoming requests and connections is similar to the internal +case, except you use the +<a href="extension.html#event-onRequestExternal">chrome.extension.onRequestExternal</a> +or +<a href="extension.html#event-onConnectExternal">chrome.extension.onConnectExternal</a> +methods. Here's an example of each: +<pre> +// For simple requests: +chrome.extension.onRequestExternal.addListener( + function(request, sender, sendResponse) { + if (sender.id == blacklistedExtension) + sendResponse({}); // don't allow this extension access + else if (request.getTargetData) + sendResponse({targetData: targetData}); + else if (request.activateLasers) { + var success = activateLasers(); + sendResponse({activateLasers: success}); + } + }); + +// For long-lived connections: +chrome.extension.onConnectExternal.addListener(function(port) { + port.onMessage.addListener(function(msg) { + // See other examples for sample onMessage handlers. + }); +}); +</pre> + +<p> +Likewise, sending a message to another extension is similar to sending one +within your extension. The only difference is that you must pass the ID of the +extension you want to communicate with. For example: +<pre> +// The ID of the extension we want to talk to. +var laserExtensionId = "abcdefghijklmnoabcdefhijklmnoabc"; + +// Make a simple request: +chrome.extension.sendRequest(laserExtensionId, {getTargetData: true}, + function(response) { + if (targetInRange(response.targetData)) + chrome.extension.sendRequest(laserExtensionId, {activateLasers: true}); + }); + +// Start a long-running conversation: +var port = chrome.extension.connectExternal(laserExtensionId); +port.postMessage(...); +</pre> diff --git a/chrome/common/extensions/docs/tabs.html b/chrome/common/extensions/docs/tabs.html index 39ff3fc..16dddc4 100644 --- a/chrome/common/extensions/docs/tabs.html +++ b/chrome/common/extensions/docs/tabs.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> @@ -2926,11 +2927,49 @@ For example: </p> <!-- Note: intentionally longer 80 columns --> - <pre>function(<span>null</span>) <span class="subdued">{...}</span>);</pre> + <pre>function(<span>any response</span>) <span class="subdued">{...}</span>);</pre> <dl> - <div style="display: none; "> + <div jsinstance="*0"> <div> - </div> + <dt> + <var>response</var> + <em> + + <!-- TYPE --> + <div style="display:inline"> + ( + <span class="optional" style="display: none; ">optional</span> + <span id="typeTemplate"> + <span style="display: none; "> + <a> Type</a> + </span> + <span> + <span style="display: none; "> + array of <span><span></span></span> + </span> + <span>any</span> + </span> + </span> + ) + </div> + + </em> + </dt> + <dd class="todo" style="display: none; "> + Undocumented. + </dd> + <dd>The JSON response object sent by the handler of the request.</dd> + + <!-- OBJECT PROPERTIES --> + <dd style="display: none; "> + <dl> + <div> + <div> + </div> + </div> + </dl> + </dd> + </div> </div> </dl> </div> diff --git a/chrome/common/extensions/docs/template/api_template.html b/chrome/common/extensions/docs/template/api_template.html index 518ac09..763c685 100644 --- a/chrome/common/extensions/docs/template/api_template.html +++ b/chrome/common/extensions/docs/template/api_template.html @@ -106,6 +106,7 @@ <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> </li> diff --git a/chrome/common/extensions/docs/test.html b/chrome/common/extensions/docs/test.html index 2a703c9..128e4a6 100644 --- a/chrome/common/extensions/docs/test.html +++ b/chrome/common/extensions/docs/test.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/themes.html b/chrome/common/extensions/docs/themes.html index 06b0647..a142b9b 100644 --- a/chrome/common/extensions/docs/themes.html +++ b/chrome/common/extensions/docs/themes.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/toolstrip.html b/chrome/common/extensions/docs/toolstrip.html index 8b7ef70..a337f25 100644 --- a/chrome/common/extensions/docs/toolstrip.html +++ b/chrome/common/extensions/docs/toolstrip.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/tut_debugging.html b/chrome/common/extensions/docs/tut_debugging.html index 21f1379..cfcfd8e 100644 --- a/chrome/common/extensions/docs/tut_debugging.html +++ b/chrome/common/extensions/docs/tut_debugging.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/tutorials.html b/chrome/common/extensions/docs/tutorials.html index 8180dd6..ab36fc7 100644 --- a/chrome/common/extensions/docs/tutorials.html +++ b/chrome/common/extensions/docs/tutorials.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/windows.html b/chrome/common/extensions/docs/windows.html index b848bd5..dd337c6 100644 --- a/chrome/common/extensions/docs/windows.html +++ b/chrome/common/extensions/docs/windows.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li><a href="xhr.html">Cross-Origin XHR</a></li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> diff --git a/chrome/common/extensions/docs/xhr.html b/chrome/common/extensions/docs/xhr.html index 8b1916a..00ffecf 100644 --- a/chrome/common/extensions/docs/xhr.html +++ b/chrome/common/extensions/docs/xhr.html @@ -121,6 +121,7 @@ <ul> <li><a href="background_pages.html">Background Pages</a></li> <li><a href="content_scripts.html">Content Scripts</a></li> + <li><a href="messaging.html">Message Passing</a></li> <li class="leftNavSelected">Cross-Origin XHR</li> <li><a href="npapi.html">NPAPI Plugins</a></li> </ul> |