【发布时间】:2016-10-10 04:16:20
【问题描述】:
我想使用localStorage 来保存和使用数据。我可以保存和使用数据,但是当我在chrome.tabs.executeScript 中使用localStorage 时,localStorage 总是为空...为什么?
我的代码
chrome.tabs.executeScript({ code:
'document.getElementById(":2k.en").onclick = function (){
alert(localStorage.getItem("Message"));
document.getElementById(":2i").value = localStorage.getItem("Token");
document.getElementById(":2k.jl").textContent = "it is OK";
}'
});
谢谢!
【问题讨论】:
-
我猜你正在使用内容脚本;我的扩展也有同样的问题。解决方案是使用
chrome.storage,讨论见here -
谢谢你的回答,你能给我一些示例代码吗(比如如何在bankgroud.js中设置以及如何进入content_script)?我红了link,我试过了,不行.......
-
有一些有用的例子here。这有帮助吗?
-
no....我想在 popup.js 中设置一个值并在 content_script 中获取值,设置
chrome.storage.sync.set({'Message': json.data},function(){});和获取chrome.storage.sync.get('Message', function(){}),我像他的例子一样写了这个,但是它不起作用... -
我找到了答案:enter link description here
标签: google-chrome-extension google-chrome-app