Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've set up your extension to inject your code using a content script. A content script will get injected into any page that matches your matches field. What you want is to only inject the script if they click on your Browser action. For this you can either have a background page that listens for the chrome.browserAction.onClicked event and reacts to it. Or you can have a default page for your Browser Action that injects the code into the page and if your put window.close as part of the script this will stop any popup from occuring and allow you to avoid using a background page (Im big on avoiding using a background page when ever possible). </p> <p>For an example of the first method, check out this sample....<br> <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/browserAction/make_page_red/" rel="nofollow">http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/browserAction/make_page_red/</a> </p> <p>And here's an example of the second method (which is a modified version of the set_page_color sample)...<br> <strong>manifest.json</strong></p> <pre><code>{ "name": "A browser action that changes the page color.", "version": "1.0", "permissions": [ "tabs", "http://*/*", "https://*/*" ], "browser_action": { "default_title": "Set this page's color.", "default_icon": "icon.png", "default_popup": "popup.html" }, "manifest_version": 2 } </code></pre> <p><strong>popup.html</strong></p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;script src="popup.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>popup.js</strong></p> <pre><code>chrome.tabs.executeScript(null,{file:"injectedCode.js"}); window.close(); </code></pre> <p><strong>injectedCode.js</strong> </p> <pre><code>// This was just a quick look at changing text on a page // there could be more tags you should ignore and there could be a better way of doing this all together var ignoreTags = ["NOSCRIPT","SCRIPT","STYLE"]; var nodeIterator = document.createNodeIterator( document, NodeFilter.SHOW_TEXT, function (node){ var parentTag = node.parentNode.tagName.toUpperCase(); if ( ignoreTags.indexOf(parentTag)==-1 ) {return true} else {return false}; }, false ); var node; while ( (node = nodeIterator.nextNode()) ) { node.data = node.data.replace(/a/g, 'z'); } </code></pre> <p><strong>NOTE</strong><br> As Rob W points out in the comments <code>chrome.tabs</code> and <code>chrome.browserAction</code> are not usable in a content script which is why the <code>chrome.browserAction.onClicked</code> has to be used in a background page.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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