Google:Error in event handler for runtime.onInstalled: TypeError: Cannot read property 'sync'...

开发谷歌插件出现错误

错误信息:

Error in event handler for runtime.onInstalled: TypeError: Cannot read property 'sync' of undefined

错误原因

根据官网文档在引入后出错

  • manifest.json
{
  "name": "Getting Started Example",
  "version": "1.0",
  "description": "Build an Extension!",
  "background" : { "scripts" : [ "background.js"],"persistent" : false },
  "manifest_version": 2
}

  • background.js
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

'use strict';

chrome.runtime.onInstalled.addListener(function() {
  chrome.storage.sync.set({color: '#3aa757'}, function() {
    console.log("The color is green.");
  });
});

解决办法

接着看文档后面说明了需要在 manifest.json 文件中加入权限注册

Most APIs, including the storage API, must be registered under the “permissions” field in the manifest for the extension to use them.

更改 manifest.json 文件如下解决问题

{
  "name": "Getting Started Example",
  "version": "1.0",
  "permissions": ["storage"],
  "description": "Build an Extension!",
  "background" : { "scripts" : [ "background.js"],"persistent" : false },
  "manifest_version": 2
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值