localStorage 在 chrome.tabs.executeScript 中不起作用?
localStorage doesn't work in chrome.tabs.executeScript?
我想用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";
}'
});
谢谢!
@Noam Hacker,你是对的。需要使用 Chrome.storage
API to store, retreive and track changes to user data. By using the said API you need to declare the storage
permission in the extension manifest 才能使用存储空间 API.
{
"name": "My extension",
...
"permissions": [
"storage"
],
...
}
使用 get
StorageArea.get(string or array of string or object keys, function callback)
它的回调从存储中获取一个或多个项目。空列表或对象将 return 空结果对象。传入 null 以获取存储的全部内容。
background.js
此脚本将内容设置为 chrome 存储
//Set some content from background page
chrome.storage.local.set({"identifier":"Some awesome Content"},function (){
console.log("Storage Succesful");
});
//get all contents of chrome storage
chrome.storage.local.get(null,function (obj){
console.log(JSON.stringify(obj));
});
popup.js
此脚本检索并设置内容 from\to chrome 存储
document.addEventListener("DOMContentLoaded",function (){
//Fetch all contents
chrome.storage.local.get(null,function (obj){
console.log(JSON.stringify(obj));
});
//Set some content from browser action
chrome.storage.local.set({"anotherIdentifier":"Another awesome Content"},function (){
console.log("Storage Succesful");
});
});
我想用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";
}'
});
谢谢!
@Noam Hacker,你是对的。需要使用 Chrome.storage
API to store, retreive and track changes to user data. By using the said API you need to declare the storage
permission in the extension manifest 才能使用存储空间 API.
{
"name": "My extension",
...
"permissions": [
"storage"
],
...
}
使用 get
StorageArea.get(string or array of string or object keys, function callback)
它的回调从存储中获取一个或多个项目。空列表或对象将 return 空结果对象。传入 null 以获取存储的全部内容。
background.js
此脚本将内容设置为 chrome 存储
//Set some content from background page
chrome.storage.local.set({"identifier":"Some awesome Content"},function (){
console.log("Storage Succesful");
});
//get all contents of chrome storage
chrome.storage.local.get(null,function (obj){
console.log(JSON.stringify(obj));
});
popup.js
此脚本检索并设置内容 from\to chrome 存储
document.addEventListener("DOMContentLoaded",function (){
//Fetch all contents
chrome.storage.local.get(null,function (obj){
console.log(JSON.stringify(obj));
});
//Set some content from browser action
chrome.storage.local.set({"anotherIdentifier":"Another awesome Content"},function (){
console.log("Storage Succesful");
});
});