Home » Javascript » javascript – event onClicked doesn't work when I open browser action popup in a Google Chrome extension

javascript – event onClicked doesn't work when I open browser action popup in a Google Chrome extension

Posted by: admin February 23, 2020 Leave a comment

Questions:

following the browserAction docs, already exists an event listener when my browser action popup is clicked (opened).

onClicked

Fired when a browser action icon is clicked. Does not fire if the browser action has a popup.

addListener

chrome.browserAction.onClicked.addListener(function callback)

I loaded this event in a background script events.js from manifest

{
    ...
    "background": {
        "scripts": ["events.js"],
        "persistense": false
    },
    "default_popup": "popup.html"
}

events.js

chrome.browserAction.onClicked.addListener(function (tab) { console.log('popup clicked') });

events.js is loaded because I put this code and works it:

chrome.browserAction.setTitle({title: 'new title of my popup'});

But onClicked event listener doesn’t works. Any idea to fire an event when popup is opened ?

How to&Answers: