Base64 link decoder

Replaces any base64 encoded link in a <code> block as that is the normal format for links on the instance

  1. // ==UserScript==
  2. // @name Base64 link decoder
  3. // @namespace Violentmonkey Scripts
  4. // @match *://lemmy.dbzer0.com/*
  5. // @grant none
  6. // @version 1.0
  7. // @license MIT
  8. // @author sweepline
  9. // @description Replaces any base64 encoded link in a <code> block as that is the normal format for links on the instance
  10. // ==/UserScript==
  11.  
  12. function decodeURLS() {
  13. while (true) {
  14. // We must do FIRST_ORDERED_NODE_TYPE runs many times as the iterator dies in NODE_ITERATOR_TYPE if the dom changes.
  15. let xpath = document.evaluate("//code[contains(text(),'aHR0c')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  16. if (xpath.singleNodeValue == null) {
  17. break;
  18. }
  19. const decoded = atob(xpath.singleNodeValue.innerText)
  20. xpath.singleNodeValue.innerHTML = `<a href="${decoded}">${decoded}</a>`;
  21. }
  22. }
  23.  
  24. // First load replace
  25. window.addEventListener('load', decodeURLS, {once: true, capture: false});
  26.  
  27. // For listening on navigation
  28. const observeUrlChange = () => {
  29. let oldHref = document.location.href;
  30. const body = document.querySelector("body");
  31. const observer = new MutationObserver(mutations => {
  32. if (oldHref !== document.location.href) {
  33. oldHref = document.location.href;
  34. setTimeout(decodeURLS, 300);
  35. }
  36. });
  37. observer.observe(body, { childList: true, subtree: true });
  38. };
  39.  
  40. window.onload = observeUrlChange;