Blogger HTML Editor Code Prettify 添加器

在 Blogger 背景HTML 编辑器中添加代码美化功能

目前为 2025-02-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Blogger HTML Editor Code Prettify 添加器
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 在 Blogger 背景HTML 编辑器中添加代码美化功能
  6. // @author moran
  7. // @match https://www.blogger.com/blog/themes/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 创建一个按钮,用于插入代码
  16. const button = document.createElement('button');
  17. button.textContent = '插入 Code Prettify';
  18. button.style.position = 'fixed';
  19. button.style.top = '50px'; // 调整到顶部50px
  20. button.style.right = '10px';
  21. button.style.zIndex = '9999';
  22. button.style.padding = '8px 16px';
  23. button.style.backgroundColor = '#2196F3'; // 改为蓝色
  24. button.style.color = 'white';
  25. button.style.border = 'none';
  26. button.style.borderRadius = '4px';
  27. button.style.cursor = 'pointer';
  28.  
  29. // 要插入的 HTML 代码
  30. const codeToInsert = `<!-- Code Prettify Resources -->
  31. <script src='https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js'></script>
  32. <link href='https://cdn.jsdelivr.net/gh/google/code-prettify@master/styles/sunburst.css' rel='stylesheet' />
  33. <style type='text/css'>
  34. .copy-code-button {
  35. background-color: #f1f1f1;
  36. border: 1px solid #ccc;
  37. border-radius: 4px;
  38. color: #333;
  39. cursor: pointer;
  40. font-size: 12px;
  41. margin: 5px 0 0;
  42. padding: 3px 8px;
  43. position: absolute;
  44. right: 10px;
  45. top: 10px;
  46. }
  47. .copy-code-button:hover {
  48. background-color: #ddd;
  49. }
  50. .prettyprint {
  51. position: relative;
  52. padding: 20px 10px 10px;
  53. }</style>`;
  54.  
  55. const scriptToInsert = `<script>
  56. document.addEventListener('DOMContentLoaded', function() {
  57. var prettyPrintBlocks = document.querySelectorAll('.prettyprint');
  58. prettyPrintBlocks.forEach(function(block) {
  59. // 为每个代码块添加复制按钮
  60. var copyButton = document.createElement('button');
  61. copyButton.className = 'copy-code-button';
  62. copyButton.textContent = '复制代码';
  63. block.appendChild(copyButton);
  64. });
  65.  
  66. var copyButtons = document.querySelectorAll('.prettyprint .copy-code-button');
  67. copyButtons.forEach(function(button) {
  68. button.addEventListener('click', function() {
  69. var codeBlock = this.parentNode.querySelector('code');
  70. var codeText = codeBlock.textContent;
  71. var textarea = document.createElement('textarea');
  72. textarea.value = codeText;
  73. textarea.setAttribute('readonly', '');
  74. textarea.style.position = 'absolute';
  75. textarea.style.left = '-9999px';
  76. document.body.appendChild(textarea);
  77. textarea.select();
  78. document.execCommand('copy');
  79. document.body.removeChild(textarea);
  80. var originalText = this.textContent;
  81. this.textContent = '已复制!';
  82. setTimeout(function() {
  83. button.textContent = originalText;
  84. }, 2000);
  85. });
  86. });
  87. });</script>`;
  88.  
  89. // 监听HTML编辑器的出现
  90. const observer = new MutationObserver(function(mutations) {
  91. mutations.forEach(function(mutation) {
  92. if (document.querySelector('.CodeMirror')) {
  93. observer.disconnect();
  94. document.body.appendChild(button);
  95. }
  96. });
  97. });
  98.  
  99. observer.observe(document.body, {
  100. childList: true,
  101. subtree: true
  102. });
  103.  
  104. // 按钮点击事件
  105. button.addEventListener('click', function() {
  106. const editor = document.querySelector('.CodeMirror').CodeMirror;
  107. if (editor) {
  108. const currentContent = editor.getValue();
  109. // 检查是否已经插入了代码
  110. if (currentContent.includes('code-prettify@master')) {
  111. alert('代码已经插入过了!');
  112. return;
  113. }
  114.  
  115. // 找到合适的插入位置
  116. let headStart = currentContent.indexOf('<head>');
  117. let headEnd = currentContent.indexOf('</head>');
  118. if (headStart === -1 || headEnd === -1) {
  119. alert('未找到 head 标签,请检查 HTML 结构');
  120. return;
  121. }
  122. // 在head标签内的最后插入代码
  123. let newContent = currentContent.slice(0, headEnd) +
  124. '\n' + codeToInsert + '\n' +
  125. currentContent.slice(headEnd);
  126. // 在</body>前插入复制功能脚本
  127. newContent = newContent.replace('</body>', scriptToInsert + '\n</body>');
  128. // 更新编辑器内容
  129. editor.setValue(newContent);
  130. // 提示成功
  131. alert('代码已成功插入!');
  132. } else {
  133. alert('未找到编辑器!请确保你在HTML编辑模式。');
  134. }
  135. });
  136. })();