0

I did simple example page redder as given in chrome api page example. There, they used background script to change the background color via executeScript. I don't want to do that, I want to use content script only (easier access to DOM). Here is my code: 1) manifest.json

{
  "manifest_version": 2,
  "name": "Modified page redder example",
  "version": "1.0",
  "icons": { "16": "icon_16.png",
             "48": "icon_48.png",
            "128": "icon_128.png" },

  "permissions": [
      "activeTab"
    ],

  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",

  "content_scripts" : [
     {
       "matches" : [ "<all_urls>" ],
       "js" : [ "contentscript.js" ]
     }
   ],
   "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html",
        "default_title": "Load apps"
    }
}

2) myscript.js

document.getElementById('btchange').onclick=function(){
    chrome.runtime.sendMessage('red');
}

3) popup.html

<html>
<body>
    <input type='button' value='change' id='btchange'>
    <script src='myscript.js'></script>
</body>
</html>

4) contentscript.js

chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse){
        //chrome.tabs.executeScript({code: 'document.body.style.backgroundColor = "'+request+'"'});
        alert(request);
    }
);

I don't receive the result as I want. No error. No promt (by alert red).

Freelancer
  • 837
  • 6
  • 21
  • 1
    To send a message to a tab's content script use chrome.`tabs`.sendMessage. – wOxxOm Mar 21 '17 at 03:34
  • I tried, but same result! Didn't work. – Freelancer Mar 21 '17 at 03:47
  • 1
    You did it wrong :-) it does work. I'm off to bed so here's a [zipped](https://puu.sh/uSwKZ/edf6f0100b.zip) working example. Hint: read the documentation for every API method because they have different parameters. Even I do that. – wOxxOm Mar 21 '17 at 04:02
  • Also note, the content script is autoinjected only on page load/reload so you might want to use executeScript instead. – wOxxOm Mar 21 '17 at 04:04
  • Thank you, it works nice :) I have one more question, if in myscript (as your zip file), I use chrome.tabs.getSelected, not use chrome.tabs.query. Will be same? The chrome.tabs.query has many param, so it makes me confused :) – Freelancer Mar 21 '17 at 04:08
  • Like I said, read the documentation, don't ask me. The documentation says getSelected is deprecated so unless you like taking the unnecessary risks it's obviously not a good choice. – wOxxOm Mar 21 '17 at 04:10
  • Thank you bro :) – Freelancer Mar 21 '17 at 04:16
  • If your user interaction *begins* with the user clicking a `browserAction` button, then the content script should be injected with [`chrome.tabs.executeScript()`](https://developer.chrome.com/extensions/tabs#method-executeScript) instead of a *manifest.json* `content_script` entry. That way your content script does not burden the browser by being injected into every page just to wait to be used. Using `chrome.tabs.executeScript()`, the script can begin functioning when it is injected with [the data, if any is needed, that has been passed to it](http://stackoverflow.com/a/40815514/3773011). – Makyen Mar 21 '17 at 05:33
  • To be specific, you don't need a background script to use `tabs.executeScript()`. Your popup code is operating in the background context. Thus, it can use `tabs.executeScript()` to inject the content script. It can do this instead of, or just before, the `tabs.query()` and `tabs.sendMessage()` which it should be using instead of `runtime.sendMessage()` (`runtime.sendMessage()` *cannot* send a message to your content script, as has already been mentioned). – Makyen Mar 21 '17 at 05:40

0 Answers0