view disable-discord-menu-on-links.user.js @ 131:77c5ac0ae047

Add disabling discord menu on links
author nanaya <me@nanaya.net>
date Fri, 30 Jun 2023 20:28:27 +0900
parents
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();