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

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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';

    let lastClipboardText = '';

    document.addEventListener('keydown', async (e) => {
        if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'c') {
            // 延迟一点读取剪贴板
            setTimeout(async () => {
                try {
                    const text = await navigator.clipboard.readText();
                    if (!text || text === lastClipboardText) return;

                    lastClipboardText = text;

                    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.warn('无法读取剪贴板内容:', err);
                }
            }, 100);
        }
    });
})();