()
| 132 | * 检测页面中的代码块并添加"解释代码"按钮 |
| 133 | */ |
| 134 | export function addExplainButtonsToCodeBlocks() { |
| 135 | const codeBlocks = document.querySelectorAll("pre code") |
| 136 | |
| 137 | codeBlocks.forEach((block) => { |
| 138 | // 避免重复添加按钮 |
| 139 | if (block.parentElement?.querySelector(".codebox-explain-btn")) { |
| 140 | return |
| 141 | } |
| 142 | |
| 143 | const button = document.createElement("button") |
| 144 | button.textContent = "💬 解释代码" |
| 145 | button.className = "codebox-explain-btn" |
| 146 | button.style.cssText = ` |
| 147 | position: absolute; |
| 148 | top: 8px; |
| 149 | right: 8px; |
| 150 | padding: 4px 8px; |
| 151 | background: #4CAF50; |
| 152 | color: white; |
| 153 | border: none; |
| 154 | border-radius: 4px; |
| 155 | cursor: pointer; |
| 156 | font-size: 12px; |
| 157 | z-index: 1000; |
| 158 | ` |
| 159 | |
| 160 | button.onclick = async () => { |
| 161 | const code = block.textContent || "" |
| 162 | const language = block.className.replace("language-", "") |
| 163 | await explainCode(code, language) |
| 164 | } |
| 165 | |
| 166 | // 确保 pre 元素有相对定位 |
| 167 | if (block.parentElement) { |
| 168 | block.parentElement.style.position = "relative" |
| 169 | block.parentElement.insertBefore(button, block) |
| 170 | } |
| 171 | }) |
| 172 | } |
nothing calls this directly
no test coverage detected