Uncaught TypeError:无法读取chrome扩展中未定义的属性'local' [英] Uncaught TypeError: Cannot read property 'local' of undefined in chrome extension

查看:544
本文介绍了Uncaught TypeError:无法读取chrome扩展中未定义的属性'local'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Chrome扩展。我无法使用
localStorage.setItem localStorage.getItem 来存储和检索,因为后台和浏览器操作运行在不同的环境中[如此处所示]。

I have written a Chrome extension. I cannot use localStorage.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:


未捕获TypeError:无法读取未定义的属性'local'

Uncaught TypeError: Cannot read property 'local' of undefined

我做错了什么?

推荐答案

确保所有必要的权限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:


  1. 阅读您正在使用的API的文档,并熟悉先决条件,通常是清单权限( eg chrome.storage #manifest ) 。 通过查看新增功能

  2. 检查脚本是否正确运行上下文。大多数API仅适用于扩展程序的过程。 ( chrome.storage API也可以在内容脚本中使用)

  3. 否则,请使用常用的调试技巧:错别字,变量阴影......

  1. Read the documentation of the API you're using and get yourself familiar with the prerequisites, usually manifest permissions (e.g. chrome.storage#manifest).
  2. Check if your (user's) Chrome version supports the API, by looking at What's new.
  3. 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)
  4. Otherwise, resort to your usual debugging skills: Typos, variable shadowing, ...

这篇关于Uncaught TypeError:无法读取chrome扩展中未定义的属性'local'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆