Greasy Fork

复制网址自动打开新标签页

复制内容中包含网址时自动在新标签打开该网址

// ==UserScript==
// @name         复制网址自动打开新标签页
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  复制内容中包含网址时自动在新标签打开该网址
// @source        https://github.com/Phinsin666/Copying-a-URL-automatically-opens-a-new-tab
// @author       Phinsin666T
// @match        *://*/*
// @grant        none
// ==/UserScript==


(function () {
    'use strict';

    document.addEventListener('copy', async () => {
        try {
            // 延迟一点时间以获取剪贴板数据
            await new Promise(resolve => setTimeout(resolve, 100));

            // 读取剪贴板文本
            const text = await navigator.clipboard.readText();
            if (!text) return;

            // 提取网址(支持 http 和 https)
            const urlRegex = /(https?:\/\/[^\s]+)/g;
            const matches = text.match(urlRegex);
            if (matches && matches.length > 0) {
                // 避免打开多个,取第一个网址
                const url = matches[0];
                window.open(url, '_blank');
            }
        } catch (err) {
            console.error('复制监听失败:', err);
        }
    });
})();