简体   繁体   English

将内容脚本注入 Chrome 选项卡时出现问题(Manifest V3)

[英]Trouble with Injecting Content Scripts into Chrome Tab (Manifest V3)

I'm building a Chrome Extension that needs to detect DOM events (click, mouseover, etc.) on any tab the user switches to or opens during a recording session. The way that seems most appropriate to do this is using the scripting API to inject a script that set's the appropriate event listeners and can post messages back to the chrome.runtime API to collect data.我正在构建一个 Chrome 扩展,它需要检测用户在录制过程中切换到或打开的任何选项卡上的 DOM 事件(单击、鼠标悬停等)session。最合适的方法似乎是使用scripting API 来注入设置适当事件监听器的脚本,并可以将消息发回chrome.runtime API 以收集数据。

However, I'm unable to inject the content Script successfully.但是,我无法成功注入内容脚本。 My project is a Vue3 + Vite + Manifest V3 Chrome Extension, and in my background.js on the appropriate chrome.tabs listeners I'm executing:我的项目是一个 Vue3 + Vite + Manifest V3 Chrome 扩展,在我的background.js中,我正在执行相应的chrome.tabs监听器:

  chrome.scripting.executeScript({
    target: { tabId },
    files: ['src/content_scripts/events.js']
  });

The documentation clearly states that a relative path from the directory root is required.文档明确指出需要从目录根目录开始的相对路径。 However, no matter how I structure that path, it says the file isn't found.但是,无论我如何构建该路径,它都表示找不到该文件。

The exact error I'm receiving is:我收到的确切错误是:

Uncaught (in promise) Error: Could not load file: 'src/content_scripts/events.js'.

Also, my manifest.js file is:另外,我的manifest.js文件是:

{
  "manifest_version": 3,
  "name": "walkthrough.ai capturer",
  "version": "1.0.0",
  "icons": {
    "16": "icons/icon16.png",
    "32": "icons/icon32.png",
    "48": "icons/icon48.png",
    "128": "icons/icon128x128.png"
  },
  "permissions": [
    "desktopCapture",
    "tabCapture",
    "scripting",
    "activeTab",
    "tabs"
  ],
  "host_permissions": ["https://*/*", "http://*/*"],
  "action": {
    "default_icon": {
      "16": "icons/icon16.png",
      "32": "icons/icon32.png"
    },
    "default_title": "Open Options"
  },
  "background": {
    "service_worker": "src/background/index.js",
    "type": "module"
  }
}

Any ideas on what could be happening?关于可能发生的事情有什么想法吗?

Thank you谢谢

How about using chrome.runtime.getURL('where content script is from you are calling this API') instead of moving the content script file to public folder?如何使用chrome.runtime.getURL('where content script is from you are calling this API')而不是将内容脚本文件移动到公共文件夹?

That way I get my file path correctly after building the project.这样我就可以在构建项目后正确获取文件路径。

After doing some more digging on @wOxxOm 's suggestion, I was able to figure it out.在对@wOxxOm 的建议进行了更多挖掘之后,我能够弄明白。

The content script wasn't showing up in the build folder, which is where the scripting API was looking for it to be a relative import from.内容脚本没有出现在构建文件夹中,脚本 API 正在寻找它作为相对导入的位置。

In order to solve the issue, I put the content script in my /public folder so that it gets placed in the /dist folder when the project is built.为了解决这个问题,我将内容脚本放在我的/public文件夹中,以便在构建项目时将它放在/dist文件夹中。

This worked for now.这暂时有效。 Thank you!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM