changeset 131:77c5ac0ae047

Add disabling discord menu on links
author nanaya <me@nanaya.net>
date Fri, 30 Jun 2023 20:28:27 +0900
parents 49658e99888a
children 8ccdc82249a2
files disable-discord-menu-on-links.user.js
diffstat 1 files changed, 46 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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();