Unable to trigger chrome.browserAction.onClicked.addListener with google chrome extensions

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

I'm a bit stuck here and was wondering if anyone can point out where I might be wrong.

I am simply trying to make the body color change to red on click of the app icon.

manifest.json

{     "name": "Bagde",     "description": "",     "version": "1",     "manifest_version": 2,     "background": {         "scripts": [             "background.js"         ]     },     "browser_action": {         "default_title": "Test",         "default_popup": "popup.html"     } } 

popup.html

               

Some Content ..

popup.js

document.addEventListener("DOMContentLoaded", function () {     //Get Reference to Functions     backGround = chrome.extension.getBackgroundPage();     //Call Function     backGround.updateIcon();   }); 

background.js

var i = 1;  function updateIcon() {     i = 1;     chrome.browserAction.setBadgeText({         text: 'Test'     });     chrome.browserAction.setPopup({         popup: "popup.html"     }); }   chrome.browserAction.setBadgeBackgroundColor({     color: [200, 0, 0, 100] });  window.setInterval(function () {     chrome.browserAction.setBadgeText({         text: String(i)     });     i++; }, 4000);  chrome.browserAction.onClicked.addListener(function(tab) {     chrome.tabs.executeScript(null,         {code:"document.body.bgColor='red'"}); }); 

any ideas what I may be doing wrong? Thanks for taking your time to reading this.

回答1:

If you define default_popup, you can't have a listener for browserAction.onClicked. In this case you can simply add the code in your handler to your popup.js.

EDIT: That is, add to popup.js the following:

chrome.tabs.executeScript(null, {code:"document.body.bgColor='red'"}); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!