Nitter Teleporter

Converts Twitter links to Nitter ones

  1. // ==UserScript==
  2. // @name Nitter Teleporter
  3. // @namespace lousando
  4. // @match https://*/*
  5. // @match http://*/*
  6. // @exclude-match https://nitter.net/*
  7. // @run-at document-idle
  8. // @version 1.0.4
  9. // @author lousando
  10. // @description Converts Twitter links to Nitter ones
  11. // @grant GM_getValue
  12. // @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@2
  13. // ==/UserScript==
  14.  
  15. // allow for overriding of Nitter instance
  16. const nitterDomain = GM_getValue("nitter_domain", "nitter.net");
  17.  
  18. VM.observe(document.body, () => {
  19. const twitterRegex = /https?:\/\/twitter\.com?/i;
  20. const shortTwitterRegex = /https?:\/\/t\.co/i;
  21.  
  22. // links
  23. Array.from(document.body.querySelectorAll("a[href]")).filter(link => {
  24. return twitterRegex.test(link.href);
  25. }).forEach(link => {
  26. const parsedURL = new URL(link.href);
  27. parsedURL.hostname = nitterDomain;
  28.  
  29. if (twitterRegex.test(link.innerText)) {
  30. link.innerText = link.innerText.replace(twitterRegex, nitterDomain);
  31. }
  32.  
  33. link.setAttribute("href", parsedURL.toString());
  34. });
  35.  
  36. Array.from(document.body.querySelectorAll("a[href]")).filter(link => {
  37. return shortTwitterRegex.test(link.href);
  38. }).forEach(link => {
  39. // expand link
  40. fetch(link.href).then(r => r.text())
  41. .then(response => {
  42. const domParser = new DOMParser();
  43. const doc = domparser.parseFromString(response, "text/xml")
  44.  
  45. const parsedURL = new URL(link.href);
  46. parsedURL.hostname = nitterDomain;
  47.  
  48.  
  49. if (shortTwitterRegex.test(link.innerText)) {
  50. link.innerText = link.innerText.replace(shortTwitterRegex, nitterDomain);
  51. }
  52.  
  53. link.setAttribute("href", parsedURL.toString());
  54. });
  55.  
  56. });
  57. });