admin管理员组文章数量:1394187
My extension has a content script that captures some info from certain pages and saves them into local storage using the following:
localStorage[myVarName] = value;
If I put the following into my content script, I have no problem retrieving the stored values:
myGottenVariable = localStorage[myVarName];
Elsewhere, my extension creates a new window using
chrome.windows.create({url:LocalURL}}
The newly created window is the main UI for my extension. One of the things I want it to do is retrieve the stored value at "myVarName." However, it is undefined.
I assume that this has to do with how the localstorage is organized. I have an intuition that each extension has its own storage space for local storage and that the created window lacks access to the content script's storage space.
This intuition is confirmed by the fact that I have tried storing content locally from the newly created window, and it has no problem retrieving that content.
My question is, what is the simplest method of achieving my desired result? I get tripped up pretty quickly when dealing with a lot of the chrome extension API stuff: so the ideal option would be as clean and simple as possible. That's why I liked the syntax of:
localStorage[myVarName] = value;
Is there some simple way of modifying this to allow easy access to the desired content? Are both pages actually in the same storage space, but at different places in that directory?
My extension has a content script that captures some info from certain pages and saves them into local storage using the following:
localStorage[myVarName] = value;
If I put the following into my content script, I have no problem retrieving the stored values:
myGottenVariable = localStorage[myVarName];
Elsewhere, my extension creates a new window using
chrome.windows.create({url:LocalURL}}
The newly created window is the main UI for my extension. One of the things I want it to do is retrieve the stored value at "myVarName." However, it is undefined.
I assume that this has to do with how the localstorage is organized. I have an intuition that each extension has its own storage space for local storage and that the created window lacks access to the content script's storage space.
This intuition is confirmed by the fact that I have tried storing content locally from the newly created window, and it has no problem retrieving that content.
My question is, what is the simplest method of achieving my desired result? I get tripped up pretty quickly when dealing with a lot of the chrome extension API stuff: so the ideal option would be as clean and simple as possible. That's why I liked the syntax of:
localStorage[myVarName] = value;
Is there some simple way of modifying this to allow easy access to the desired content? Are both pages actually in the same storage space, but at different places in that directory?
Share Improve this question asked Jul 13, 2016 at 12:24 COMisHARDCOMisHARD 9233 gold badges15 silver badges37 bronze badges 1- The local storage is unique per domain. In Chrome or Firefox you can lookup the content of the local storage using the developer tools (F12, Resource Tab). I don't know how these extensions work, but you can try the sessionStorage instead to be independent from domains. – Eytibi Commented Jul 13, 2016 at 12:32
1 Answer
Reset to default 6localStorage
is isolated per domain, which means if the new window has different domain from the domain where you saved data, you won't get what you saved.
To achieve what you want, you could use chrome.storage
. The chrome.storage
is shared by background page/content scripts. For your case, you could save data in content scripts and retrieve it in the created window (via content scripts).
chrome.storage.local.set({ key: value });
chrome.storage.local.get(key, function (result) {
console.log(result.key);
});
本文标签: javascriptChrome Extension Local Storage Accessing From Multiple PlacesStack Overflow
版权声明:本文标题:javascript - Chrome Extension Local Storage Accessing From Multiple Places - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744664150a2618427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论