从内容脚本向后台发送消息

时间:2021-07-17 07:33:39

标签: javascript google-chrome-extension

我必须从内容脚本向后台发送消息,我总是收到错误 Uncaught TypeError: Cannot read property 'sendMessage' of undefined

不需要后台脚本,因为我在内容脚本中有错误

我检查了 chrome.extension.sendMessage 和同样的错误

内容脚本:

  try {
    chrome.storage.sync.set({
      "channelId": cid
    });
  } catch (error) {
    if (toReload(error.toString())) {

      chrome.runtime.sendMessage({ message: "errorreset" }, function (response) {
          console.log(123);
        });
        
    }
  }

清单

"background": {
    "page": "notifications.html",
    "persistent": true
  },
  "content_scripts": [
    {
      "matches": ["*://*.youtube.com/*"],
      "js": [
        "lib/jquery-3.6.0.min.js",
        "init.js"
      ],
      "run_at": "document_idle",
      "all_frames": true
    }
  ],

0 个答案:

没有答案