复制内容中包含网址时自动在新标签打开该网址
// ==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);
}
});
})();