61

I am trying to write a JavaScript function that will open my extension like when the extension icon is clicked. I know how to open my extension in a new tab:

var url = "chrome-extension://kelodmiboakdjlbcdfoceeiafckgojel/login.html";
window.open(url);

But I want to open a pop-up in the upper right corner of the browser, like when the extension icon is clicked.

O'Neil
  • 3,790
  • 4
  • 16
  • 30
Yuqing Huang
  • 785
  • 2
  • 7
  • 7
  • 5
    I think that this cannot be done - Chrome only allows extension pop-ups to be opened by a user action. – Wladimir Palant May 07 '12 at 10:14
  • 1
    Duplicate of [(Chrome Extension)How to pragmatically open the popup window from background.html](http://stackoverflow.com/questions/5544256/chrome-extensionhow-to-pragmatically-open-the-popup-window-from-background-htm) – Dan Dascalescu Aug 18 '15 at 14:04
  • One approach that might work would be to setup a keyboard shortcut for the pop up in the extension's manifest, then use an executable file to artificially trigger that keyboard shortcut. See [Native Messaging](https://developer.chrome.com/extensions/nativeMessaging) for more info about how to communicate with an executable file from an extension. – Miscreant May 03 '16 at 08:11
  • why they dont allow to open extension itself.. is it risky or what? – Sunil Garg May 05 '22 at 07:09

6 Answers6

41

The Chromium dev team has explicitly said they will not enable this functionality. See Feature request: open extension popup bubble programmatically :

The philosophy for browser and page action popups is that they must be triggered by user action. Our suggestion is to use the new html notifications feature...

Desktop notifications can be used progammatically to present the user with a small HTML page much like your popup. It's not a perfect substitution, but it might provide the type of functionality you need.

apsillers
  • 112,806
  • 17
  • 235
  • 239
27

Chrome team did create a method to open the popup programmatically, but it's only enabled as a private API, and plans to make it generally available have stalled due to security concerns.

So, as of March 2018 as of now, you still can't do it.

Xan
  • 74,770
  • 16
  • 179
  • 206
7

Short answer is that you cannot open browserAction programmatically. But you can create a dialog with your content script which emulates your browserAction and display that isntead (programmatically). However you won't be able to access your extension's background page from this popup directly as you can from your popup.html. You will have to pass message instead to your extension.

Juzer Ali
  • 4,109
  • 3
  • 35
  • 62
  • Could you provide a bit of code example here? Create a dialog? alert("blah")? doesn't work. I don't seem to be able to get anything to work without clicking on the extension icon. – edencorbin Nov 26 '16 at 21:01
  • 3
    Yes, it seems that most extensions "mimic" having the popup appear by creating a floating div "in the upper right hand" side of your current browser window that *looks* like it came from your extension because it's located close to the icon, and the icon text changes to match, etc. But it didn't, it's just an inserted div...see also https://stackoverflow.com/questions/5544256/chrome-extensionhow-to-pragmatically-open-the-popup-window-from-background-htm – rogerdpack Jun 16 '17 at 01:18
4

As mentioned there is no public API for this.

One workaround I have come up with is launching the extension as an iframe inside a content script with a button click. Whereby the background script emits the extension URL to the content script to be set as the iframe's src, something like below.

background.js

browser.runtime.onMessage.addListener((request) => {
  if (request.open) {
    return new Promise(resolve => {
      chrome.browserAction.getPopup({}, (popup) => {
        return resolve(popup)
      })
    })
  }
})

content-scipt.js

const i = document.createElement('iframe')
const b = document.createElement('button')
const p = document.getElementById('some-id')

b.innerHTML = 'Open'
b.addEventListener('click', (evt) => {
  evt.preventDefault()
  chrome.runtime.sendMessage({ open: true }, (response) => {
    i.src = response
    p.appendChild(i)
  })
})
p.appendChild(b)

This opens the extension in the DOM of the page the script is running on. You will also need to add the below to the manifest.

manifest.json

....
"web_accessible_resources": [
  "popup.html"
]
....
camwhite
  • 837
  • 8
  • 13
0

You could emulate the popup by displaying a fixed html element on the page in the same location the popup would be and style it to look like the popup.

-7

I had the same requirement: When the user clicks on the extension icon a small popup should open. In my case, I was writing an extension which will give updates on selective stocks whenever the icon is clicked. This is how my popup looked.

enter image description here

If you were having the same requirement then please read the answer below.

This is how my manifest.json file looked.

enter image description here

All the heavy lifting was handled by manifest.json file only. There is a section browser_action inside which there is a key called default_popup, just put the name of the HTML file that you want the popup to display.

I wanted my extension to work on all the pages that's why I added the attribute matches under content_scripts. I really didn't need to put the jquery file jquery-3.2.1.js inside the js array but the extension manager was not allowing me to keep that array empty.

Hope this helps, do comment if you have any doubt regarding the answer.

Rito
  • 3,092
  • 2
  • 27
  • 40
  • 9
    sounds like you are just describing the normal vanilla extension behavior: click the icon and a popup opens. The question here is how to open that same popup programmatically and the other answers seem pretty complete. – Tom Grundy Jan 04 '18 at 14:15