【发布时间】: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;
}
});
【问题讨论】: