微信公众号编辑器中英文自动加空格

在微信公众号编辑器中加入一个用于自动在中英文间添加空格的按钮

  1. // ==UserScript==
  2. // @name 微信公众号编辑器中英文自动加空格
  3. // @namespace https://coding.net/u/BackRunner/p/GreaseMonkey-JS/git
  4. // @version 2020.6
  5. // @description 在微信公众号编辑器中加入一个用于自动在中英文间添加空格的按钮
  6. // @author BackRunner
  7. // @include *mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit*
  8. // @include *mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit_v2*
  9. // @license MIT
  10. // @run-at document-end
  11. // @grant unsafeWindow
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. //Set
  16. var toolbar;
  17. var yiban;
  18. var count = 0;
  19. var retry = 0;
  20.  
  21. //Run
  22. $(document).ready(function(){
  23. setTimeout(function(){
  24. getToolbar();
  25. injectEditor();
  26. }, 5000);
  27. });
  28.  
  29. //Functions
  30. function getToolbar(){
  31. // 检查壹伴的工具栏
  32. yiban = document.getElementById('mpa-extra-tools-container');
  33. if (yiban){
  34. console.log(yiban);
  35. createBtnYiBan();
  36. return;
  37. }
  38. toolbar = document.getElementById('js_toolbar_0');
  39. while (typeof toolbar == 'undefined' && count < 10){
  40. count++;
  41. toolbar = document.getElementById('js_toolbar_0');
  42. }
  43. if (count < 5){
  44. createBtn();
  45. } else {
  46. if (retry < 5){
  47. setTimeout(function(){
  48. getToolbar();
  49. retry++;
  50. },3000);
  51. }
  52. }
  53. }
  54.  
  55. function createBtn(){
  56. var wrap = document.createElement("div");
  57. wrap.setAttribute("class","edui-box edui-button edui-default");
  58. var div = document.createElement("div");
  59. div.setAttribute("class","edui-box edui-button-body edui-default");
  60. div.setAttribute("data-mpa-tooltip","在中英文之间添加空格");
  61. var btn_name = document.createElement("div");
  62. btn_name.setAttribute("style","font-size:12px !important;");
  63. btn_name.innerHTML = "添加空格";
  64. wrap.appendChild(div);
  65. div.appendChild(btn_name);
  66. toolbar.appendChild(wrap);
  67. div.addEventListener('click', Event);
  68. }
  69.  
  70. function createBtnYiBan(){
  71. var wrap = document.createElement("div");
  72. wrap.setAttribute("class","edui-box edui-button edui-default");
  73. var div = document.createElement("div");
  74. div.setAttribute("class","edui-box edui-button-body edui-default");
  75. div.setAttribute("data-mpa-tooltip","在中英文之间添加空格");
  76. var btn_name = document.createElement("div");
  77. btn_name.setAttribute("style","font-size:12px !important;line-height: 34px;margin-right:8px;cursor:pointer;");
  78. btn_name.innerHTML = "添加空格";
  79. wrap.appendChild(div);
  80. div.appendChild(btn_name);
  81. var breakdiv = document.getElementById("br-breakdiv");
  82. if (typeof breakdiv == 'undefined' || breakdiv == null){
  83. breakdiv = document.createElement("div");
  84. breakdiv.setAttribute("id","br-breakdiv");
  85. yiban.appendChild(breakdiv);
  86. }
  87. yiban.appendChild(wrap);
  88. div.addEventListener('click', Event);
  89. }
  90.  
  91. function injectEditor() {
  92. let iframe = document.getElementById("ueditor_0");
  93. let script = document.createElement('script');
  94. script.setAttribute('src', 'https://static.backrunner.top/pangu-simple/1.0.2/pangu.min.js');
  95. iframe.contentDocument.head.append(script);
  96. }
  97.  
  98. function Event(){
  99. let iframe = document.getElementById("ueditor_0");
  100. iframe.contentDocument.body.setAttribute('contenteditable', false);
  101. iframe.contentWindow.pangu.spacingElementByTagName('p');
  102. iframe.contentWindow.pangu.spacingElementByTagName('span');
  103. iframe.contentDocument.body.setAttribute('contenteditable', true);
  104. }
  105. })();

QingJ © 2025

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