问题描述
我写了一个Chrome扩展。我无法使用
localStorage.setItem
和 localStorage.getItem
来存储和检索,因为后台和浏览器操作运行在不同的环境中[]。
I have written a Chrome extension. I cannot uselocalStorage.setItem
and localStorage.getItem
for storing and retrieving because background and browser action runs in different environment [as seen here].
因此,我决定使用Chrome存储API:
So I decided to use the Chrome storage API:
var storage = chrome.storage.local;
var myTestVar = 'somevar';
var obj = {};
obj[myTestVar] = $("#somevar").val();
storage.set(obj);
其中产生以下错误:
which produced the following error:
我做错了什么?
推荐答案
确保所有必要的。 storage
,就你的情况而言。
Make sure that all necessary permissions have been declared in the manifest file. "storage"
, in your case.
一般来说,以下步骤应该可以解决Chrome显然未定义的问题API:
In general, the following steps should fix the problem of apparently undefined Chrome APIs:
- 阅读您正在使用的API的文档,并熟悉先决条件,通常是清单权限() 。 通过查看。
- 检查脚本是否正确运行。大多数API仅适用于扩展程序的过程。 (
chrome.storage
API也可以在内容脚本中使用) - 否则,请使用常用的调试技巧:错别字,变量阴影......
- Read the documentation of the API you're using and get yourself familiar with the prerequisites, usually manifest permissions (e.g.
chrome.storage
#manifest). - Check if your (user's) Chrome version supports the API, by looking at What's new.
- Check if the script is running in the right context. Most APIs are only available to the extension's process. (the
chrome.storage
API can also be used in content script though) - Otherwise, resort to your usual debugging skills: Typos, variable shadowing, ...
这篇关于Uncaught TypeError:无法读取chrome扩展中未定义的属性'local'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!