ekclone
ekclone

Reputation: 1092

Open multiple links in incognito with chrome.windows.create and chrome.tabs.create

I'm making a chrome extension for myself where i want to open multiple tabs in incognito for new sessions for my websites,

When the function is called it should open a new incognito window and open 4-5 tabs there, but currently the links are opened in the window where the chrome extension button has been clicked.

Current code:

    chrome.windows.create({focused: true, incognito: true }, function(win) {
          for (var i = 0; i < links.length; i++) {
            array = links[i].href;
            chrome.tabs.create({ 
                url: array, 
                selected: true
            })
          }
    });

What should I change to make it open the new tabs in the incognito window?

Upvotes: 1

Views: 4786

Answers (2)

Jan Croonen
Jan Croonen

Reputation: 719

I also wanted to open several tabs in a newly created incognito window. Using the object received from creating the window to open the tabs with.

        chrome.windows.create({
            url: splashNodes[0].url,
            incognito: true,
        }, w => {
            console.log({w});
            for (let i = 1; i < splashNodes.length; i++) {
                chrome.tabs.create({
                    url: splashNodes[i].url,
                    windowId: w.id
                });
            }
        })

The window created as incognito didn't give anything back in the callback. chrome.windows.onCreated didn't fire either. Adding permission {"incognito":"split"} to the manifest as suggested by someone didn't help.

What solved my problem was when the user granted incognito permission to my extension (extension manager, details) though, as explained on https://developer.chrome.com/extensions/permission_warnings.

Upvotes: 2

woxxom
woxxom

Reputation: 73526

Apparently the callback is invoked right after the window was created but before it was focused.

Specify the new window id in chrome.tabs.create options explicitly:

chrome.tabs.create({ 
    url: 'http://example.com', 
    windowId: win.id,
});

Upvotes: 2

Related Questions