英文前部加粗

网页英文前部加粗脚本

当前为 2022-05-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 英文前部加粗
  3. // @namespace https://github.com/itorr/bionic-reading.user.js
  4. // @version 0.8.1
  5. // @description 网页英文前部加粗脚本
  6. // @author itorr
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // @run-at document-idle
  11. // @supportURL https://github.com/itorr/bionic-reading.user.js/issues
  12. // ==/UserScript==
  13.  
  14. const enCodeHTML = s=> s.replace(/[\u00A0-\u9999<>\&]/g,w=>'&#'+w.charCodeAt(0)+';');
  15.  
  16. let body = document.body;
  17.  
  18. if(/weibo/.test(location.hostname)){
  19. const wbMainEl = document.querySelector('.WB_main');
  20. if(wbMainEl) body = wbMainEl;
  21.  
  22. // 修复旧版微博自定义样式失效 bug
  23. const customStyleEl = document.querySelector('#custom_style');
  24. if(customStyleEl)customStyleEl.removeAttribute('id');
  25. }
  26.  
  27. const styleEl = document.createElement('style');
  28. styleEl.innerHTML = `
  29. bbb{
  30. font-weight:bold;
  31. }
  32. a spann{
  33. pointer-events: none;
  34. }
  35. `;
  36.  
  37. const excludeTagNames = [
  38. 'script','style','xmp',
  39. 'input','textarea','select',
  40. 'pre','code',
  41. 'h1','h2','h3','h4',
  42. 'b','strong'
  43. ].map(a=>a.toUpperCase());
  44.  
  45. const gather = el=>{
  46. let textEls = [];
  47. el.childNodes.forEach(el=>{
  48. if(el.isEnB) return;
  49.  
  50. if(el.nodeType === 3){
  51. textEls.push(el);
  52. }else if(el.childNodes){
  53. if(excludeTagNames.includes(el.tagName)) return;
  54.  
  55. // Skip DIV Code Frame
  56. if(el.getAttribute('class').includes('highlight')) return;
  57. textEls = textEls.concat(gather(el))
  58. }
  59. })
  60. return textEls;
  61. };
  62.  
  63. const engRegexi = /[a-z][a-z0-9]+/i;
  64. const engRegexig = /[a-z][a-z0-9]+/ig;
  65. let replaceTextByEl = el=>{
  66. const text = el.data;
  67. if(!engRegexi.test(text))return;
  68.  
  69. const spanEl = document.createElement('spann');
  70. spanEl.isEnB = true;
  71. spanEl.innerHTML = enCodeHTML(text).replace(engRegexig,word=>{
  72. let halfLength;
  73. if(/ing$/.test(word)){
  74. halfLength = word.length - 3;
  75. }else if(word.length<5){
  76. halfLength = Math.floor(word.length/2);
  77. }else{
  78. halfLength = Math.ceil(word.length/2);
  79. }
  80.  
  81. return '<bbb>'+word.substr(0,halfLength)+'</bbb>'+word.substr(halfLength)
  82. })
  83. el.after(spanEl);
  84. el.remove();
  85. };
  86.  
  87. // replaceTextByEl = el=>{
  88. // el.data = el.data.replace(engRegexig,word=>{
  89. // let halfLength;
  90. // if(/ing$/.test(word)){
  91. // halfLength = word.length - 3;
  92. // }else if(word.length<5){
  93. // halfLength = Math.floor(word.length/2);
  94. // }else{
  95. // halfLength = Math.ceil(word.length/2);
  96. // }
  97. // const a = word.substr(0,halfLength).
  98. // replace(/[a-z]/g,w=>'\uD835' + String.fromCharCode(w.charCodeAt(0)+56717)).
  99. // replace(/[A-Z]/g,w=>'\uD835' + String.fromCharCode(w.charCodeAt(0)+56723));
  100. // const b = word.substr(halfLength).
  101. // replace(/[a-z]/g,w=> String.fromCharCode(55349,w.charCodeAt(0)+56665)).
  102. // replace(/[A-Z]/g,w=> String.fromCharCode(55349,w.charCodeAt(0)+56671));
  103. // return a + b;
  104. // })
  105. // }
  106.  
  107. const bionic = _=>{
  108. const textEls = gather(body);
  109.  
  110. textEls.forEach(replaceTextByEl);
  111. document.head.appendChild(styleEl);
  112. }
  113.  
  114. const lazy = (func,ms = 0)=> {
  115. return _=>{
  116. clearTimeout(func.T)
  117. func.T = setTimeout(func,ms)
  118. }
  119. };
  120. lazy(bionic)();
  121.  
  122. if(window.ResizeObserver){
  123. (new ResizeObserver(lazy(bionic,100))).observe(body);
  124. }else{
  125. const {open,send} = XMLHttpRequest.prototype;
  126. XMLHttpRequest.prototype.open = function(){
  127. this.addEventListener('load',lazy(bionic));
  128. return open.apply(this,arguments);
  129. };
  130. document.addEventListener('click',lazy(bionic));
  131.  
  132. window.addEventListener('load',lazy(bionic));
  133. document.addEventListener("DOMContentLoaded",lazy(bionic));
  134. }