Open_Links

自动打开链接列表

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Open_Links
// @name:zh-CN   自动打开链接
// @namespace    https://blog.chrxw.com
// @version      1.0
// @description  自动打开链接列表
// @author       Chr_
// @include      *
// @connect      steamcommunity.com
// @connect      steampowered.com
// @license      AGPL-3.0
// @icon         https://blog.chrxw.com/favicon.ico
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function () {
    'use strict';

    // 注册菜单项
    GM_registerMenuCommand('输入链接列表', openLinks);

    function openLinks() {
        // 弹出输入框
        const input = prompt('请输入网页链接列表,每行一个链接:');
        if (!input) return;

        // 解析输入的链接列表,排除空行和无效的URL
        const urls = input.split('\n').map(url => url.trim()).filter(url => isValidUrl(url));

        if (urls.length === 0) {
            alert('没有有效的链接');
            return;
        }

        // 打开链接
        openNextLink(urls);
    }

    function isValidUrl(url) {
        try {
            new URL(url);
            return true;
        } catch (_) {
            return false;
        }
    }

    function openNextLink(urls) {
        if (urls.length === 0) return;

        const url = urls.shift();
        const newTab = window.open(url, '_blank');

        // 监听标签页关闭事件
        const checkTabClosed = setInterval(() => {
            if (newTab.closed) {
                clearInterval(checkTabClosed);
                openNextLink(urls);
            }
        }, 1000);
    }
})();