How can I open my extension's pop-up with JavaScript?

Google ChromeGoogle Chrome-Extension

Google Chrome Problem Overview


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.

Google Chrome Solutions


Solution 1 - Google Chrome

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.

Solution 2 - Google Chrome

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.

Solution 3 - Google Chrome

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.

Solution 4 - Google Chrome

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"
]
....

Solution 5 - Google Chrome

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYuqing HuangView Question on Stackoverflow
Solution 1 - Google ChromeapsillersView Answer on Stackoverflow
Solution 2 - Google ChromeXanView Answer on Stackoverflow
Solution 3 - Google ChromeJuzer AliView Answer on Stackoverflow
Solution 4 - Google ChromecamwhiteView Answer on Stackoverflow
Solution 5 - Google ChromeRitoView Answer on Stackoverflow