Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

javascript - chrome.browserAction.onClicked.addListener() with popup

I want to add Listener to the event which fires, everytime the browser icon is clicked. I have also a popup which comes up on click on this icon.

I tried chrome.browserAction.onClicked.addListener() but didnot get it fired, later i saw that the doc says:

Fired when a browser action icon is clicked. 
This event will not fire if the browser action has a popup. 

so, I have popup, so this Listener doesnot work. What workaround can I do to attach Listener to icon in my case?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no workaround to attach a listener to that event, but you can instead use messaging to let your background page know that the popup was opened.

In your popup, as soon as possible:

chrome.runtime.sendMessage({popupOpen: true});

In your background page:

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
  if(message.popupOpen) { /* do your stuff */ }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...