chrome extensions 开发过程中 background.js 调用chrom.** js的过程中经常遇到
background.js:24 Uncaught TypeError: Cannot read property 'onClicked' of undefined
at background.js:24
chrome.browserAction.onClicked.addListener(function(tab) {
alert("hello world");
解决办法
https://developer.chrome.com/extensions/browserAction#event-onClicked
onClicked
Fired when a browser action icon is clicked. This event will not fire if the browser action has a popup.
"browser_action": {
"default_icon": "image/icon.png" ,
"default_title": "default title",
"default_popup": "popup.html"
},
去掉default_popup 就可以了
第二个错误
_generated_background_page.html:1 Error in event handler for runtime.onInstalled: TypeError: Cannot read property 'onPageChanged' of undefined
at chrome-extension://ncbflhomeaakikacjdicoamijeabneml/js/background.js:8:27
chrome.runtime.onInstalled.addListener(function() {
// Replace all rules ...
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
解决办法
https://developer.chrome.com/extensions/declarativeContent
chrome.declarativeContent
Description: | Use the chrome.declarativeContent API to take actions depending on the content of a page, without requiring permission to read the page's content. |
Availability: | Since Chrome 33. |
Permissions: | "declarativeContent" |
至此问题解决
总结: 遇到这种错误一般都是相关属性使用错误
主要参考官方文档
https://developer.chrome.com/extensions 官方文档首页
https://developer.chrome.com/extensions/samples 官方demo