Enhancement Userscript for LIHKG

An Enhancement Userscript for LIHKG

当前为 2021-06-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Enhancement Userscript for LIHKG
  3. // @version 0.1
  4. // @description An Enhancement Userscript for LIHKG
  5. // @include /https?\:\/\/lihkg\.com/
  6. // @icon https://www.google.com/s2/favicons?domain=lihkg.com
  7. // @grant none
  8. // @namespace https://gf.qytechs.cn/users/371179
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. document.cssAll = function() {
  14.  
  15. var s = document.querySelectorAll.apply(this, arguments)
  16.  
  17. s = Array.prototype.slice.call(s, 0)
  18. return s
  19. }
  20.  
  21. function urlConvert(url) {
  22. var src = url.replace(/\w+\:\/\//, '')
  23. var replacements = [...src.matchAll(/[\w\.]+/g)].filter((t) => /\./.test(t))
  24. if (replacements.length > 1) {
  25. replacements.length--;
  26.  
  27. }
  28. replacements.forEach((s) => {
  29. src = src.replace(s, '')
  30. })
  31.  
  32. src = src.replace(/\/+/g, '/')
  33.  
  34. return src;
  35.  
  36. }
  37.  
  38. var emoji = {};
  39.  
  40. setInterval(() => {
  41.  
  42. document.cssAll('img[src*="lihkg.com"][alt]:not([title])').forEach(function(imgElm) {
  43. var src = imgElm.getAttribute('src');
  44. var erc = urlConvert(src)
  45. var imgAlt = imgElm.getAttribute('alt') || "";
  46. if (/^[\x20-\x7E]+$/.test(imgAlt) && /\#/.test(imgAlt)) {
  47. emoji[erc] = imgAlt.trim()
  48. }
  49.  
  50. imgElm.setAttribute('title', imgAlt)
  51.  
  52. })
  53.  
  54.  
  55. document.cssAll('a[href*="profile/"]:not([href*="//"]):not([title])').forEach(function(aElm) {
  56. aElm.setAttribute('title', aElm.getAttribute('href'))
  57. })
  58.  
  59. document.cssAll('[data-ic~="hkgmoji"]:not([title])>img[src*="lihkg.com"]:not([alt])').forEach(function(imgElm) {
  60. var src = imgElm.getAttribute('src');
  61. var erc = urlConvert(src)
  62. var text = emoji[erc] ? emoji[erc] : "[img]" + erc + "[/img]"
  63. imgElm.parentNode.setAttribute('title', text)
  64. imgElm.setAttribute('alt', text)
  65.  
  66.  
  67. })
  68.  
  69. document.cssAll('a[href*="local.lihkg.com"]>img:not([anchored])').forEach(function(img) {
  70. img.setAttribute('anchored', 'true')
  71.  
  72.  
  73. var originalSrc = img.getAttribute('src') || img.getAttribute('data-original') || ""
  74. var newSrc = originalSrc.replace('local.lihkg.com', 'cdn.lihkg.com');
  75.  
  76. if (newSrc && originalSrc != newSrc) {
  77.  
  78. console.log(originalSrc, newSrc)
  79.  
  80. var fx = function() {
  81. if (img.complete == false) return setTimeout(fx, 33);
  82.  
  83. if (img.currentSrc == "") {
  84.  
  85. var b = img.cloneNode(false);
  86. b.removeAttribute('data-original');
  87. b.removeAttribute('data-src');
  88.  
  89. if (b.getAttribute('alt') == "") b.removeAttribute('alt')
  90. if (b.getAttribute('title') == "") b.removeAttribute('title')
  91.  
  92. b.setAttribute('src', newSrc);
  93. img.parentNode.replaceChild(b, img)
  94.  
  95. if (b.parentNode.getAttribute('href')) {
  96. b.parentNode.setAttribute('href', b.parentNode.getAttribute('href').replace(originalSrc, newSrc));
  97. if (b.nextElementSibling && b.nextElementSibling.hasAttribute('data-error')) b.nextElementSibling.parentNode.removeChild(b.nextElementSibling);
  98. if (b.nextElementSibling && b.nextElementSibling.outerHTML.toLocaleLowerCase() == '<ins></ins>') b.nextElementSibling.parentNode.removeChild(b.nextElementSibling);
  99.  
  100.  
  101.  
  102. }
  103.  
  104. b.removeAttribute('anchored')
  105.  
  106.  
  107. }
  108.  
  109. }
  110. fx();
  111.  
  112.  
  113. }
  114.  
  115. })
  116.  
  117.  
  118. document.cssAll('div[contenteditable] p>br:not([hacked])').forEach((brElm) => {
  119. brElm.setAttribute('hacked', 'true')
  120. if (brElm.nextElementSibling === null && brElm.parentNode.nextElementSibling && brElm.parentNode.nextElementSibling.nodeType == 1 && brElm.parentNode.nextElementSibling.tagName == 'P') {
  121. brElm.style.display = 'none'
  122. // brElm.parentNode.removeChild(brElm)
  123. }
  124.  
  125. })
  126.  
  127.  
  128. }, 33)
  129.  
  130. // Your code here...
  131. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址