I'm trying to access a page's localStorage
variable from content_script, similar to how a page's cookies
can be accessed from content_script.
I have no issues accessing a page's cookies from content_script.
However, localStorage.getItem("showDash")
and localStorage.setItem("showDash", "value")
don't work in content script. The browser doesn't seem to recognize it and throws an error:
Uncaught TypeError: undefined is not a function
Why does localStorage
not work in content_scripts? Any ways I could work around this?
part of manifest.json:
"content_scripts": [{
"matches": ["https://cms.mmu.edu.my/*"],
"js": ["angular.min.js", "jquery.min.js", "page_handler.js"]
}]
part of page_handler.js:
if (! localStorage.getItem("DashIsOn"))
localStorage.setItem("DashIsOn", "true");
The localStorage
is accessible in content scripts, and you can easily test it. Open a tab on stackoverflow.com (like this one with your question), keep it active, and run the following code in your background page script/console:
chrome.tabs.query({active:true}, function(tabs) {
chrome.tabs.executeScript(tabs[0].id, {code: 'alert(localStorage.getItem("se:fkey"));'});
});
You'll see that the alert will come up showing your Stack Exchange key. Therefore your problem must be in another part of the code, or, maybe, yuo're using localStorage.getItem()
somewhere else than in a content script. I suggest you to add more code to your question, like the manifest.json
file and the full content.js
script.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.