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;
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With