Greasy Fork

Greasy Fork is available in English.

取消超链接 Link2Text

长按<a>元素超链接取消超链接

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         取消超链接 Link2Text
// @namespace    link_to_text
// @version      1.1
// @description  长按<a>元素超链接取消超链接
// @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,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.style.position = 'relative';
        container.innerHTML = linkHTML;
        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: -8px;
            right: -8px;
            width: 16px;
            height: 16px;
            background-color: #ff0000;
            color: white;
            border-radius: 50%;
            font-size: 12px;
            text-align: center;
            line-height: 16px;
            cursor: pointer;
            box-shadow: 1px 1px 4px rgba(0, 0, 0, 0.2);
        }
    `;
    document.head.appendChild(style);
})();