吾爱代码块支持

吾爱代码块隐藏/显示切换+md复制按钮支持

  1. // ==UserScript==
  2. // @name 吾爱代码块支持
  3. // @namespace wuai_copy
  4. // @version 0.1.0
  5. // @description 吾爱代码块隐藏/显示切换+md复制按钮支持
  6. // @author 涛之雨
  7. // @match https://www.52pojie.cn/*
  8. // @grant none
  9. // @note 吾爱代码块隐藏/显示切换+md复制按钮支持
  10. // @icon https://www.52pojie.cn/favicon.ico
  11. // @home https://greasyfork.org/zh-CN/scripts/416512
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. window.onscroll=function(){
  16. document.querySelectorAll(".viewsource").forEach((a)=>{
  17. var b=a.parentElement;
  18. if(b.lastChild.className=="hideCode"||b.lastChild.className=="showCode"){
  19. return;
  20. }
  21. var c = document.createElement('em');
  22. c.setAttribute("class","hideCode");
  23. c.style="cursor:pointer;font-size:12px;color:#369 !important;";
  24. c.innerHTML=" 隐藏代码";
  25. c.onclick=function(){
  26. var a=this;
  27. if(a.className=="hideCode"){
  28. a.parentElement.parentElement.lastChild.style.height="0";
  29. a.parentElement.parentElement.lastChild.style.overflow="hidden";
  30. a.setAttribute("class","showCode");
  31. a.innerHTML=" 显示代码";
  32. }else if(a.className=="showCode"){
  33. a.parentElement.parentElement.lastChild.style.height="";
  34. a.parentElement.parentElement.lastChild.style.overflow=""
  35. a.setAttribute("class","hideCode");
  36. a.innerHTML=" 隐藏代码";
  37. }
  38. };
  39. b.appendChild(c);
  40. });
  41. document.querySelectorAll("pre").forEach((a)=>{
  42. if(a.firstChild.className=="hideCode"||a.firstChild.className=="CopyMyCode"||a.firstChild.className=="showCode"){
  43. return;
  44. }else{
  45. var c = document.createElement('em');
  46. c.setAttribute("class","hideCode");
  47. c.style="cursor:pointer;font-size:12px;color:#369 !important;";
  48. c.innerHTML=" 隐藏代码";
  49. a.insertBefore(c,a.firstChild);
  50. c.onclick=function(){
  51. var a=this;
  52. if(a.className=="hideCode"){
  53. a.parentElement.lastChild.style.height="0";
  54. a.parentElement.lastChild.style.overflow="hidden";
  55. a.setAttribute("class","showCode");
  56. a.innerHTML=" 显示代码";
  57. }else if(a.className=="showCode"){
  58. a.parentElement.lastChild.style.height="";
  59. a.parentElement.lastChild.style.overflow="";
  60. a.setAttribute("class","hideCode");
  61. a.innerHTML=" 隐藏代码";
  62. }
  63. };
  64. c = document.createElement('em');
  65. c.setAttribute("class","CopyMyCode");
  66. c.style="cursor:pointer;font-size:12px;color:#369 !important;";
  67. c.innerHTML=" 复制代码";
  68. a.insertBefore(c,a.firstChild);
  69. c.onclick=function(){
  70. var container = this.parentElement.lastChild;
  71. var lines = container.childNodes;
  72. var code = [];
  73. for (var i = 0; i < lines.length; i++) {
  74. code.push(lines[i].innerText || lines[i].textContent);
  75. }
  76. code = code.join('');
  77. setCopy(code, '代码已复制到剪贴板');
  78. };
  79. }
  80. });
  81. }
  82. })();