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

6

63

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.

Tannenbaum answered 7/5, 2012 at 9:28 Comment(4)
I think that this cannot be done - Chrome only allows extension pop-ups to be opened by a user action.Prim
Duplicate of (Chrome Extension)How to pragmatically open the popup window from background.htmlNadia
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 for more info about how to communicate with an executable file from an extension.Lexicology
why they dont allow to open extension itself.. is it risky or what?Guitar
N
42

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.

Negligent answered 7/5, 2012 at 15:28 Comment(2)
Somehow it is possible with Google's own Google Cast extension.Patterson
@Derek朕會功夫 I can't find that extension. Have to check does it opens the popup or injects script.Candlestick
T
28

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.

Twedy answered 4/3, 2016 at 12:42 Comment(0)
S
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.

Stoltz answered 8/5, 2012 at 7:54 Comment(2)
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.Ani
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 #5544756Reinhold
C
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"
]
....
Circumlunar answered 27/11, 2018 at 21:18 Comment(0)
B
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.

Berny answered 2/2, 2023 at 22:39 Comment(0)
F
-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.

Feel answered 11/11, 2017 at 18:37 Comment(1)
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.Dingess

© 2022 - 2024 — McMap. All rights reserved.