Linkify Plain Text URLs

Turn plain text URLs into clickable links

当前为 2022-11-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Linkify Plain Text URLs
  3. // @version 1.0.1
  4. // @author salad: https://greasyfork.org/en/users/241444-salad
  5. // @description Turn plain text URLs into clickable links
  6. // @namespace https://greasyfork.org/users/241444
  7. // @include *
  8. // @run-at document-idle
  9. // @grant GM.registerMenuCommand
  10. // @license GPL-3.0-only
  11. // ==/UserScript==
  12.  
  13. /* dunno who wrote this. I just converted a bookmarklet to a userscript. */
  14.  
  15. (function () {
  16.  
  17. function linkifyPlainText() {
  18.  
  19. document.body.normalize();
  20.  
  21. function linkifyNode(n) {
  22. var M, R, currentNode;
  23. if (n.nodeType == 3) {
  24. const urlPosition = n.data.search(/https?:\/\/[^\s]*/);
  25.  
  26. if (urlPosition < 0) return;
  27.  
  28. M = n.splitText(urlPosition);
  29. R = M.splitText(RegExp.lastMatch.length);
  30. const linkTag = document.createElement("A");
  31. linkTag.href = M.data;
  32. linkTag.appendChild(M);
  33. R.parentNode.insertBefore(linkTag, R);
  34.  
  35. } else if (n.tagName != "STYLE" && n.tagName != "SCRIPT" && n.tagName != "A")
  36.  
  37. for (let i = 0; currentNode = n.childNodes[i]; ++i) {
  38. linkifyNode(currentNode);
  39. }
  40. }
  41.  
  42. linkifyNode(document.body);
  43.  
  44. }
  45.  
  46. GM.registerMenuCommand('Linkify Plain Text URLs', linkifyPlainText);
  47.  
  48. })();