Greasy Fork

Greasy Fork is available in English.

取消超链接

长按<a>元素超链接取消超链接,并提供一个额外的新的按钮以恢复原超链接。

当前为 2025-03-24 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            取消超链接
// @name:EN         Link2Text
// @namespace       https://github.com/zxk2099/Link2Text
// @version         1.2
// @description     长按<a>元素超链接取消超链接,并提供一个额外的新的按钮以恢复原超链接。
// @description:EN  A long press on the <a> element hyperlink cancels the hyperlink, with an additional new button to scroll back.
// @author          zxk2099
// @icon            https://github.com/zxk2099/Link2Text/blob/main/icon.png
// @supportURL      https://github.com/zxk2099/Link2Text/issues
// @match           *://*/*
// ==/UserScript==

(function () {

    // 长按时间(毫秒)
    const longPressTime = 1000;

    let pressTimer = null;
    let targetLink = null;
    const savedLinks = new Map();
    let linkCounter = 0;

    document.addEventListener('mousedown', (event) => {
        const linkElement = event.target.closest('a');
        if (linkElement) {
            event.preventDefault();
            targetLink = linkElement;
            pressTimer = setTimeout(() => {
                replaceLinkWithText(targetLink);
            }, longPressTime);
        }
    });

    document.addEventListener('mouseup', () => {
        if (pressTimer) {
            clearTimeout(pressTimer);
            pressTimer = null;
            targetLink = null;
        }
    });

    document.addEventListener('mouseleave', () => {
        if (pressTimer) {
            clearTimeout(pressTimer);
            pressTimer = null;
            targetLink = null;
        }
    });

    const replaceLinkWithText = (linkElement) => {
        const linkId = `link-${linkCounter++}`;
        const attributes = {};
        for (const attr of linkElement.attributes) {
            attributes[attr.name] = attr.value;
        }
        savedLinks.set(linkId, {
            attributes,
            innerHTML: linkElement.innerHTML,
        });
        const linkHTML = linkElement.innerHTML;

        const container = document.createElement('span');
        container.innerHTML = linkHTML;
        for (const [name, value] of Object.entries(attributes)) {
            if (name !== 'href' && name !== 'target') {
                container.setAttribute(name, value);
            }
        }
        container.style.position = 'relative';
        container.title = "恢复此处URL";

        // fix DOMListener in Text To Link 2.8.7 at https://update.greasyfork.icu/scripts/342/Text%20To%20link.user.js
        container.classList.add("textToLink");

        const restoreButton = document.createElement('div');
        restoreButton.className = 'restore-button';
        restoreButton.textContent = '↺';
        restoreButton.dataset.id = linkId;
        restoreButton.addEventListener('click', (event) => {
            event.stopPropagation(); 
            restoreLink(container, linkId); 
        });
        container.appendChild(restoreButton);
        linkElement.replaceWith(container);
    };

    const restoreLink = (container, linkId) => {
        const linkData = savedLinks.get(linkId);
        if (!linkData) return;
        const newLink = document.createElement('a');
        for (const [name, value] of Object.entries(linkData.attributes)) {
            newLink.setAttribute(name, value);
        }
        newLink.innerHTML = linkData.innerHTML;
        container.replaceWith(newLink);
        savedLinks.delete(linkId);
    };

    const style = document.createElement('style');
    style.textContent = `
        .restore-button {
            position: absolute;
            top: -10px;
            left: -8px;
            width: 16px;
            height: 16px;
            background-color: #e31111;
            color: white;
            border-radius: 50%;
            font-size: 12px;
            text-align: center;
            line-height: 16px;
            cursor: pointer;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        }
    `;
    document.head.appendChild(style);
})();