HTML5本地存储,如何传递变量?

发布于 2024-10-30 20:06:26 字数 871 浏览 0 评论 0原文

好吧,这是我的问题。我正在写一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别低头,皇冠会掉 2024-11-06 20:06:26

三种是两种本地存储 - 一种属于扩展本身(只能从后台页面、弹出页面或捆绑到扩展包中的任何页面访问),另一种属于站点(只能从内容脚本访问) 。

因此,如果您需要从内容脚本访问在弹出窗口中设置的本地存储值,则需要向后台页面发送请求,读取那里的值,然后将其发送回内容脚本。 消息传递文档中介绍了如何执行此操作。

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.

不甘平庸 2024-11-06 20:06:26

检查一下:

内容脚本:

chrome.extension.sendRequest({ action : "getCode" }, function(response) {
    alert(response.getCode);
}); 

背景页面:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 
{   
    case "getCode":
        sendResponse({ getCode: localStorage['code'] });
    break;
}

Check this out :

Content-Script :

chrome.extension.sendRequest({ action : "getCode" }, function(response) {
    alert(response.getCode);
}); 

Background Page :

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 
{   
    case "getCode":
        sendResponse({ getCode: localStorage['code'] });
    break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文