我正在编写一个 Chrome 扩展程序,它会在单击浏览器操作图标时将我重定向到一个 URL。
我正在尝试使用:
chrome.browserAction.onClicked.addListener
但我明白了
未捕获的类型错误:无法读取未定义的属性“onClicked”
这是我的清单文件:
{
"name": "first extension",
"version": "2.2.12",
"description": "redirct to a link icon",
"browser_action": {
"default_icon": "icontest.png",
"default_title": "Do action"
},
"permissions": ["tabs", "http://*/*"],
"content_scripts": [{
"matches": ["http://*.twitter.com/*", "https://*.twitter.com/*"],
"js": ["twterland.js"]
}],
"icons": {
"16": "icontest.png",
"48": "icontest.png",
"128": "icontest.png"
}
}
这是我的 js 文件:
chrome.browserAction.onClicked.addListener(function(tab) { alert("hi"); });
原文由 Alon Mahl 发布,翻译遵循 CC BY-SA 4.0 许可协议
代码似乎在您的
twterland.js
文件中,这是您的内容脚本。browserAction
只能在扩展页面中使用,因此不能在内容脚本中使用。文档: https ://developer.chrome.com/extensions/content_scripts
而是将其放在 背景页面 上。