且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在现有的隐身窗口中打开新标签页?

更新时间:2023-12-02 22:08:40

如果要在现有窗口中创建标签,则可以使用 chrome.windows.getAll() 获取当前打开的窗口的数组并遍历结果,直到看到 incognito 设置为 true 的窗口

If you want to create a tab inside an existing window you can use chrome.tabs.create() specifying the windowId of an existing window. To know which one of the open windows is in incognito mode, you can use chrome.windows.getAll() to get an array of currently open windows and iterate through the results until you see one with incognito set to true.

这是一个可行的示例:

chrome.windows.getAll({populate: false, windowTypes: ['normal']}, function(windows) {
    for (let w of windows) {
        if (w.incognito) {
            // Use this window.
            chrome.tabs.create({url: "https://google.com", windowId: w.id});
            return;
        }
    }

    // No incognito window found, open a new one.
    chrome.windows.create({url: "https://google.com", incognito: true});
});