Search code examples
javascriptgoogle-chrome-extensionbookmarks

Open several tabs inside a new Chrome window


I want to simulate the Chrome bookmark manager's context menu item of any bookmark folder "Open all bookmarks in a new window". In the morning, when I start Windows 8.1 and Chrome, the bookmarks of 4 folders should appear in 4 separate Chrome windows.

Here are my efforts to open one new window:

manifest.json

{
    "manifest_version": 2,
    "name": "MyChromeExtension",
    "description": "Description of my Chrome Extension.",
    "version": "1.0",
    "background": {
        "scripts": [ "atExtensionLoad.js" ],
        "persistent": false
    },
    "permissions": [ "tabs", "bookmarks" ],
    "icons": {
        "16": "images/get_started16.png",
        "32": "images/get_started32.png",
        "48": "images/get_started48.png",
        "128": "images/get_started128.png"
    }
}

atExtensionLoad.js

'use strict';

chrome.runtime.onInstalled.addListener(function () {

    // Search the bookmarks for the folder with the name "Spotify"
    chrome.bookmarks.search("Spotify", function (bookmarkTreeNodes) {
        if (bookmarkTreeNodes.length > 0) {
            // Open all bookmarks of the folder
            openNewWindow(bookmarkTreeNodes[0]);
        }
    });

    // Should open all bookmarks of the given folder inside a new Chrome window
    function openNewWindow(bookmarkTreeNode) {
        // Get all bookmarks of the folder ...
        chrome.bookmarks.getChildren(bookmarkTreeNode.id, function (tabs) {
            // ... and open them as additional tabs inside the existing window
            for (var i = 0; i < tabs.length; i++) {
                // A
                chrome.tabs.create({
                    url: tabs[i].url
                });
                // B (has the same effect as A)
                //window.open(tabs[i].url);
            }
        });

        // One possibility: How can I push the opened tabs into a new Chrome window?
    };
});

Solution

  • Something like:

    chrome.bookmarks.getChildren(bookmarkTreeNode.id, function (tabs) {
        //create a new window with all the bookmarks, each on one tab
        chrome.windows.create({url: tabs.map(tab=>tab.url)});
    });