您可以关注Chrome扩展程序中的弹出窗口吗? [英] Can you focus a popup window from a Chrome Extension
问题描述
我有一个Chrome扩展程序,当单击扩展程序图标时,它会执行window.open()。 (由于Chrome中存在无关的错误,因此无法使用传统的Chrome扩展程序弹出窗口)。我想知道如果有一种方法可以集中弹出窗口,如果它已经打开。 Chrome禁用了window.focus(),但我认为Chrome扩展中可能会有这样做。
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;
}
});
推荐答案
而不是使用window.open()使用Chromes chrome.windows.create ... http://code.google.com /chrome/extensions/windows.html#method-create
...然后在回调中你可以记录它的window.id,然后任何时候你想让它聚焦,你可以使用chrome.windows.update。
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.
这篇关于您可以关注Chrome扩展程序中的弹出窗口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!