Pedram marandi
Pedram marandi

Reputation: 1614

Open extension popup when click on context menu

I have to make an extension that when clicked on text in the context menu, in callback opens the extension menu popup.

chrome.runtime.onInstalled.addListener(function() {
  var context = "selection";
  var title = "Google for Selected Text";
  var id = chrome.contextMenus.create({"title": title, "contexts":["selection"],
                                         "id": "context" + context});  
});

// add click event
chrome.contextMenus.onClicked.addListener(onClickHandler);

// The onClicked callback function.
function onClickHandler(info, tab) {
  var sText = info.selectionText;
  var url = "https://www.google.com/search?q=" + encodeURIComponent(sText);  


    //what i have put here to open extension popup


    };

In this case, when I click on the menu I open a new tab with this search.

Upvotes: 4

Views: 5716

Answers (4)

Brandon Richardson
Brandon Richardson

Reputation: 83

It is now possible to open a browser action popup programmatically from inside the handler for a user action.

browser.menus.create({
  id: "open-popup",
  title: "open popup",
  contexts: ["all"]
});

browser.menus.onClicked.addListener(() => {
  browser.browserAction.openPopup();
});

You can read more about it here.

Edit:

This feature is only available as of Firefox 57. In Chrome, it is only available in the dev channel.

Sources: chrome/common/extensions/api/_api_features.json - chromium/src - Git at Google

Upvotes: 3

manutdfan
manutdfan

Reputation: 295

There is no way of opening the default browser action popup programmatically. A work around is use content scripts to open a modal or a lightbox and show the contents of your popup.

Another way would be - within the clickhandler of your context menu item, create a new tab and make it inactive and then pass that tab to chrome.windows.create api to create a new popup window.

    chrome.tabs.create({
      url: chrome.extension.getURL('popup.html'),
      active: false
  }, function(tab) {
      // After the tab has been created, open a window to inject the tab
      chrome.windows.create({
          tabId: tab.id,
          type: 'popup',
          focused: true
      });
  });

It is just a work around. Hope it helps.

Upvotes: 6

Daniel Olszewski
Daniel Olszewski

Reputation: 14401

Unfortunately, it cannot be done.

Chrome API doesn't provide a method to open extension popup programmatically. The Chromium team rejected the feature request for such an option with an explanation that:

The philosophy for browser and page action popups is that they must be triggered by user action.

Here's the source.

Upvotes: 2

Emrys Myrooin
Emrys Myrooin

Reputation: 2231

You can use the chrome.window API (documentation here).

What you want is something like this :

chrome.windows.create({
    url : "http://yourPopupUrl.com"
    focused : true
    type : "popup"});

This will open a new windows in popup mode (without the top menu bar) and load the "http://yourPopupUrl.com".

Upvotes: 1

Related Questions