view disable-discord-menu-on-links.user.js @ 133:717bc604371b

[mandarake direct link] version bump
author nanaya <me@nanaya.net>
date Sun, 07 Jan 2024 19:20:55 +0900
parents 77c5ac0ae047
children
line wrap: on
line source

// ==UserScript==
// @name        Disable Discord Menu on Links
// @namespace   https://nanaya.net
// @match       https://discord.com/*
// @grant       none
// @version     1.0.0
// @author      nanaya
// @description Disable custom context menu for external links in Discord
// @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/disable-discord-menu-on-links.user.js
// ==/UserScript==

function disableCustomContextMenu (e) {
  e.stopPropagation();
}

function fix (link) {
  if (!(link instanceof window.HTMLAnchorElement)) return;
  if (link.rel.match(/\bnoopener\b/) == null && link.dataset.role !== 'img') return;

  if (link._ecDisableDiscordMenuOnLinks) return;
  link._ecDisableDiscordMenuOnLinks = true;
  link.addEventListener('contextmenu', disableCustomContextMenu);
}

function run (nodes) {
  nodes ??= [document.body];

  for (const node of nodes) {
    if (!(node instanceof window.HTMLElement)) continue;
    fix(node);
    for (const link of node.querySelectorAll('a[rel~=noopener], a[data-role=img]')) {
      fix(link);
    }
  }
}

function onMutate (mutations) {
  for (const mutation of mutations) {
    run(mutation.addedNodes);
  }
}

const observer = new window.MutationObserver(onMutate);
observer.observe(document, { childList: true, subtree: true });

run();