我有一个Chrome扩展,当单击扩展图标时,它可以执行window.open()。(由于Chrome中有一个无关的bug,它无法使用传统的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;
}
});
发布于 2012-01-24 02:40:24
而不是使用window.open()使用色度chrome.windows.create..。http://code.google.com/chrome/extensions/windows.html#method-create
...then在回调中,您可以记录它的window.id,然后在任何时候想要使它聚焦,您都可以使用chrome.windows.update。
https://stackoverflow.com/questions/8984047
复制