带有 chrome 扩展的 cookie 或 localStorage
我已阅读此处有关该主题的所有其他问题,但无法解决我的问题。
我在我的网站上设置 localStorage 中用户的电子邮件,我想在扩展中检索它。
localStorage.setItem("user", "[email protected]" );
但是当我尝试使用 chrome 扩展程序接收它时,它失败了
value = localStorage.getItem("user");
哪种方式更容易? cookie 本地存储 ?我不自命不凡
I've read all the other q's here regarding the topic but couldn't solve my problem.
I'm setting on my website the email of the user in the localStorage and i want to retrieve it in the extension.
localStorage.setItem("user", "[email protected]" );
But when i try to receive it with the chrome extension it fails to do so
value = localStorage.getItem("user");
Which way is easier ? cookies localstorage ? im not pretentious
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当 localstorage 可以使用 cookie 时,您不想使用 cookie。这是因为
You do not want to use cookies when localstorage can do. That is because
请参阅有关 Chrome 内容脚本的信息。我敢打赌您陷入了与我相同的初始陷阱 - 尝试从页面注入脚本读取 localStorage,是吗?
Please see the information on Chrome content scripts. I'm betting you fell into the same initial trap that I did -- trying to read localStorage from an page-injected script, yes?
使用 chrome.storage.local 而不是 localstorage。使用 chrome.storage 的内容脚本看到的内容与扩展页面看到的内容相同。更多信息请访问 https://developer.chrome.com/extensions/storage.html
Use
chrome.storage.local
instead oflocalstorage
. Content scripts usingchrome.storage
see the same thing that the extension page sees. More at https://developer.chrome.com/extensions/storage.html请参阅:
http://code.google.com/chrome /extensions/content_scripts.html#host-page-communication
内容脚本在单独的 JavaScript 世界中运行,这意味着内容脚本的
localStorage
与网站的localStorage
不同代码>.两者唯一共享的是 DOM,因此您需要使用 DOM 节点/事件来进行通信。Please see this:
http://code.google.com/chrome/extensions/content_scripts.html#host-page-communication
Content scripts are run in a separate JavaScript world, which means the content script's
localStorage
is different from the website'slocalStorage
. The only thing the two share is the DOM, so you'll need to use DOM nodes/events to communicate.