简体   繁体   English

通过Chrome扩展程序编程中的内容脚本访问本地存储

[英]Access local storage from content script in Chrome extension programing

I'm trying to develop a Chrome extension. 我正在尝试开发Chrome扩展程序。 In it I have one option page and a contentscript.js both registered in manifest.js . 在其中,我有一个选项页和一个contentscript.js都注册在manifest.js

I have used local storage in option.js . 我在option.js使用了本地存储。 Now I want to access data of local storage from contentscript.js so I found following code to send request to background.js from contentscript.js : 现在我想从contentscript.js访问本地存储的数据,所以我找到了以下代码以将请求从contentscript.js发送到background.js

chrome.extension.sendRequest({method: "getStatus"}, function(response) {
  console.log(response.status);
});

And background.js using following to respond: 然后background.js使用以下响应:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "getStatus")
       sendResponse({status: localStorage['status']});
else
       sendResponse({}); });

Now it is showing following error: 现在显示以下错误:

undefined in line "console.log(response.status);" 在“ console.log(response.status)”行中未定义; in file contentscript.js . 在contentscript.js文件中。

What is the problem with that? 这是什么问题? How can I get whole local storage access from contentscript.js ? 如何从contentscript.js获取整个本地存储访问权限?

It looks like working code. 看起来像工作代码。 Check your manifest file and localStorage.status 检查清单文件和localStorage.status

http://code.google.com/chrome/extensions/messaging.html http://code.google.com/chrome/extensions/content_scripts.html http://code.google.com/chrome/extensions/messaging.html http://code.google.com/chrome/extensions/content_scripts.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从内容脚本访问 chrome 扩展本地存储数据? - How to access chrome extension local storage data from content scripts? 如何从Chrome扩展程序访问本地存储? - How to access local storage from chrome extension? 获取本地存储值到内容脚本[Chrome扩展程序]的简便方法 - Easy way to get Local Storage Value to Content Script [Chrome Extension] 每次页面加载时自动将本地存储数据从chrome扩展名传递到内容脚本 - Passing local storage data from chrome extension to content script automatically on every page load 我可以在Chrome扩展程序内容脚本中使用和访问Chrome存储API吗? - Can I use and access the Chrome storage API within a chrome extension content script? Chrome扩展程序和本地存储 - Chrome extension and local storage Chrome扩展程序使内容脚本能够访问iframe - Chrome extension enable content script to access iframe chrome扩展:从本地存储切换到chrome.storage.local - chrome extension: switching from localstorage to chrome.storage.local 将 Chrome 扩展数据从 Chrome 本地存储迁移到 MongoDB - Migrating Chrome Extension Data from Chrome Local Storage to MongoDB 内容脚本中的Chrome扩展程序storage.sync.set会引发异常 - Chrome extension storage.sync.set in content script throws exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM