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