Google chrome extension 为什么不是';我的chrome扩展不工作吗?
我花了好几个小时想弄清楚我的chrome扩展有什么问题。我知道我的function.js代码很好,因为它可以与我链接到的html页面一起工作,但也许它不适合chrome扩展? 谢谢你的帮助Google chrome extension 为什么不是';我的chrome扩展不工作吗?,google-chrome-extension,Google Chrome Extension,我花了好几个小时想弄清楚我的chrome扩展有什么问题。我知道我的function.js代码很好,因为它可以与我链接到的html页面一起工作,但也许它不适合chrome扩展? 谢谢你的帮助 manifest.json { "manifest_version": 2, "name": "Font Find", "description": "This extension allows you to click on any text, and find out
manifest.json
{
"manifest_version": 2,
"name": "Font Find",
"description": "This extension allows you to click on any text, and find out what font it is written in.",
"version": "1.0",
"background": {
"page": "popup.html",
"persistent": true
},
"permissions": [
"activeTab",
"http://*/*",
"https://*/*"
],
"browser_action": {
"default_title": "Find the Font",
"default_icon": "icon.png"//,
// "default_popup": "popup.html"
},
"background": { "scripts": ["jquery.min.js"] },
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": [
"function.js"
]
}
]
}
popup.html
文件(注意它没有设置为弹出窗口)
function.js
文件
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.executeScript({
file: "function.js"
});
});
首先,编辑
manifest.json
文件。添加此代码:
"content_scripts": [{
"matches": [
"http://*/*",
"https://*/*"
], "js": [
"jquery.min.js",
"function.js"
], "run_at": "document_start"
}]
现在,在function.js
文件中使用此代码
document.addEventListener("DOMContentLoaded", function() {
document.body.addEventListener("dblclick", function(e) {
var target = e.target;
alert("What you clicked on: " + $(target).text() + " It's font-family is: " + $(target).attr("font-family"));
}, true);
});
我测试了这段代码,它运行正常。我希望这会有所帮助。投票结束,因为这是一个“为什么这段代码不起作用?”的问题。请说得更具体些。例如,您是否有任何错误?它怎么会“不起作用”?你安装正确了吗?很抱歉,如果我没有遵守堆栈的所有规则,我是一个全新的提问者。我没有收到错误。我确实安装正确了。当我点击我的图标时,我应该能够双击任何一段文本,一个警告框将弹出,告诉我它的字体系列。欢迎使用SO!我猜安装可能有问题。你把这个添加到Chrome上了吗?是的,我把它添加到Chrome上了。非常感谢:)我真的很感激。我正在写我的javascript!我希望情况会有所好转。为什么我原来的JS没有用于chrome扩展?@Steven,可能在文档开始时,jquery.JS没有被注入,在加载jquery.JS之前,调用use jquery的其他地方。显然,jquery是先加载的(如果你看代码的话)。你是在评论修复该问题帖子的人!
"content_scripts": [{
"matches": [
"http://*/*",
"https://*/*"
], "js": [
"jquery.min.js",
"function.js"
], "run_at": "document_start"
}]
document.addEventListener("DOMContentLoaded", function() {
document.body.addEventListener("dblclick", function(e) {
var target = e.target;
alert("What you clicked on: " + $(target).text() + " It's font-family is: " + $(target).attr("font-family"));
}, true);
});