Greasy Fork is available in English.

Wechat Text Link to Hyperlink

Wechat Text Link to Hyperlink,Make links Clickable.

  1. // ==UserScript==
  2. // @name Wechat Text Link to Hyperlink
  3. // @name:zh-CN 微信公众号文本地址转超链接
  4. // @description Wechat Text Link to Hyperlink,Make links Clickable.
  5. // @description:zh-CN 微信公众号文本地址转超链接,让链接变得可点击。
  6. // @namespace https://www.runningcheese.com
  7. // @version 0.1
  8. // @author RunningCheese
  9. // @match https://mp.weixin.qq.com/s/*
  10. // @match https://mp.weixin.qq.com/s?__biz=*
  11. // @run-at document-start
  12. // @icon https://t1.gstatic.cn/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://mp.weixin.qq.com
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16.  
  17. const formatLimit = 5;
  18. const formatList = new WeakMap();
  19. const reg = /\b((?:https?:\/\/)?(?![\d-]+\.[\d-]+)([\w-]+(\.[\w-]+)+[\S^\"\'\[\]\{\}\>\<]*))/gi;
  20. const ignore = ['SCRIPT', 'STYLE', 'A', 'TEXTAREA', 'NOSCRIPT', 'CODE', 'TITLE'];
  21.  
  22. QueryElement(document);
  23. let obs = new MutationObserver(m => {
  24. m.forEach(mm => {
  25. FormatHref(mm.target, mm.addedNodes)
  26. mm.addedNodes.forEach(i => QueryElement(i))
  27. })
  28. });
  29. obs.observe(document, { subtree: true, childList: true });
  30. function QueryElement(element) {
  31. [...(element.querySelectorAll?.("*") ?? [])].forEach(i => FormatHref(i, i.childNodes))}
  32. function FormatHref(target, childNodes) {
  33. if (ignore.find(n => n == target.nodeName) || target.translate == false) return
  34. let formatTimes = formatList.get(target) || 0
  35. if (formatTimes > formatLimit) return
  36. let mark = false;
  37. [...childNodes].forEach(c => {
  38. if (c.nodeName == '#text' && c.textContent.match(reg)) {
  39. console.log(target, c.textContent)
  40. c.textContent = c.textContent.replace(reg, (m) => { return `<a href='${m}' target='_blank'>${m}</a>` })
  41. mark = true
  42. }
  43. })
  44. if (mark) {
  45. //console.log(target,target.nodeName, formatTimes)
  46. formatList.set(target, formatTimes + 1)
  47. target.innerHTML = target.innerHTML.replace(/&lt;a /g, "<a ").replace(/&lt;\/a&gt;/g, "</a>").replace(/' target='_blank'&gt;/g, "' target='_blank'>")
  48. }
  49. }