// ==UserScript== // @name Blogger HTML Editor Code Prettify 添加器 // @namespace http://tampermonkey.net/ // @version 0.1 // @description 在 Blogger 背景HTML 编辑器中添加代码美化功能 // @author moran // @match https://www.blogger.com/blog/themes/* // @grant none // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 创建一个按钮,用于插入代码 const button = document.createElement('button'); button.textContent = '插入 Code Prettify'; button.style.position = 'fixed'; button.style.top = '50px'; // 调整到顶部50px button.style.right = '10px'; button.style.zIndex = '9999'; button.style.padding = '8px 16px'; button.style.backgroundColor = '#2196F3'; // 改为蓝色 button.style.color = 'white'; button.style.border = 'none'; button.style.borderRadius = '4px'; button.style.cursor = 'pointer'; // 要插入的 HTML 代码 const codeToInsert = ` `; const scriptToInsert = ``; // 监听HTML编辑器的出现 const observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (document.querySelector('.CodeMirror')) { observer.disconnect(); document.body.appendChild(button); } }); }); observer.observe(document.body, { childList: true, subtree: true }); // 按钮点击事件 button.addEventListener('click', function() { const editor = document.querySelector('.CodeMirror').CodeMirror; if (editor) { const currentContent = editor.getValue(); // 检查是否已经插入了代码 if (currentContent.includes('code-prettify@master')) { alert('代码已经插入过了!'); return; } // 找到合适的插入位置 let headStart = currentContent.indexOf('
'); let headEnd = currentContent.indexOf(''); if (headStart === -1 || headEnd === -1) { alert('未找到 head 标签,请检查 HTML 结构'); return; } // 在head标签内的最后插入代码 let newContent = currentContent.slice(0, headEnd) + '\n' + codeToInsert + '\n' + currentContent.slice(headEnd); // 在