lomse
lomse

Reputation: 4165

How to programmatically open chrome extension popup.html

I have a created a desktop notification using google extension which works great:

icon = '';
    var popup = window.webkitNotifications.createNotification(my notification');
            icon, 'Awesome title', 'Click here to view more. ');
    popup.show();

Is there a way to launch the actual google extension popup.html (as shown on the image below), when the user click on the the desktop notification?

The popup.html The desktop notification

Thanks.

Upvotes: 39

Views: 50245

Answers (4)

Roy Six
Roy Six

Reputation: 298

Starting in newer versions of Chrome (127+, expected stable in July 2024), it is possible to open the popup programmatically by calling chrome.action.openPopup().

Note: Because you can't access the chrome.action API from a content script, if you need to open the popup from there you can send a message to the service worker to do it.

chrome.action.openPopup(
  options?: OpenPopupOptions,
  callback?: function,
)

Upvotes: 4

towith
towith

Reputation: 307

I think maybe we can only get option page address from chrome extension's manifest json file, then open the address like chrome-extension://<extensionId>/options.html, which extension can not do, but require external program to get option addresses.

Upvotes: 0

Ambrose Leung
Ambrose Leung

Reputation: 4215

I wanted to add to the comment that Miscreant posted.

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

This is how you set up a shortcut key that opens your extension.

...
"commands": {
"_execute_browser_action": {
    "suggested_key": {
          "default": "Ctrl+Shift+Y"
        }
}

Here's the API on 'commands'.

Upvotes: 7

Franz Payer
Franz Payer

Reputation: 4137

Short answer is that you can't, but if you want to open the popup in a new tab instead, this is how you would do it:

Put this before you call show

popup.onclick = function() { 
    chrome.tabs.create({url : "popup.html"}); 
    popup.cancel();
}

Upvotes: 18

Related Questions