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.
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.
If you were having the same requirement then please read the answer below.
This is how my
manifest.json
file looked.All the heavy lifting was handled by
manifest.json
file only. There is a sectionbrowser_action
inside which there is a key calleddefault_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
undercontent_scripts
. I really didn't need to put the jquery filejquery-3.2.1.js
inside thejs
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.
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 the extension URL to the content script to be set as the iframe'ssrc
, something like below.background.js
content-scipt.js
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
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 2018as of now, you still can't do it.The Chromium dev team has explicitly said they will not enable this functionality. See Feature request: open extension popup bubble programmatically :
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.
Short answer is that you cannot open
browserAction
programmatically. But you can create adialog
with your content script which emulates yourbrowserAction
and display that isntead (programmatically). However you won't be able to access your extension'sbackground page
from this popup directly as you can from yourpopup.html
. You will have topass message
instead to your extension.