在新的Chrome窗口中打开几个标签

时间:2018-12-19 14:00:25

标签: javascript google-chrome-extension bookmarks

我想模拟任何书签文件夹“在新窗口中打开所有书签”的Chrome书签管理器的上下文菜单项。早晨,当我启动Windows 8.1和Chrome时,4个文件夹的书签应出现在4个单独的Chrome窗口中。

这是我打开一个新窗口的努力:

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?
    };
});

1 个答案:

答案 0 :(得分:2)

类似的东西:

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)});
});