HTML5本地存储,如何传递变量?
好吧,这是我的问题。我正在写一个Chrome扩展程序,快完成了,但问题是,例如:
[Page popup.html]
localStorage["code"] = "alert('Hello!');";
[Fileinjection.js;每次加载页面时都会调用它,我认为是内容脚本]
localStorage["code"] = (localStorage["code"] != undefined) ? localStorage["code"] : "alert('Default!');";
eval(localStorage["code"]);
我尝试了两种方法,第一件事是不要打开弹出窗口,这样我加载的每个页面都会运行命令alert("Default!");它有效。然后我尝试先加载弹出窗口,然后我意识到变量 localStorage["code"]
现在取决于我加载的页面,而不是我的扩展。快速解释一下,在我的扩展中, localStorage["code"] == "alert('Hello');";
但在 http://google.com localStorage["code"] == "";
或在 http://facebook.com localStorage["code"] == "";
。
我的问题是,是否有办法从 popup.html 存储注入.js 可以访问的 localStorage["code"] ?
Ok, here is my problem. I am writing a Chrome extension, it's almost done, but the problem is, for example:
[Page popup.html]
localStorage["code"] = "alert('Hello!');";
[File injection.js; which will be called everytime a page is loaded, content script I think]
localStorage["code"] = (localStorage["code"] != undefined) ? localStorage["code"] : "alert('Default!');";
eval(localStorage["code"]);
I have tried 2 methods, the first thing is to NOT OPEN THE POP UP, so that every page I load would run command alert("Default!"); and it works. Then I tried to load the popup first, and I realize that the variable localStorage["code"]
is now up to the page I load, not to my extenstion anymore. In a quick explanation, in my extension, localStorage["code"] == "alert('Hello');";
but in http://google.com localStorage["code"] == "";
or in http://facebook.com localStorage["code"] == "";
.
My question is, is there anyway to store localStorage["code"] from popup.html that injection.js can access?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
三种是两种本地存储 - 一种属于扩展本身(只能从后台页面、弹出页面或捆绑到扩展包中的任何页面访问),另一种属于站点(只能从内容脚本访问) 。
因此,如果您需要从内容脚本访问在弹出窗口中设置的本地存储值,则需要向后台页面发送请求,读取那里的值,然后将其发送回内容脚本。 消息传递文档中介绍了如何执行此操作。
Three are 2 kinds of local storages - one belongs to extension itself (can be accessed only from a background page, popup page or any page bundled into extension package), another one belongs to the site (can be accessed only from a content script).
So if you need to access local storage value that was set in a popup from a content script, you need to send a request to a background page, read the value there, and send it back to a content script. How to do that is explained in Message Passing doc.
检查一下:
内容脚本:
背景页面:
Check this out :
Content-Script :
Background Page :