【发布时间】:2016-03-04 17:49:21
【问题描述】:
由于某种原因,jQuery 在我的 Google Chrome 扩展程序的 background.js 中不起作用。
假设我的插件告诉我页面上的所有图像。我验证它可以到达下面的方法,但它在 jQuery 循环处停止。
首先,manifest.json:注意我包括 jQuery,文件存在:
{
"name": "Gallery",
"version": "0.0.1",
"manifest_version": 2,
"description": "Gallery",
"browser_action": {
"default_icon": "G.png"
},
"background": {
"persistent": true,
"scripts": ["jquery-1.11.2.min.js", "background.js"]
},
"permissions": [
"tabs", "contextMenus", "http://*/*", "https://*/*"
],
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"css": ["contentstyle.css"],
"js": ["jquery-1.11.2.min.js", "contentscript.js"]
}
],
"icons": {
"32": "G.png"
}
}
background.js 使用 jQuery 语法:
chrome.browserAction.onClicked.addListener(scanImages);
function scanImages()
{
// IT GETS HERE - THIS IS OK
alert('Clicked plugin button, about to start looping thru IMG...');
// BUT STOPS HERE: JQUERY DOESN'T EXECUTE
$("img").each(function() {
alert($(this).prop("src"));
});
// ANOTHER JQUERY THAT DOESN'T WORK
alert('Page title = ' + $(document).find("title").text());
}
【问题讨论】:
-
所以我不能在 background.js 中使用 jQuery?仅在弹出窗口中?
-
带有代码的示例:JQuery and Chrome Extension 更多示例只需 google
stackoverflow chrome extension access web page from popup or background page -
好的,我明白了。需要进入内容脚本。谢谢
标签: google-chrome google-chrome-extension