Jervis2049 / vite-plugin-crx-mv3

Build a Chrome Extension with Vite⚡
201 stars 30 forks source link

注入js文件 #24

Closed Kitesource closed 1 year ago

Kitesource commented 1 year ago

请问项目里如何在content_scripts 注入一个js文件, 放在public目录下?

Jervis2049 commented 1 year ago

参考examples/crx-webAccessibleResources这个例子,你修改一下,新建一个public目录,把injected.ts放进去。

/**
 * injected.ts
 */
const script = document.createElement('script')
script.src = chrome.runtime.getURL('public/injected.ts')
;(document.head || document.documentElement).appendChild(script)

打包会输出:dist/public/injected.js

Kitesource commented 1 year ago

ERROR [vite-plugin-crx-mv3] Could not resolve entry module "src/public/injected.ts".

Jervis2049 commented 1 year ago

ERROR [vite-plugin-crx-mv3] Could not resolve entry module "src/public/injected.ts".

你安装的是哪个版本的vite-plugin-crx-mv3,我本地试了是可以。

Kitesource commented 1 year ago

0.1.0

Kitesource commented 1 year ago

应该是带上了src 导致报错? 打包后是没有src目录的

Jervis2049 commented 1 year ago

应该是带上了src 导致报错? 打包后是没有src目录的

是的

Kitesource commented 1 year ago

这个怎么解决呀,我重新安装到了0.1.3还是会报错

Jervis2049 commented 1 year ago

这个怎么解决呀,我重新安装到了0.1.3还是会报错 你这样写不行吗,路径不要带src

chrome.runtime.getURL('public/injected.ts')
Kitesource commented 1 year ago

我没有加src 还是报这个错误,这是我的demo: https://github.com/Kitesource/chrome-extension-demo

Jervis2049 commented 1 year ago

我没有加src 还是报这个错误,这是我的demo: https://github.com/Kitesource/chrome-extension-demo

我以为你的public是在src目录下...你这个inject.js可以放src下面吗,放到最外层目录去的需求很少见。

Kitesource commented 1 year ago

thanks