【问题标题】:Can you focus a popup window from a Chrome Extension您可以从 Chrome 扩展程序中聚焦弹出窗口吗
【发布时间】:2012-01-24 08:50:04
【问题描述】:

我有一个 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;
  }
});

【问题讨论】:

    标签: google-chrome-extension


    【解决方案1】:

    不要使用 window.open(),而是使用 Chrome 的 chrome.windows.create...http://code.google.com/chrome/extensions/windows.html#method-create
    ...然后在回调中您可以记录它的 window.id,然后您可以随时使用 chrome.windows.update。

    【讨论】:

    • 干杯,这正是我需要的。
    猜你喜欢
    • 2012-05-07
    • 2012-09-05
    • 2013-01-17
    • 2023-01-02
    • 1970-01-01
    • 2011-09-27
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多