0

I have a chrome extension with a simple pop-up. The pop-up contains a button "Add site". When the button is pressed I simply want to add the site name to a list of tracked sites. In order to do this I want to do

localStorage.setItem("isTracked",1);

When the user loads a page I can check the local storage to see if the page is tracked. I cannot simply say localStorage.setItem since it doesn't access the website's local storage and so I must use content scripts. This is my content script part of the manifest.json file

"content_scripts": [ {
    "js":        [ "runThisOnLoad.js" ],
    "matches":   [ "<all_urls>"],
    "run_at":    "document_start"
},
{
    "js":        [ "storeInLocal.js" ],
    "matches":   [ "<all_urls>"]
} ],

runThisOnLoad.js is the method I use to check the local storage ant it works fine.

I have this line of code in my popup.js which runs when the button is pressed (to send message):

    chrome.runtime.sendMessage(1);

and this line of code in my storeInLocal.js (to receive message):

chrome.runtime.onMessage.addListener(function(response, sender, sendResponse) {
        localStorage.setItem("isTracked",response);
});

Yet the item does not get stored in local storage.

How do I get the storeInLocal.js to inject the code into the webpage and store the data?

I have searched a lot around the internet for about 2 days now but with no answers. The chrome documentation on Messaging and chrome.storage don't help either.

  • You need chrome.tabs.sendMessage – wOxxOm Feb 04 '17 at 12:58
  • "content_scripts": [ { "js": [{ "runThisOnLoad.js" ,"storeInLocal.js"}], "matches": [ ""], "run_at": "document_start" } ], – ankur kumar Feb 04 '17 at 14:31
  • 3
    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 Feb 04 '17 at 21:06
  • Please [edit] the question to be on-topic: include a **complete** [mcve] that *duplicates the problem*. Including a *manifest.json*, some of the background/content/popup scripts/HTML. Questions seeking debugging help ("**why isn't this code working?**") must include: ►the desired behavior, ►a specific problem or error *and* ►the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: "**How to create a [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Feb 04 '17 at 21:07
  • @Makyen Thanks! executeScript() worked. Post your comment as answer and I will mark it as the answer. – Phantom intruder Feb 05 '17 at 15:52

0 Answers0