且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Chrome扩展程序:在内容脚本中访问localStorage

更新时间:2023-12-05 20:23:16

更新2016:



Google Chrome发布了存储API: http://developer.chrome.com/extensions/storage.html



与其他Chrome API一样使用起来非常容易,您可以在Chrome中的任何页面环境中使用它。

  //使用Chrome扩展程序存储API保存它。 
chrome.storage.sync.set({'foo':'hello','bar':'hi'},function(){
console.log('Settings saved');
});

//使用存储API读取它
chrome.storage.sync.get(['foo','bar'],function(items){
message('检索的设置',项目);
});

要使用它,请确保在清单中定义它:

 permissions:[
storage
],

有一些方法可以删除,清除,getBytesInUse和一个事件侦听器来侦听已更改的存储onChanged



使用本地localStorage(来自2011年的旧回复)



内容脚本运行在网页上下文中,而不是扩展页上。因此,如果您从您的内容中访问localStorage,它将成为该网页的存储空间,而不是扩展页面存储。