chrome.storage.sync返回为undefined

ffdz8vbo  于 2023-08-01  发布在  Go
关注(0)|答案(1)|浏览(108)

你好,
我最近收到了一个错误,在编码我的Chrome扩展程序时出现了Uncaught TypeError: Cannot read properties of undefined (reading 'sync')。虽然我已经阅读并修改了我的文档足够的同时做了深入的研究,以找到这个问题的根源,我仍然遇到了一个大错误。
我正试图使用存储来存储一个布尔值,该布尔值将确定按钮是否显示。
我试图找到我得到的许多错误的来源,我发现chrome.storage.sync是唯一一个我不能自己修复的错误。
快速提示:如果你用纯JavaScript回复,请告诉我在哪里放置它,因为这可能也是关闭的。我从一个HTML页面执行此脚本,不希望它是在一个后台脚本(如果可能的话)。
我的代码是这样的:
manifest.json

"permissions": [
    "storage"
  ],
  "background": {
    "service_worker": "background.js"
  },
  "action": {
    "default_icon": "images/icon-48.png",
    "default_popup": "popup.html"
  },
  "content_scripts": [
    {
      "matches": ["###"],
      "js": ["inject.js"]
    }
  ],
  "sandbox": {
    "pages": ["popup.html"]
  }

字符串
popup.html这是我得到错误的页面

<body>
   <script>
        chrome.storage.sync.set({ "enabled": true }, function() {
            alert('set');
        });
   </script>
</body>


Error I am receiving:(如果需要)

Uncaught TypeError: Cannot read properties of undefined (reading 'local')
Context
popup.html
Stack Trace
popup.html:88 (anonymous function)

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
chrome.storage.local.set({"enabled": true}, function() {     <<<<<------ line with error
            alert('set');
        });
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

zbsbpyhn

zbsbpyhn1#

请试试这个。
manifest.json

{
  "name": "hoge",
  "manifest_version": 3,
  "version": "1.0",
  "permissions": [
    "storage"
  ],
  "action": {
    "default_popup": "popup.html"
  }
}

字符串
popup.html

<html>
<body>
  <script src="popup.js"></script>
</body>
</html>


popup.js

chrome.storage.sync.set({ "enabled": true }, function() {
  alert('set');
});

相关问题