Nucktrooper Nucktrooper - 1 year ago 133
Javascript Question

Chrome extension, switch browserAction onclick

I am developping an extension and I would like to let the users choose if they want the browserAction button to open an HTML page in the popup or to open it in a new tab.

first case (in manifest):

"browser_action": {
"default_icon": {
"19": "data/button.png"
},
"default_page": "./popup/popup.html"
}


second case (in index.js):

chrome.browserAction.onClicked.addListener(function() { window.open('../popup/popup.html', ...); });


Is there a way to switch between these two cases in the background script?

Answer Source

Yes, you can use chrome.browserAction.setPopup(). If you set the popup to '', no popup is shown:

chrome.browserAction.setPopup({popup: ''});

You can then set the popup back to a HTML page when you are wanting the popup:

chrome.browserAction.setPopup({popup: '/myPopup.html'});

A convenient thing is that if a popup is shown, the browserAction event is not fired. Thus, the tab will not open. This switches between the two by either setting the browserAction.setPopup() to '', or not.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download