简体   繁体   中英

Chrome Browser Action click not working

I am trying to create chrome extension, however my browser action click does not work! I've tried pretty much everything. Here is my setup:

manifest.json:

{
"name": "blah",
"version": "1.0",
"description": "blah",
"browser_action": {
  "default_icon": "icon1.png",
  "popup": "popup.html"
 },  
"permissions": [
  "bookmarks",
  "tabs",
  "http://*/*", 
  "https://*/*"    
],
"background_page": "background.html"
}

popup.html:

<html>
<head>
<script>
<!-- Try adding the listener in popup.html  -->
    chrome.browserAction.onClicked.addListener( function(tab){
    console.log("Hello from popup"); // This does not show up either
    } );
 </script>
</head><body>
 Hello
</body>
</html>

background.html:

<html>
<head>
<script>
console.log("Background.html"); // This gets displayed. O.K.

function hello() {
  console.log("HELLO"); // THIS NEVER GETS DISPLAYED
}

// Supposed to Called when the user clicks on the browser action icon.
chrome.browserAction.onClicked.addListener(hello); 
</script>
</head>
</html>

But no matter how hard i click on the Icon nothing happens and "HELLO" is not printed out in the console!

I am using Chrome 4.0.249.43. I installed the Beta version BUT it is exactly the same as the released version (same verion number). Could that be a problem?

You cannot have a "popup" with an onclick event. Remove the popup.html from the manifest file. And keep the background page, and it will work.

You need delete the popup.html, you already have a popup in popup.html in theory the background.html should do the event for browserAction but is wrong. When you clicked in the Icon of you app there's already defined an function onClicked in popup.

i dont know for what you need, but you can make many functions when you clicked in the browser.

For example: in background.html do:

 foo(){
        if(browserAction && browserAction.onClicked) // you can add all stuff that you need.
        do something
    }

and you call from popup.html with chrome.extension.getBackgroundPage().foo();

well i hope this help you.

I found the root cause with my problem on :- MDN

browser​Action​.onClicked:- Fired when a browser action icon is clicked. This event will not fire if the browser action has a popup.

removed the deafult_popup from manifest.json

"browser_action": {
    "default_icon": "img/icon.png",
    "default_popup": "popup.html"
  }

I had some problems too while trying to create a chrome extension. Make sure you reload the extension after any change to the manifest.json or the background page. To reload it, go to the extensions page (wrench>tools>extensions) and choose reload. That might just be the cause of the problem, everything else seems just fine.

According to the doc :

chrome.browserAction.onClicked.addListener(function(Tab tab) {...});

so:

// Supposed to be called when the user clicks on the browser action icon.    
chrome.browserAction.onClicked.addListener(function(tab) {
    hello();
});

should work

Are you sure that you're viewing the correct JavaScript console for the background page, and not for the other tab?

Try having your hello() method set a global variable in the background page, then have a button in popup.html retrieve that variable (using chrome.extension.getBackgroundPage()) and display it as an alert.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM