|
10 | 10 | return btoa(binary); |
11 | 11 | } |
12 | 12 |
|
13 | | - function findCppCodeBlocks() { |
| 13 | + function findCppCodeBlocks(root) { |
14 | 14 | const selectors = [ |
15 | 15 | "pre > code.language-cpp", |
16 | 16 | "pre > code.language-c\\+\\+", |
17 | 17 | "pre > code.lang-cpp", |
18 | 18 | "pre > code.lang-c\\+\\+", |
19 | 19 | ]; |
20 | | - return Array.from(document.querySelectorAll(selectors.join(","))); |
21 | | - } |
22 | | - |
23 | | - function codeBlockHasMain(code) { |
24 | | - return /\bmain\s*\(/.test(code); |
25 | | - } |
26 | | - |
27 | | - function getPreElement(codeEl) { |
28 | | - if (!codeEl) return null; |
29 | | - const pre = codeEl.closest("pre"); |
30 | | - return pre instanceof HTMLElement ? pre : null; |
31 | | - } |
32 | | - |
33 | | - function getHighlightContainer(preEl) { |
34 | | - const highlight = preEl.closest(".highlight"); |
35 | | - return (highlight instanceof HTMLElement ? highlight : preEl.parentElement) || preEl; |
36 | | - } |
37 | | - |
38 | | - function createButton(onClick) { |
39 | | - const btn = document.createElement("button"); |
40 | | - btn.type = "button"; |
41 | | - btn.className = "md-clipboard online-compile-button"; |
42 | | - btn.title = "在线编译(打开 Compiler Explorer)"; |
43 | | - btn.setAttribute("aria-label", btn.title); |
44 | | - btn.addEventListener("click", (e) => { |
45 | | - e.preventDefault(); |
46 | | - e.stopPropagation(); |
47 | | - onClick(); |
48 | | - }); |
49 | | - btn.textContent = "在线编译"; |
50 | | - return btn; |
| 20 | + return Array.from((root || document).querySelectorAll(selectors.join(","))); |
51 | 21 | } |
52 | 22 |
|
53 | 23 | function buildCompilerExplorerUrl(source) { |
|
77 | 47 | return `https://godbolt.org/clientstate/${encoded}`; |
78 | 48 | } |
79 | 49 |
|
80 | | - function addButtons() { |
81 | | - const blocks = findCppCodeBlocks(); |
82 | | - for (const codeEl of blocks) { |
83 | | - const preEl = getPreElement(codeEl); |
84 | | - if (!preEl) continue; |
| 50 | + function getSourceForCurrentPage() { |
| 51 | + const content = document.querySelector(".md-content"); |
| 52 | + const blocks = findCppCodeBlocks(content || document); |
| 53 | + const first = blocks[0]; |
| 54 | + const code = (first?.textContent || "").trim(); |
| 55 | + if (code) return code; |
| 56 | + return "#include <iostream>\n\nint main() {\n std::cout << \"Hello, C++!\" << '\\n';\n return 0;\n}\n"; |
| 57 | + } |
| 58 | + |
| 59 | + function openCompilerExplorer() { |
| 60 | + const url = buildCompilerExplorerUrl(getSourceForCurrentPage()); |
| 61 | + window.open(url, "_blank", "noopener,noreferrer"); |
| 62 | + } |
85 | 63 |
|
86 | | - const container = getHighlightContainer(preEl); |
87 | | - if (container.querySelector(".online-compile-button")) continue; |
| 64 | + function createHeaderButton() { |
| 65 | + const a = document.createElement("a"); |
| 66 | + a.href = "https://godbolt.org/"; |
| 67 | + a.className = "md-header__button md-icon online-compile-header-button"; |
| 68 | + a.title = "在线编译(打开 Compiler Explorer)"; |
| 69 | + a.setAttribute("aria-label", a.title); |
| 70 | + a.innerHTML = |
| 71 | + '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true">' + |
| 72 | + '<path d="M10 17l6-5-6-5v10ZM19 19H5V5h14v14Zm0-16H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2Z"/>' + |
| 73 | + "</svg>"; |
| 74 | + |
| 75 | + a.addEventListener("click", (e) => { |
| 76 | + e.preventDefault(); |
| 77 | + openCompilerExplorer(); |
| 78 | + }); |
88 | 79 |
|
89 | | - const code = (codeEl.textContent || "").trim(); |
90 | | - if (!code) continue; |
| 80 | + return a; |
| 81 | + } |
91 | 82 |
|
92 | | - const btn = createButton(() => { |
93 | | - const url = buildCompilerExplorerUrl(code); |
94 | | - window.open(url, "_blank", "noopener,noreferrer"); |
95 | | - }); |
| 83 | + function ensureHeaderButton() { |
| 84 | + const headerInner = |
| 85 | + document.querySelector(".md-header__inner") || |
| 86 | + document.querySelector("header.md-header") || |
| 87 | + document.body; |
96 | 88 |
|
97 | | - if (!codeBlockHasMain(code)) { |
98 | | - btn.classList.add("online-compile-button--no-main"); |
99 | | - btn.title = "在线编译(片段可能需要补全 main)"; |
100 | | - btn.setAttribute("aria-label", btn.title); |
101 | | - } |
| 89 | + if (!(headerInner instanceof HTMLElement)) return; |
| 90 | + if (headerInner.querySelector(".online-compile-header-button")) return; |
102 | 91 |
|
103 | | - container.appendChild(btn); |
104 | | - container.classList.add("online-compile-container"); |
105 | | - } |
| 92 | + headerInner.appendChild(createHeaderButton()); |
106 | 93 | } |
107 | 94 |
|
108 | 95 | function onReady(fn) { |
|
114 | 101 | } |
115 | 102 |
|
116 | 103 | onReady(() => { |
117 | | - addButtons(); |
| 104 | + ensureHeaderButton(); |
118 | 105 |
|
119 | | - const observer = new MutationObserver(() => addButtons()); |
| 106 | + const observer = new MutationObserver(() => ensureHeaderButton()); |
120 | 107 | observer.observe(document.body, { childList: true, subtree: true }); |
121 | 108 | }); |
122 | 109 | })(); |
0 commit comments