TXTClickableLinks

Converts URLs in plain text files to clickable links.

  1. // ==UserScript==
  2. // @name TXTClickableLinks
  3. // @name:de TXTClickableLinks
  4. // @name:en TXTClickableLinks
  5. // @namespace sun/userscripts
  6. // @version 1.1.16
  7. // @description Converts URLs in plain text files to clickable links.
  8. // @description:de Konvertiert URLs in Textdateien zu anklickbaren Links.
  9. // @description:en Converts URLs in plain text files to clickable links.
  10. // @compatible chrome
  11. // @compatible edge
  12. // @compatible firefox
  13. // @compatible opera
  14. // @compatible safari
  15. // @homepageURL https://forgejo.sny.sh/sun/userscripts
  16. // @supportURL https://forgejo.sny.sh/sun/userscripts/issues
  17. // @contributionURL https://liberapay.com/sun
  18. // @contributionAmount €1.00
  19. // @author Sunny <sunny@sny.sh>
  20. // @include *://*/*
  21. // @match *://*/*
  22. // @run-at document-end
  23. // @inject-into auto
  24. // @grant none
  25. // @noframes
  26. // @icon https://forgejo.sny.sh/sun/userscripts/raw/branch/main/icons/TXTClickableLinks.png
  27. // @copyright 2021-present, Sunny (https://sny.sh/)
  28. // @license Hippocratic License; https://forgejo.sny.sh/sun/userscripts/src/branch/main/LICENSE.md
  29. // ==/UserScript==
  30.  
  31. (() => {
  32. // Regular Expression for URL validation
  33. //
  34. // Copyright (c) 2010-2018 Diego Perini (http://www.iport.it)
  35. //
  36. // Permission is hereby granted, free of charge, to any person
  37. // obtaining a copy of this software and associated documentation
  38. // files (the "Software"), to deal in the Software without
  39. // restriction, including without limitation the rights to use,
  40. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  41. // copies of the Software, and to permit persons to whom the
  42. // Software is furnished to do so, subject to the following
  43. // conditions:
  44. //
  45. // The above copyright notice and this permission notice shall be
  46. // included in all copies or substantial portions of the Software.
  47. const reWeburl =
  48. /((?:(?:(?:https?|ftp|file):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?)/gi;
  49.  
  50. if (
  51. (document.body.childElementCount === 1 &&
  52. document.body.getElementsByTagName("pre")[0].getAttribute("style") ===
  53. "word-wrap: break-word; white-space: pre-wrap;") || // Chrome
  54. document.getElementsByTagName("link")[0].getAttribute("href") ===
  55. "resource://content-accessible/plaintext.css" // Firefox
  56. ) {
  57. const pre = document.getElementsByTagName("pre")[0];
  58. pre.innerHTML = pre.innerHTML.replace(reWeburl, '<a href="$1">$1</a>');
  59. }
  60. })();