Linkify Plain Text URLs

Turn plain text URLs into clickable links

当前为 2019-06-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Linkify Plain Text URLs
  3. // @version 1.0.0
  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. // ==/UserScript==
  11.  
  12. /* dunno who wrote this. I just converted a bookmarklet to a userscript. */
  13.  
  14. (function () {
  15.  
  16. function linkifyPlainText() {
  17.  
  18. document.body.normalize();
  19.  
  20. function linkifyNode(n) {
  21. var M, R, currentNode;
  22. if (n.nodeType == 3) {
  23. const urlPosition = n.data.search(/https?:\/\/[^\s]*/);
  24.  
  25. if (urlPosition < 0) return;
  26.  
  27. M = n.splitText(urlPosition);
  28. R = M.splitText(RegExp.lastMatch.length);
  29. const linkTag = document.createElement("A");
  30. linkTag.href = M.data;
  31. linkTag.appendChild(M);
  32. R.parentNode.insertBefore(linkTag, R);
  33.  
  34. } else if (n.tagName != "STYLE" && n.tagName != "SCRIPT" && n.tagName != "A")
  35.  
  36. for (let i = 0; currentNode = n.childNodes[i]; ++i) {
  37. linkifyNode(currentNode);
  38. }
  39. }
  40.  
  41. linkifyNode(document.body);
  42.  
  43. }
  44.  
  45. GM_registerMenuCommand('Linkify Plain Text URLs', linkifyPlainText);
  46.  
  47. })();