chrome.windows.create 多次执行时创建新的弹出窗口
chrome.windows.create creates new popups when execute multiple times
我有一个带有 browser_action 按钮的 chrome 扩展。单击此浏览器操作时,后台脚本会打开一个弹出窗口,如下所示
const popup = window.open('popup.html', tab.id, 'menubar=0,innerWidth=900,innerHeight=800');
当用户多次点击浏览器操作时,每次都会重新创建弹出窗口(我没有得到多个弹出窗口 windows)
但是,现在我想用 browser.windows.create
替换此代码,据我所知,它更像是扩展方式。所以我创建了
browser.windows.create({url: 'popup.html', height: 800, width: 900, type: 'popup'});
这几乎完全相同,除了当用户多次单击浏览器操作按钮时,我得到多个弹出窗口 windows,这不是我想要的。有没有办法获得与 `window.open?
相同的行为
这是我的建议(打字稿):
function findTab(tab: chrome.tabs.Tab) {
return tab.url == "popup.html";
}
// first get all open chrome windows
let windows = await chrome.windows.getAll({populate: true, windowTypes: ["popup"]});
// find a window that has a tab with the url "popup.html"
let myWindow = windows.find(window => window.tabs.some(tab => findTab(tab)));
// find a tab that has the url "popup.html"
let myTab = myWindow?.tabs.find(tab => findTab(tab));
if (myWindow && myTab) {
// if such tab exists, focus the parent window and the tab
await chrome.windows.update(myWindow.id, {focused: true});
await chrome.tabs.update(myTab.id, {active: true});
}
else {
// open the window and the tab
await chrome.windows.create({url: "popup.html", type: "popup"});
}
我有一个带有 browser_action 按钮的 chrome 扩展。单击此浏览器操作时,后台脚本会打开一个弹出窗口,如下所示
const popup = window.open('popup.html', tab.id, 'menubar=0,innerWidth=900,innerHeight=800');
当用户多次点击浏览器操作时,每次都会重新创建弹出窗口(我没有得到多个弹出窗口 windows)
但是,现在我想用 browser.windows.create
替换此代码,据我所知,它更像是扩展方式。所以我创建了
browser.windows.create({url: 'popup.html', height: 800, width: 900, type: 'popup'});
这几乎完全相同,除了当用户多次单击浏览器操作按钮时,我得到多个弹出窗口 windows,这不是我想要的。有没有办法获得与 `window.open?
相同的行为这是我的建议(打字稿):
function findTab(tab: chrome.tabs.Tab) {
return tab.url == "popup.html";
}
// first get all open chrome windows
let windows = await chrome.windows.getAll({populate: true, windowTypes: ["popup"]});
// find a window that has a tab with the url "popup.html"
let myWindow = windows.find(window => window.tabs.some(tab => findTab(tab)));
// find a tab that has the url "popup.html"
let myTab = myWindow?.tabs.find(tab => findTab(tab));
if (myWindow && myTab) {
// if such tab exists, focus the parent window and the tab
await chrome.windows.update(myWindow.id, {focused: true});
await chrome.tabs.update(myTab.id, {active: true});
}
else {
// open the window and the tab
await chrome.windows.create({url: "popup.html", type: "popup"});
}