add Code

微信公众号编辑图文消息在文章末尾增加代码段

  1. // ==UserScript==
  2. // @name add Code
  3. // @namespace Violentmonkey Scripts
  4. // @match https://mp.weixin.qq.com/cgi-bin/appmsg
  5. // @grant none
  6. // @version 1.0
  7. // @author jun95585
  8. // @description 微信公众号编辑图文消息在文章末尾增加代码段
  9. // ==/UserScript==
  10.  
  11. window.onload = function(){
  12. var addCodeBtnHtml = '<div style="position: fixed; z-index: 999; bottom: 150px;left: 0;padding: 10px; background: #fff; box-shadow: 0 1px 4px 0 rgba(0,0,0,0.2); border-radius: 0 4px 4px 0;"><span id="addCode" class="btn btn_input btn_default r no_extra"><button type="button" onclick="showCodeDialog()">添加代码</button></span></div>'
  13. var addCodeDialogHtml = `
  14. <div
  15. id="codeDialog"
  16. style="
  17. display: none;
  18. position:fixed;
  19. top: 0;
  20. left: 0;
  21. width: 100vw;
  22. height: 100vh;
  23. background-color: rgba(0,0,0,0.5);
  24. z-index: 999;
  25. ">
  26. <div
  27. style="
  28. display: flex;
  29. flex-direction: column;
  30. margin: auto;
  31. width: 80%;
  32. height: 200px;
  33. font-size: 14px;
  34. padding: 8px 0;
  35. background: #fff;
  36. box-shadow: 0 1px 4px 0 rgba(0,0,0,0.1);
  37. border-radius: 4px;
  38. "
  39. >
  40. <textarea
  41. id="codeText"
  42. rows=6
  43. placeholder="请输入代码段"
  44. style="
  45. margin: auto;
  46. width: 80%;
  47. font-size: 14px;
  48. padding: 8px;
  49. background: #fff;
  50. border: 1px solid rgba(208, 208, 208, 0.9);
  51. border-radius: 0 4px 4px 0;
  52. "
  53. ></textarea>
  54. <div style="display:flex; margin: auto;">
  55. <div style="margin-right: 20px;" class="btn btn_input btn_default r no_extra"><button id="addCodeDialogSubmit" type="button">添加至末尾</button></div>
  56. <div class="btn btn_input btn_default r no_extra"><button id="addCodeDialogClose" type="button">取消</button></div>
  57. </div>
  58. </div>
  59. </div>`
  60.  
  61. // 图文编辑页面
  62. if (window.location.search.substring(1).includes(action=edit)) {
  63. document.body.innerHTML = document.body.innerHTML + (addCodeBtnHtml + addCodeDialogHtml)
  64. }
  65. var showCodeDialog = function showCodeDialog () {
  66. document.getElementById('codeDialog').style.display = 'flex';
  67. }
  68.  
  69. var closeCodeDialog = function closeCodeDialog () {
  70. document.getElementById('codeText').innerHTML = '';
  71. document.getElementById('codeDialog').style.display = 'none';
  72. }
  73. document.getElementById('addCode').onclick = function(e){
  74. showCodeDialog();
  75. e.stopPropagation()
  76. };
  77. document.getElementById('addCodeDialogSubmit').onclick = function(e){
  78. var codeText = document.getElementById('codeText').value
  79. var editorDom = document.getElementById('ueditor_0')
  80. editorDom.contentWindow.document.getElementsByClassName('view')[0].innerHTML =
  81. editorDom.contentWindow.document.getElementsByClassName('view')[0].innerHTML +
  82. `<section>${codeText}</section>`
  83. closeCodeDialog();
  84. e.stoppropagation()
  85. };
  86. document.getElementById('addCodeDialogClose').onclick = function(e){
  87. closeCodeDialog();
  88. e.stoppropagation()
  89. };
  90. }

QingJ © 2025

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