Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you have an <a href="http://code.google.com/chrome/extensions/options.html" rel="noreferrer">options page</a> and you want to communicate to the <a href="http://code.google.com/chrome/extensions/background_pages.html" rel="noreferrer">background page</a>, you can simply do, <a href="http://code.google.com/chrome/extensions/extension.html#method-getBackgroundPage" rel="noreferrer">chrome.extension.getBackgroundPage()</a></p> <h2>Options Page communicating to the Background Page</h2> <hr> <p><strong>options.html</strong></p> <pre><code>var bkg = chrome.extension.getBackgroundPage() bkg.startExtension(); bkg.stopExtension(); </code></pre> <p><strong>background.html</strong></p> <pre><code>function startExtension() { console.log('Starting Extension'); } function stopExtension() { console.log('Stopping Extension'); } </code></pre> <h2>Content Script communicating to the Background Page</h2> <hr> <p>When you are referring to "Code being injected to the page" is that any website? If so, you would need to use a <a href="http://code.google.com/chrome/extensions/content_scripts.html" rel="noreferrer">content script</a> with <a href="http://code.google.com/chrome/extensions/messaging.html" rel="noreferrer">Message Passing</a>. To do so, you can do this.</p> <p><strong>content_script.js</strong></p> <pre><code>chrome.extension.sendRequest({action:'start'}, function(response) { console.log('Start action sent'); }); </code></pre> <p><strong>background.html</strong></p> <pre><code>function onRequest(request, sender, sendResponse) { if (request.action == 'start') startExtension() else if (request.action == 'stop') stopExtension() sendResponse({}); }; chrome.extension.onRequest.addListener(onRequest); </code></pre> <p>In any case, <a href="http://code.google.com/chrome/extensions/messaging.html" rel="noreferrer">message passing</a> is a good read for anyone coming into extensions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload