问了 gpt ,使用了以下两个方法都没用:
- 动态加载 script 标签:
function loadScript(url) {
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
document.head.appendChild(script);
}
// 调用函数加载其他 JavaScript 文件
loadScript(chrome.runtime.getURL('path/to/your/other-script.js'));
- 在 manifest.json 中配置 module, 然后使用 export import:
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"content.js",
"myfunction.js"
],
"type": "module"
}
],
myfunction.js:
function hello() {
console.log("Hello World")
}
content.js:
hello()