annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
1 // ==UserScript==
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
2 // @name Disable Discord Menu on Links
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
3 // @namespace https://nanaya.net
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
4 // @match https://discord.com/*
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
5 // @grant none
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
6 // @version 1.0.0
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
7 // @author nanaya
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
8 // @description Disable custom context menu for external links in Discord
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
9 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/disable-discord-menu-on-links.user.js
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
10 // ==/UserScript==
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
11
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
12 function disableCustomContextMenu (e) {
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
13 e.stopPropagation();
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
14 }
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
15
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
16 function fix (link) {
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
17 if (!(link instanceof window.HTMLAnchorElement)) return;
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
18 if (link.rel.match(/\bnoopener\b/) == null && link.dataset.role !== 'img') return;
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
19
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
20 if (link._ecDisableDiscordMenuOnLinks) return;
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
21 link._ecDisableDiscordMenuOnLinks = true;
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
22 link.addEventListener('contextmenu', disableCustomContextMenu);
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
23 }
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
24
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
25 function run (nodes) {
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
26 nodes ??= [document.body];
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
27
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
28 for (const node of nodes) {
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
29 if (!(node instanceof window.HTMLElement)) continue;
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
30 fix(node);
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
31 for (const link of node.querySelectorAll('a[rel~=noopener], a[data-role=img]')) {
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
32 fix(link);
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
33 }
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
34 }
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
35 }
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
36
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
37 function onMutate (mutations) {
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
38 for (const mutation of mutations) {
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
39 run(mutation.addedNodes);
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
40 }
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
41 }
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
42
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
43 const observer = new window.MutationObserver(onMutate);
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
44 observer.observe(document, { childList: true, subtree: true });
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
45
77c5ac0ae047 Add disabling discord menu on links
nanaya <me@nanaya.net>
parents:
diff changeset
46 run();