javascript - 在新的 Chrome 窗口中打开多个标签页

标签 javascript google-chrome-extension bookmarks

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

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

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

最佳答案

类似于:

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

关于javascript - 在新的 Chrome 窗口中打开多个标签页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53852828/

相关文章:

google-chrome - 我可以使用 chrome.devtools API 访问 JavaScript 片段吗?

emacs - Emacs 的上下文

javascript - AJAX 内容加载器 : it's SEO friendly, 但书签呢?

javascript - Firebase 数据库规则 - 如何不覆盖现有数据

javascript - Meteor 1.3、React、React Router - 数据未通过

javascript - Google Chrome - 使用 iframe 时屏幕捕获失败,相同的脚本在没有 iframe 的情况下工作

javascript - 保存 popup.html 信息以供下一次 session 使用

javascript - 如何在文本中替换更多值?

javascript - 如何在单击jquery时交换图像

emacs - 如何在 Emacs à la Vim 中设置标记?