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.