【Web】插件开发之Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.

Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist. 报错,说明 popup.js 通过 chrome.tabs.sendMessage 发送消息时,目标页面并没有注入 content script(即 content.js 未在该 tab 上运行)。

常见原因及排查建议:

  1. manifest.json 未配置 content_scripts

    • 检查 manifest.json 是否有如下内容:
      "content_scripts": [
        {
          "matches": ["https://chat.openai.com/*", "https://chatgpt.com/*"],
          "js": ["content.js"]
        }
      ]
      
    • 若无此配置,content.js 不会自动注入,导致消息无接收方。
  2. 目标页面未刷新

    • 安装/更新扩展后,需刷新目标网页,确保 content script 注入。
  3. 发送消息的 tabId 不正确

    • popup.js 需确保发送消息到当前激活的 tab,且该 tab 的 url 匹配 content_scripts 的 matches。
  4. 动态注入脚本未成功

    • 若用 chrome.scripting.executeScript 动态注入,需确保注入成功且 content.js 已执行。
  5. 页面为特殊 iframe 或未匹配 host_permissions

    • 某些 iframe 或未在 host_permissions 范围内的页面不会注入 content script。

建议操作:

  • 检查 manifest.json 是否正确配置 content_scripts 和 host_permissions。
  • 刷新目标网页后再点击 popup。(这点很重要)
  • 在 content.js 顶部加日志,确认脚本已注入。
  • 在 popup.js 发送消息前后加日志,确认 tabId 和 url。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ganshenml

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值