Delink on Touch

Stop showing context menu when long-pressing a link, enable text selection.

目前為 2024-11-07 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name Delink on Touch
// @version 0.1.1
// @description Stop showing context menu when long-pressing a link, enable text selection.
// @homepageURL https://github.com/eight04/delink-on-touch
// @supportURL https://github.com/eight04/delink-on-touch/issues
// @license MIT
// @author eight04 <[email protected]>
// @namespace https://github.com/eight04
// @match *://*/*
// @grant GM_registerMenuCommand
// ==/UserScript==

/* global GM_registerMenuCommand */

let id;

register();

function register() {
  id = GM_registerMenuCommand("Enable", init, {id});
}

function init() {
  const links = new Map();
  GM_registerMenuCommand("Disable", uninit, {id});

  document.addEventListener("touchstart", onTouchStart);
  document.addEventListener("touchend", onTouchEnd);

  let timeoutId;

  function onTouchStart(e) {
    clearTimeout(timeoutId);
    // add a delay so users can still tap the link without triggering this
    timeoutId = setTimeout(() => {
      const a = e.target.closest("a");
      if (a && a.getAttribute("href") && e.target.textContent) {
        links.set(a, a.href);
        a.href = "";
      }
    }, 150);
  }

  function onTouchEnd() {
    clearTimeout(timeoutId)
    for (const [a, href] of links) {
      a.href = href;
    }
    links.clear();
  }

  function uninit() {
    register();
    document.removeEventListener("touchstart", onTouchStart);
    document.removeEventListener("touchend", onTouchEnd)
  }
}