首页 > 解决方案 > 未捕获的类型错误:尽管具有存储权限,但无法读取未定义错误的属性“本地”

问题描述

与在类似的SO帖子中添加“存储”作为权限解决了问题不同,尽管拥有存储权限,但我还是收到了错误。我不是在 chrome://extension 中收到错误,但是当我打开实时服务器并检查控制台时

背景.js:

chrome.storage.local.set({event: 'test'}, function() {
  console.log('Value is set to ' + 'test');
});

内容.js

chrome.storage.local.get(['event'], function(result) {
  console.log('Value currently is ' + result.key);
});

清单.json:

{
    "manifest_version": 2,
    "name": "To be named",
    "description": "This extension helps...",
    "version": "0.1.0",
    "permissions": [
        "storage",
        "identity",
        "identity.email",
        "http://127.0.0.1:5000/test",
        "http://127.0.0.1:5000/Time"
    ],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },  
    "content_scripts": [{
        "matches": ["https://www.youtube.com/"], 
        "js": ["content.js"],
        "css": ["styles.css"]
    }]
}

错误:

Uncaught TypeError: Cannot read property 'local' of undefined in content.js but not background in the console.

标签: javascriptgoogle-chrome-extensiongoogle-api

解决方案


推荐阅读