Disable All Links

Disables all links on a webpage and monitors for new links.

  1. // ==UserScript==
  2. // @name Disable All Links
  3. // @namespace https://github.com/ghoulatsch
  4. // @version 240923
  5. // @description Disables all links on a webpage and monitors for new links.
  6. // @author Perplexity AI
  7. // @icon https://www.iconsdb.com/icons/preview/black/shield-xxl.png
  8. // @match *://*/*
  9. // @run-at document-end
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Function to disable a link
  17. function disableLink(link) {
  18. link.classList.add('disabled');
  19. link.removeAttribute('href');
  20. link.setAttribute('aria-disabled', 'true');
  21. link.style.pointerEvents = 'none';
  22. link.style.cursor = 'not-allowed';
  23. }
  24.  
  25. // Function to disable all existing links
  26. function disableAllLinks() {
  27. document.querySelectorAll('a').forEach(link => {
  28. disableLink(link);
  29. });
  30. }
  31.  
  32. // Function to observe mutations in the DOM
  33. function observeNewLinks() {
  34. const observer = new MutationObserver(mutations => {
  35. mutations.forEach(mutation => {
  36. mutation.addedNodes.forEach(node => {
  37. if (node.nodeType === 1) { // Check if it's an element node
  38. const links = node.querySelectorAll('a');
  39. links.forEach(link => {
  40. disableLink(link);
  41. });
  42. }
  43. });
  44. });
  45. });
  46.  
  47. observer.observe(document.body, {
  48. childList: true,
  49. subtree: true
  50. });
  51. }
  52.  
  53. // Initial setup
  54. disableAllLinks();
  55. observeNewLinks();
  56. })();