Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you focus a popup window from a Chrome Extension

I have a Chrome Extension that does a window.open() when the extensions icon is clicked. (It can't use the traditional Chrome extension popup due to an unrelated bug in Chrome). I'm wondering if there's a way to focus a popup window if its already open. Chrome disables window.focus() but I thought there might be a way to do it in a Chrome Extension.

Update: For anyone interested this is the code I ended up using in my background page:

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;
  }
});
like image 941
Sean Bannister Avatar asked Jan 24 '12 08:01

Sean Bannister


Video Answer


1 Answers

Instead of using window.open() use the Chromes chrome.windows.create... http://code.google.com/chrome/extensions/windows.html#method-create
...then in the call back you can record its window.id and then any time you want to make it focused you can use chrome.windows.update.

like image 140
PAEz Avatar answered Sep 23 '22 05:09

PAEz