Greasy Fork is available in English.

检测网址跳转

选中一个网址后在新标签页跳转,并添加 F2 弹出窗口功能

// ==UserScript==
// @name         检测网址跳转
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  选中一个网址后在新标签页跳转,并添加 F2 弹出窗口功能
// @author       小楠
// @match        *://*/*
// @icon         https://t.tutu.to/img/kjcbA
// @license      MIT
// ==/UserScript==

let isSelecting = false;
let selectedText = '';
let hasPattern = false;
let showedPopup = false;
let currentPageUrl = window.location.href;
let isDetectionEnabled = true; // 默认开启检测
let isProcessed = false;
let lastSelectedUrl = '';

// 添加用于存储提取到的链接的数组
let linkRecords = [];

document.addEventListener('mousedown', function () {
    isSelecting = true;
});

document.addEventListener('mouseup', function () {
    isSelecting = false;
    if (window.getSelection().toString()) {
        selectedText = window.getSelection().toString();
        hasPattern = selectedText.includes('http://') || selectedText.includes('https://') || selectedText.includes('www.');
    } else {
        selectedText = '';
        hasPattern = false;
    }
    checkAndPrompt();
});

let checkAndPrompt = function () {
    if (hasPattern &&!showedPopup && window.location.href === currentPageUrl && isDetectionEnabled) {
        let urlRegex = /(https?:\/\/[^\s]+)/g;
        let urls = selectedText.match(urlRegex);
        if (urls) {
            let urlToCopy = urls[0];
            console.log('Detected URL:', urlToCopy);
            if (urlToCopy!== lastSelectedUrl) {
                navigator.clipboard.writeText(urlToCopy).then(() => {
                    let confirmMessage = `是否要跳转 ${urlToCopy}?`;
                    if (confirm(confirmMessage)) {
                        window.open(urlToCopy, '_blank');
                        // 取消选中的文本
                        if (window.getSelection) {
                            if (window.getSelection().empty) {  // Chrome
                                window.getSelection().empty();
                            } else if (window.getSelection().removeAllRanges) {  // Firefox
                                window.getSelection().removeAllRanges();
                            }
                            // 尝试禁用鼠标事件短暂时间,防止误触发
                            document.body.style.pointerEvents = 'none';
                            setTimeout(() => {
                                document.body.style.pointerEvents = 'auto';
                            }, 500);
                        }
                        isProcessed = true;
                        lastSelectedUrl = urlToCopy;
                    } else {
                        showedPopup = true;
                    }
                });
            }
        }
    } else if (!hasPattern || window.location.href!== currentPageUrl ||!isDetectionEnabled) {
        showedPopup = false;
        isProcessed = false;
        lastSelectedUrl = '';
    }
};

document.addEventListener('selectionchange', checkAndPrompt);

// 添加 F2 键按下的事件处理
document.addEventListener('keydown', function (event) {
    if (event.key === 'F2') {
        const allTextContent = document.body.innerText;
        const links = allTextContent.match(/http[s]?:\/\/[^\s'"<>]+/g) || [];
        linkRecords = [...new Set(links)];

        const popupStyle = `
            position: fixed;
            top: 50%;
            right: 20px;
            transform: translateY(-50%);
            width: 300px;
            height: 300px;
            background-color: #f2f2f2;
            border: 2px solid #444;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
            padding: 20px;
            z-index: 9999;
            overflow-y: auto;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        `;
        const popupTitleStyle = `
            text-align: center;
            font-size: 18px;
            font-weight: bold;
            display: inline-block;
            margin-right: 10px;
        `;
        const linkContainerStyle = `
            margin-top: 20px;
        `;
        const popupContent = `<div style="display:flex;flex-direction:column;justify-content:space-between;height:100%;"><div style="display:flex;justify-content:space-between;align-items:center;"><h3 style="width:100%;${popupTitleStyle}">以下是网页内的链接</h3><span id="closePopup" style="cursor: pointer; font-size: 20px; color: #888; text-decoration: none;">✕</span></div><br/><div id="linkContainer" style="${linkContainerStyle}"></div></div>`;
        const popup = document.createElement('div');
        popup.style.cssText = popupStyle;
        popup.innerHTML = popupContent;
        document.body.appendChild(popup);

        let isDragging = false;
        let offsetX, offsetY;
        let originalBorderColor = '#444';
        let draggingBorderColor = '#ff6600';

        popup.addEventListener('mousedown', function (event) {
            isDragging = true;
            offsetX = event.clientX - popup.offsetLeft;
            offsetY = event.clientY - popup.offsetTop;
            popup.style.borderColor = draggingBorderColor;
        });

        document.addEventListener('mousemove', function (event) {
            if (isDragging) {
                popup.style.left = event.clientX - offsetX + 'px';
                popup.style.top = event.clientY - offsetY + 'px';
            }
        });

        document.addEventListener('mouseup', function () {
            isDragging = false;
            popup.style.borderColor = originalBorderColor;
        });

        const closeButton = document.getElementById('closePopup');
        closeButton.addEventListener('click', function () {
            document.body.removeChild(popup);
        });

        // 添加鼠标滚轮事件处理
        popup.addEventListener('wheel', function (event) {
            event.preventDefault();
            popup.scrollTop += event.deltaY;
            // 确保关闭按钮始终在固定位置
            closeButton.style.top = '8px';
            closeButton.style.right = '8px';
        });

        // 创建链接容器并填充链接
        const linkContainer = popup.querySelector('#linkContainer');
        linkRecords.forEach((link, index) => {
            const linkElement = document.createElement('p');
            linkElement.innerHTML = `<a href="${link}" target="_blank">${index + 1}. ${link}</a>`;
            linkContainer.appendChild(linkElement);
        });
    }
});