You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展报错:Cannot read properties of undefined (reading 'executeScript')求助

问题解决指南

核心错误原因

你遇到的Cannot read properties of undefined (reading 'executeScript')报错,本质是Chrome Manifest V3已移除chrome.tabs.executeScript API,该API仅在Manifest V2中有效,V3里必须使用chrome.scripting.executeScript替代。

具体修复步骤

1. 替换Background.js中的执行脚本API

将原代码中的旧API替换为V3规范的新API,同时必须指定目标标签,修改后代码如下:

// Background.js
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
  const activeTabId = tabs[0].id;
  chrome.scripting.executeScript({
    target: {tabId: activeTabId},
    func: () => window.getSelection().toString()
  }, (results) => {
    const selectedText = results[0].result;
    const resultSentence = removeStopWords(selectedText);
    // Service Worker无法直接操作Popup的DOM,需通过消息传递给Popup
    chrome.runtime.sendMessage({type: "UPDATE_POPUP", text: resultSentence});
  });
});

2. 修正Manifest.json的语法错误

你的Manifest存在两处JSON语法问题:

  • name字段末尾缺少逗号
  • action对象内最后一个属性后多了冗余逗号

修正后的Manifest:

{
  "manifest_version": 3,
  "name": "你的扩展名称",
  "version": "1.0",
  "description": "你的扩展描述",
  "permissions": [
    "activeTab",
    "scripting",
    "tabs",
    "storage"
  ],
  "action": {
    "default_popup": "index.html"
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content.js"]
    }
  ],
  "background": {
    "service_worker": "background.js"
  }
}

3. 解决Service Worker无法操作DOM的问题

Background的Service Worker没有DOM环境,不能直接调用document.getElementById修改Popup内容。需在Popup对应的JS文件(比如index.js)中监听消息并更新DOM:

// index.js(Popup关联的脚本)
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.type === "UPDATE_POPUP") {
    document.getElementById("new-task").innerHTML = message.text;
  }
});

4. 确保removeStopWords函数可用

如果removeStopWords定义在Background中,需确保函数能被正常访问;如果是定义在Content Script里,需调整逻辑或通过消息传递完成文本处理。

内容的提问来源于stack exchange,提问作者Me4836

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 11:01:02