# HG changeset patch # User nanaya # Date 1688124507 -32400 # Node ID 77c5ac0ae0479ec36eb53be41305cc7fa191d848 # Parent 49658e99888a226cf1d61467674a2be57117012b Add disabling discord menu on links diff -r 49658e99888a -r 77c5ac0ae047 disable-discord-menu-on-links.user.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/disable-discord-menu-on-links.user.js Fri Jun 30 20:28:27 2023 +0900 @@ -0,0 +1,46 @@ +// ==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();