我正在尝试在谷歌浏览器中开发我的第一个扩展,我正在遵循这个教程:Tutorial除了content.js中的2行代码之外,所有的东西都工作得很好。问题是"content.js“的内容不正确,我在chrome控制台的第2行中变得”未定义“。有人能帮我吗,让我知道为什么会发生这种情况?我和你分享content.js的内容是为了帮助我。
content.js内容:
var firstHref = $("a[href^='http']").eq(0).attr("href");
console.log(firstHref);清单/json文件内容:
{
"manifest_version": 2,
"name": "My Cool Extension",
"version": "0.2",
"icons": { "128": "icon_128.png" },
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["jquery-2.1.3.min.js", "content.js"]
}
],
"browser_action": {
"default_icon": "icon.png"
},
"permissions": [
"tabs"
]
}发布于 2017-10-10 17:28:45
我猜,您的内容脚本是在整个文档加载之前加载的,这就是它返回undefined的原因。为了确保您的内容脚本被正确注入,您应该在文件manifest.json示例中定义"run_at“:
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_end",
"all_frames": true
}
],有关更多详细信息,请查看Content Scripts parameters。
https://stackoverflow.com/questions/46662924
复制相似问题