gpt4 book ai didi

javascript - 如何在 Chrome 扩展中使用另一个 javascript 文件

转载 作者:行者123 更新时间:2023-11-29 18:30:11 25 4
gpt4 key购买 nike

如何在contentscript.js中使用javascript文件的功能?我在 a1.js 中有一些函数,我想知道如何在 contentscript.js 中调用它们

a1.js 也是扩展的一部分,与 contentscript.js 在同一个文件夹中

最佳答案

将它们放入您的 manifest.json 文件中:

  ....
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["a1.js", "contentscript.js"]
}
],
....

a1.js 将首先加载,然后是 contentscript.js

例子:

// a1.js
function x() { return 100; }
alert(typeof y); // undefined, because `contentscript.js` is not loaded yet
setTimeout( function(){alert(typeof y;)}, 1000 ); // function

// contentscript.js
function y() {
alert(x()); // Shows 100
}

关于javascript - 如何在 Chrome 扩展中使用另一个 javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9250185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com