单击上下文菜单时打开扩展弹出窗口

Ped*_*ndi 2 google-chrome google-chrome-extension

我必须做一个扩展,在上下文菜单中单击文本时,在回调中打开扩展菜单弹出窗口。

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


    };
Run Code Online (Sandbox Code Playgroud)

在这种情况下,当我单击菜单时,将使用此搜索打开一个新选项卡。

小智 5

无法以编程方式打开默认浏览器操作弹出窗口。解决方法是使用内容脚本打开模式或灯箱并显示弹出窗口的内容。

另一种方法是-在上下文菜单项的clickhandler中,创建一个新标签,使其处于非活动状态,然后将该标签传递给chrome.windows.create api,以创建一个新的弹出窗口。

    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
      });
  });
Run Code Online (Sandbox Code Playgroud)

这只是一种解决方法。希望能帮助到你。