我正在写一个chrome扩展程序。我想在我的扩展程序中使用jQuery
。我没有使用任何背景页,只是背景脚本。
以下是我的文件:
manifest.json
{
"manifest_version": 2,
"name": "Extension name",
"description": "This extension does something,",
"version": "0.1",
"permissions": [
"activeTab"
],
"browser_action": {
"default_icon": "images/icon_128.png"
},
"background": {
"scripts": ["background.js"],
"persistent": false
},
"icons": {
"16": "images/icon_16.png",
"48": "images/icon_48.png",
"128": "images/icon_128.png"
}
}
我的background.js
文件只运行另一个名为work.js
// Respond to the click on extension Icon
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.executeScript({
file: 'work.js'
});
});
我的扩展程序的主要逻辑是work.js
。我认为这个问题的内容并不重要。
我想问的是如何在扩展程序中使用jQuery。因为我没有使用任何背景页面。我不能只是添加jQuery。那么如何在我的扩展中添加和使用jQuery呢?
我尝试在background.js
文件中运行jQuery以及我的work.js。
// Respond to the click on extension Icon
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.executeScript({
file: 'thirdParty/jquery-2.0.3.js'
});
chrome.tabs.executeScript({
file: 'work.js'
});
});
它工作正常,但我担心以这种方式添加的脚本是否异步执行。如果是,则可能会发生work.js在 jQuery(或我将来可能添加的其他库)之前运行。
我还想知道在Chrome扩展程序中使用第三方库的正确和最佳方式是什么。
答案 0 :(得分:106)
您必须将jquery脚本添加到chrome-extension项目以及manifest.json的background
部分,如下所示:
"background":
{
"scripts": ["thirdParty/jquery-2.0.3.js", "background.js"]
}
如果你需要在content_scripts中使用jquery,你也必须在清单中添加它:
"content_scripts":
[
{
"matches":["http://website*"],
"js":["thirdParty/jquery.1.10.2.min.js", "script.js"],
"css": ["css/style.css"],
"run_at": "document_end"
}
]
这就是我所做的。
另外,如果我没记错的话,后台脚本会在后台窗口中执行,你可以通过chrome://extensions
打开。
答案 1 :(得分:14)
只需执行以下操作即可:
在mainfest.json中添加以下行
"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'",
现在您可以直接从url
加载jquery <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
来源:google doc
答案 2 :(得分:11)
它工作正常,但我担心以这种方式添加的脚本是否异步执行。如果是,则可能发生work.js甚至在jQuery(或我将来可能添加的其他库)之前运行。
这不应该成为一个问题:您将脚本排队在某个JS上下文中执行,并且该上下文不具备竞争条件,因为它是单线程的。< / p>
但是,消除这种担忧的正确方法是将呼叫链接起来:
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.executeScript({
file: 'thirdParty/jquery-2.0.3.js'
}, function() {
// Guaranteed to execute only after the previous script returns
chrome.tabs.executeScript({
file: 'work.js'
});
});
});
或者,概括:
function injectScripts(scripts, callback) {
if(scripts.length) {
var script = scripts.shift();
chrome.tabs.executeScript({file: script}, function() {
if(chrome.runtime.lastError && typeof callback === "function") {
callback(false); // Injection failed
}
injectScripts(scripts, callback);
});
} else {
if(typeof callback === "function") {
callback(true);
}
}
}
injectScripts(["thirdParty/jquery-2.0.3.js", "work.js"], doSomethingElse);
或者,宣传(并使其更符合正确的签名):
function injectScript(tabId, injectDetails) {
return new Promise((resolve, reject) => {
chrome.tabs.executeScript(tabId, injectDetails, (data) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError.message);
} else {
resolve(data);
}
});
});
}
injectScript(null, {file: "thirdParty/jquery-2.0.3.js"}).then(
() => injectScript(null, {file: "work.js"})
).then(
() => doSomethingElse
).catch(
(error) => console.error(error)
);
或者,为什么不能,async
/ await
- 更清晰的语法:
function injectScript(tabId, injectDetails) {
return new Promise((resolve, reject) => {
chrome.tabs.executeScript(tabId, injectDetails, (data) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError.message);
} else {
resolve(data);
}
});
});
}
try {
await injectScript(null, {file: "thirdParty/jquery-2.0.3.js"});
await injectScript(null, {file: "work.js"});
doSomethingElse();
} catch (err) {
console.error(err);
}
请注意,在Firefox中,您只需使用browser.tabs.executeScript
,因为它将返回Promise。
答案 3 :(得分:6)
除了已经提到的解决方案,您还可以在本地下载jquery.min.js
然后再使用它 -
下载 -
wget "https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"
manifest.json -
"content_scripts": [
{
"js": ["/path/to/jquery.min.js", ...]
}
],
在html中 -
<script src="/path/to/jquery.min.js"></script>
参考 - https://developer.chrome.com/extensions/contentSecurityPolicy
答案 4 :(得分:1)
就我而言,通过跨文档消息传递(XDM)得到了可行的解决方案 并执行Chrome扩展程序onclick而不是页面加载。
manifest.json
{
"name": "JQuery Light",
"version": "1",
"manifest_version": 2,
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": [
"https://*.google.com/*"
],
"js": [
"jquery-3.3.1.min.js",
"myscript.js"
]
}
],
"background": {
"scripts": [
"background.js"
]
}
}
background.js
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, {"message": "clicked_browser_action"});
});
});
myscript.js
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.message === "clicked_browser_action") {
console.log('Hello world!')
}
}
);