I want to show popup by click, but only if condition is false. After click to extension icon background js searchig for tab with current name. If tab found background js continues working. If not found - i want to show popup with instructions. Can`t understand how to just show popup in this case. I can set popup by browserAction.setPopup(), but popup will be displayed only after next clicks. I just want to show my popup one time. It is definitely posible, I've seen this behavior on other extension.
var pcTabs; // tabs array
chrome.browserAction.onClicked.addListener(buttonClick);
function buttonClick() {
// getting tabs...
if(pcTabs.length != 0){
// working with finded tabs
} else{ // tabs not found
// show popup.html here. this is the question
}
}
upd. This is my background.js. All code also in repository. How to replace alerts to popups?
In short: you can't do it the way you describe.
When a popup page is set,
chrome.browserAction.onClicked
won't fire, and the popup will show.When a popup page is not set, your event listener will execute, but you cannot show a popup programmatically. The most you can do is to set a popup for the next click.
So, what to do with it?
1) You can do an ugly hack (kind of) described in Edwin's answer. Always show the popup, check the condition as soon as possible, message the background and execute
window.close()
if the condition is met.Of course, it is ugly.
2) The proper way to do this would be updating the popup whenever the condition can potentially change. That is, whenever you add/remove data from
pcTabs
, you should set/unset the popup withchrome.browserAction.setPopup
3) The fancy way to do it is to use experimental JavaScript method,
Array.observe
, that is only supported in Chrome.Alright this is my code: