12

I have developed a chrome extension that opens a popup when I click on the icon near the address bar. Everything works fine, however I want to add some functionality to it. So I thought I'd also add a context menu item so that the user can simply search for the highlighted word. I want the popup to showup when the user clicks on the item in the context menu(the default popup in the top right corner and not a new popup window or a new tab).

Can I have this functionality? If yes, how do I implement it?

Annihilator8080
  • 2,019
  • 3
  • 21
  • 20
  • 3
    You cannot programatically open the browser action's popup. – 方 觉 Jul 25 '13 at 08:35
  • But I don't want to lose the context of the tab the user is on currently. What alternatives do I have? Do I open the url in a new window then? – Annihilator8080 Jul 25 '13 at 09:21
  • 1
    Related/duplicate: [How can I open my extension's pop-up with JavaScript?](http://stackoverflow.com/q/10479679/710446) (Answer: you can't.) – apsillers Jul 25 '13 at 12:46

3 Answers3

13

You can't make the popup page show programmatically as if the user clicked it.

However, you can still have something display based on the background script / content menu click. There are 4 main options for your background script:

  1. Open a new tab to the popup.html page
  2. Programmatic injection of javascript to construct a popup-like dialog on the page
  3. Content script message passing to do the same as above, using a running content-script.
  4. Use the notifications API for a simple minimally stylized message to the user.

Options 2, 3, 4 will allow the user to stay on their tab without any navigation. The notifications API route is the simplest to use if you just want some quick notification to the user, and there are fewer security snags. 2 and 3 require more book-keeping, but you can make the dialog look like your popup.

Tyler Peryea
  • 523
  • 4
  • 11
  • Do you know of an example for #2? The linked page just shows how to us pi to change the existing DOM, but not about constructing the dialog. – AdamMc331 Jan 24 '16 at 19:42
  • As soon as you can do PI, you can construct a dialog anyway you could via javascript. The only issue here is that PI for the CSS, HTML and JS you need may become tricky due to bookkeeping. See the example below, for pure js code that would make a dialog. If you gave this code to execute, as in the PI example above, it should make the dialog on the page. https://jsfiddle.net/pzw7kfvf/ – Tyler Peryea Feb 02 '16 at 23:29
1

There should be an API for it now (as in 2023) https://developer.chrome.com/docs/extensions/reference/action/#method-openPopup

update: tried, but failed, there was a bug. https://github.com/GoogleChrome/developer.chrome.com/issues/2602 hope they fix it soon.

AiShiguang
  • 1,031
  • 1
  • 9
  • 13
0

I was looking for extensions that I have already been used, but forget its name. This extension opened up the result into Context Menu, without open new TAB or popup.

may be useful

https://developer.chrome.com/extensions/contextMenus