英文前部加粗

网页英文前部加粗脚本

目前为 2022-05-19 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 英文前部加粗
  3. // @namespace https://github.com/itorr/bionic-reading.user.js
  4. // @version 0.5
  5. // @description 网页英文前部加粗脚本
  6. // @author itorr
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // @run-at document-idle
  11. // ==/UserScript==
  12.  
  13. const styleEl = document.createElement('style');
  14. styleEl.innerHTML = 'bbb{font-weight:bold;}';
  15.  
  16. let textEls = [];
  17. const excludeTagNames = ['script','style','xmp','input','textarea','pre','code'].map(a=>a.toUpperCase());
  18. const gather = el=>{
  19. el.childNodes.forEach(el=>{
  20. if(el.isEnB) return;
  21.  
  22. if(el.nodeType === 3){
  23. textEls.push(el);
  24. }else if(el.childNodes){
  25. if(excludeTagNames.includes(el.tagName)) return;
  26. gather(el)
  27. }
  28. })
  29. };
  30.  
  31. let body = document.body;
  32.  
  33.  
  34. if(/weibo/.test(location.hostname)){
  35. const wbMain = document.querySelector('.WB_main');
  36. if(wbMain){
  37. body = wbMain;
  38. }
  39. }
  40.  
  41. const customStyleEl = document.querySelector('#custom_style');
  42. if(customStyleEl)customStyleEl.removeAttribute('id');
  43.  
  44.  
  45. console.log({body})
  46.  
  47. const run = _=>{
  48. textEls = [];
  49. gather(body);
  50.  
  51. textEls.forEach(textEl=>{
  52. const text = textEl.data;
  53. if(!/[a-z][a-z0-9]+/i.test(text))return;
  54.  
  55. const spanEl = document.createElement('spann');
  56. spanEl.isEnB = true;
  57. spanEl.innerHTML = text.replace(/[a-z][a-z0-9]+/ig,word=>{
  58. const halfLength = Math.ceil(word.length/2);
  59. return '<bbb>'+word.substr(0,halfLength)+'</bbb>'+word.substr(halfLength)
  60. })
  61. textEl.after(spanEl);
  62. textEl.remove();
  63. });
  64. document.head.appendChild(styleEl);
  65. }
  66. run();
  67. const _run = ms=> _=>setTimeout(run,ms);
  68.  
  69. const {open,send} = XMLHttpRequest.prototype;
  70.  
  71. XMLHttpRequest.prototype.open = function(){
  72. this.addEventListener('load',_run(200));
  73. return open.apply(this,arguments);
  74. };
  75.  
  76. document.addEventListener('click',_run(250));
  77. window.addEventListener('load',_run(200));
  78. document.addEventListener("DOMContentLoaded",_run(200));
  79.