Text To link

Turn plain text URLs into clickable links

  1. // ==UserScript==
  2. // @name Text To link
  3. // @description Turn plain text URLs into clickable links
  4. // @description:zh 把文字链接转换为可点击链接
  5. // @author lkytal
  6. // @namespace Lkytal
  7. // @version 2.8.7
  8. // @homepage https://lkytal.github.io/
  9. // @homepageURL https://lkytal.github.io/GM
  10. // @license AGPL
  11. // @include *
  12. // @exclude *pan.baidu.com/*
  13. // @exclude *renren.com/*
  14. // @exclude *exhentai.org/*
  15. // @exclude *music.google.com/*
  16. // @exclude *play.google.com/music/*
  17. // @exclude *mail.google.com/*
  18. // @exclude *docs.google.com/*
  19. // @exclude *www.google.*
  20. // @exclude *acid3.acidtests.org/*
  21. // @exclude *https://greatest.deepsurf.us/*/scripts/*/code*
  22. // @exclude *.163.com/*
  23. // @exclude *.alipay.com/*
  24. // @run-at document-end
  25. // @icon https://github.com/lkytal/GM/raw/master/icons/link.png
  26. // @grant unsafeWindow
  27. // @charset UTF-8
  28. // @supportURL https://github.com/lkytal/GM/issues
  29. // ==/UserScript==
  30.  
  31. "use strict";
  32. ;
  33. var clearLink, excludedTags, linkFilter, linkMixInit, linkPack, linkify, observePage, observer, setLink, urlPrefixes, url_regexp, xPath
  34.  
  35. url_regexp = /((https?:\/\/|www\.)[\x21-\x7e]+[\w\/=]|\w([\w._-])+@\w[\w\._-]+\.(com|cn|org|net|info|tv|cc|gov|edu)|(\w[\w._-]+\.(com|cn|org|net|info|tv|cc|gov|edu))(\/[\x21-\x7e]*[\w\/])?|ed2k:\/\/[\x21-\x7e]+\|\/|thunder:\/\/[\x21-\x7e]+=)/gi
  36.  
  37. urlPrefixes = ['http://', 'https://', 'ftp://', 'thunder://', 'ed2k://', 'mailto://', 'file://']
  38.  
  39. clearLink = function (event) {
  40. var j, len, link, prefix, ref, ref1, url
  41. link = (ref = event.originalTarget) != null ? ref : event.target
  42. if (!(link != null && link.localName === "a" && ((ref1 = link.className) != null ? ref1.indexOf("textToLink") : void 0) !== -1)) {
  43. return
  44. }
  45. url = link.getAttribute("href")
  46. //console.log url
  47. for (j = 0, len = urlPrefixes.length; j < len; j++) {
  48. prefix = urlPrefixes[j]
  49. if (url.indexOf(prefix) === 0) {
  50. return
  51. }
  52. }
  53. if (url.indexOf('@') !== -1) {
  54. return link.setAttribute("href", "mailto://" + url)
  55. } else {
  56. return link.setAttribute("href", "http://" + url)
  57. }
  58. }
  59.  
  60. document.addEventListener("mouseover", clearLink)
  61.  
  62. setLink = function (candidate) {
  63. var ref, ref1, ref2, span, text
  64. if (candidate == null || ((ref = candidate.parentNode) != null ? (ref1 = ref.className) != null ? typeof ref1.indexOf === "function" ? ref1.indexOf("textToLink") : void 0 : void 0 : void 0) !== -1 || candidate.nodeName === "#cdata-section") {
  65. return
  66. }
  67. // text = candidate.textContent.replace(url_regexp, '<a href="$1" target="_blank" class="textToLink">$1</a>')
  68. text = candidate.textContent.replace(url_regexp, '<a href="$1" target="_blank" style="border: 1px dashed #000; padding: 2px; text-decoration: none;class="textToLink">$1</a>')
  69. if (((ref2 = candidate.textContent) != null ? ref2.length : void 0) === text.length) {
  70. return
  71. }
  72. span = document.createElement("span")
  73. span.innerHTML = text
  74. return candidate.parentNode.replaceChild(span, candidate)
  75. }
  76.  
  77. excludedTags = "a,svg,canvas,applet,input,button,area,pre,embed,frame,frameset,head,iframe,img,option,map,meta,noscript,object,script,style,textarea,code".split(",")
  78.  
  79. xPath = `//text()[not(ancestor::${excludedTags.join(') and not(ancestor::')})]`
  80.  
  81. linkPack = function (result, start) {
  82. var i, j, k, ref, ref1, ref2, ref3, startTime
  83. startTime = Date.now()
  84. while (start + 10000 < result.snapshotLength) {
  85. for (i = j = ref = start, ref1 = start + 10000; ref <= ref1 ? j <= ref1 : j >= ref1; i = ref <= ref1 ? ++j : --j) {
  86. setLink(result.snapshotItem(i))
  87. }
  88. start += 10000
  89. if (Date.now() - startTime > 2500) {
  90. return
  91. }
  92. }
  93. for (i = k = ref2 = start, ref3 = result.snapshotLength; ref2 <= ref3 ? k <= ref3 : k >= ref3; i = ref2 <= ref3 ? ++k : --k) {
  94. setLink(result.snapshotItem(i))
  95. }
  96. }
  97.  
  98. linkify = function (node) {
  99. var result
  100. result = document.evaluate(xPath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)
  101. return linkPack(result, 0)
  102. }
  103.  
  104. linkFilter = function (node) {
  105. var j, len, tag
  106. for (j = 0, len = excludedTags.length; j < len; j++) {
  107. tag = excludedTags[j]
  108. if (tag === node.parentNode.localName.toLowerCase()) {
  109. return NodeFilter.FILTER_REJECT
  110. }
  111. }
  112. return NodeFilter.FILTER_ACCEPT
  113. }
  114.  
  115. observePage = function (root) {
  116. var tW
  117. tW = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, { //+ NodeFilter.SHOW_ELEMENT,
  118. acceptNode: linkFilter
  119. }, false)
  120. while (tW.nextNode()) {
  121. setLink(tW.currentNode)
  122. }
  123. }
  124.  
  125. observer = new window.MutationObserver(function (mutations) {
  126. var Node, j, k, len, len1, mutation, ref
  127. for (j = 0, len = mutations.length; j < len; j++) {
  128. mutation = mutations[j]
  129. if (mutation.type === "childList") {
  130. ref = mutation.addedNodes
  131. for (k = 0, len1 = ref.length; k < len1; k++) {
  132. Node = ref[k]
  133. observePage(Node)
  134. }
  135. }
  136. }
  137. })
  138.  
  139. linkMixInit = function () {
  140. if (window !== window.top || window.document.title === "") {
  141. return
  142. }
  143. //console.time('a')
  144. linkify(document.body)
  145. //console.timeEnd('a')
  146. return observer.observe(document.body, {
  147. childList: true,
  148. subtree: true
  149. })
  150. }
  151.  
  152. setTimeout(linkMixInit, 100)