Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show page action popup on click

I'm making a chrome extension that uses pageAction.

I can set when it shows whether I want it to have a popup or handle the click myself.

What I want to do is handle the click myself, but with certain scenarios I don't want to process the normal code, and want to show the user a message. Preferably with a popup.

But it seams I can either make the pageAction have a popup or have an onClick. But not both.

I can show an alert, but that is ugly.

like image 294
HannesNZ Avatar asked Oct 14 '22 23:10

HannesNZ


2 Answers

Currently, there is no "neat" or official way to handle both. You can just do either. But there are some work arounds that some Google extension product have done.

First of all, set it up to show the popup. And within your pageAction popup, you can have the initialization code to be something like this:

Page Action Popup:

function init() {
  if (getClickBehaviour() == 'popup')
    handlePopup();
  else
    openPage();
}

function getClickBehaviour() {
  return localStorage['CLICK_BEHAVIOR'] || 'popup';
}

function openPage() {
    chrome.tabs.create({url: 'http://google.ca'});
    window.close();
  });
}

init();

Then you can let your options, set the click behavior. If you want different behaviors on each click, you can do that too.

As you noticed, we are closing the popup right after for the "default" behavior that we don't want the popup to show. That is currently the only way to implement different behaviors.

like image 131
Mohamed Mansour Avatar answered Oct 20 '22 16:10

Mohamed Mansour


I haven't tested this myself yet, but have you tried setting the popup to the empty string when you want to handle the click (chrome.pageAction.setPopup('')) and to your popup when you want to show a message. I'm not perfectly sure if the onClicked event handler gets called in that case (where the popup is dynamically set to the empty string), but it's worth looking into.

As far as I know, there is generally no way to programmatically open a popup window for a page or browser action. (Which is too bad, I would love this functionality; but you can imagine some of the annoyances if this were possible.)

like image 24
npdoty Avatar answered Oct 20 '22 16:10

npdoty