如何在 Chrome 扩展程序中执行内容脚本时导入文件?
我想执行一些 javascript,所以我像这样设置了 background.html 页面,
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<script type="text/javascript" src="jquery.js"></script>
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {file: "contentscript.js"});
});
</script>
</body>
</html>
我真的很想在这个脚本中使用 JQuery,但我不知道如何加载它。我不希望内容脚本在每次用户加载某些页面时(只是当他们单击扩展程序的按钮时)关闭。有什么想法吗?
I want to execute some javascript so I have my background.html page set up like this
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<script type="text/javascript" src="jquery.js"></script>
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {file: "contentscript.js"});
});
</script>
</body>
</html>
I would really like to use JQuery in this script, but I can't figure out how to get it to load. I don't want the content script to go off every time the user loads certain pages, just when they click the extension's button. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
chrome.tabs.executeScript
方法有第三个参数,它是加载脚本时执行的回调函数,您可以使用它按顺序加载多个脚本:您不需要包含 < code>进入您的后台页面(如果您不打算在那里使用它)。
chrome.tabs.executeScript
method has third parameter which is a callback function that executes when a script is loaded, which you can use to load more than one script in order:You don't need to include
<script type="text/javascript" src="jquery.js"></script>
into your background page if you don't plan using it there.