你能否在Chrome扩展程序中使弹出窗口获得焦点?

15
我有一个Chrome扩展程序,当单击扩展图标时,它会执行window.open()。 (由于Chrome中的一个不相关的错误,它无法使用传统的Chrome扩展程序弹出窗口)。 我想知道是否有一种方法可以聚焦已经打开的弹出窗口。 Chrome禁用了window.focus(),但我认为在Chrome扩展程序中可能有一种方法来实现它。
更新: 对于任何感兴趣的人,这是我最终在后台页面中使用的代码:
var popupId;

// When the icon is clicked in Chrome
chrome.browserAction.onClicked.addListener(function(tab) {

  // If popupId is undefined then there isn't a popup currently open.
  if (typeof popupId === "undefined") {

    // Open the popup
    chrome.windows.create({
      "url": "index.html",
      "type": "popup",
      "focused": true,
      "width": 350,
      "height": 520
    }, function (popup) {
      popupId = popup.id;
    }); 

  } 
  // There's currently a popup open
  else {
     // Bring it to the front so the user can see it
    chrome.windows.update(popupId, { "focused": true });  
  }

});

// When a window is closed
chrome.windows.onRemoved.addListener(function(windowId) {
  // If the window getting closed is the popup we created
  if (windowId === popupId) {
    // Set popupId to undefined so we know the popups not open
    popupId = undefined;
  }
});
1个回答

15

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接