Reputation: 21
I am trying to store a key on both the current page, and an external page. In the example below I use google.
url
and x
are both defined in the function earlier on.
window.localStorage.setItem(url, x);
var url2 = "http://google.com/";
document.location = url2;
storeToDB(url, x);
function storeToDB(url, x) {
setTimeout(function() { console.log("Waiting..."); }, 1000);
window.localStorage.setItem(url, x);
document.location = url;
console.log("Value Stored");
}
The method is called, however, the key is not stored. The page looks as if it is being refreshed, so I know it is passing that line.
This code is running on a content-script on a chrome-extension.
Upvotes: 0
Views: 113
Reputation: 19835
As per the documentation on content scripts, you cannot use localStorage from there. You can only use chrome.storage or use localStorage from a background page (by sending a message to it from the content script)
Upvotes: 0
Reputation: 17598
Setting location.href doesn't take effect until your script terminates. As such, if you set it multiple times, only the last value you set it to will be navigated to.
More importantly, once the browser leaves a page, any scripts running on that page will not keep running. It would be a massive security hole if you could navigate a browser to another page and execute arbitrary code there.
Upvotes: 1