|
1 | | -(function () { |
| 1 | +(function () { |
2 | 2 | "use strict"; |
3 | 3 |
|
4 | 4 | /* --------------------------------------------------------------------- |
|
18 | 18 | var root = document.documentElement; |
19 | 19 | var THEME_KEY = "ghostrun-theme"; |
20 | 20 |
|
| 21 | + function getStoredTheme() { |
| 22 | + try { |
| 23 | + return localStorage.getItem(THEME_KEY); |
| 24 | + } catch (e) { |
| 25 | + return null; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + function setStoredTheme(theme) { |
| 30 | + try { |
| 31 | + localStorage.setItem(THEME_KEY, theme); |
| 32 | + } catch (e) { |
| 33 | + /* Theme still applies for this page view when storage is unavailable. */ |
| 34 | + } |
| 35 | + } |
| 36 | + |
21 | 37 | function applyTheme(theme) { |
22 | 38 | root.setAttribute("data-theme", theme); |
23 | 39 | var btn = document.getElementById("theme-toggle"); |
24 | 40 | if (btn) btn.setAttribute("aria-label", "Switch to " + (theme === "dark" ? "light" : "dark") + " mode"); |
25 | 41 | } |
26 | 42 |
|
27 | 43 | function initTheme() { |
28 | | - var stored = localStorage.getItem(THEME_KEY); |
| 44 | + var stored = getStoredTheme(); |
29 | 45 | var theme = stored || (window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark"); |
30 | 46 | applyTheme(theme); |
31 | 47 | } |
32 | 48 |
|
33 | 49 | function toggleTheme() { |
34 | 50 | var current = root.getAttribute("data-theme") === "light" ? "light" : "dark"; |
35 | 51 | var next = current === "light" ? "dark" : "light"; |
36 | | - localStorage.setItem(THEME_KEY, next); |
| 52 | + setStoredTheme(next); |
37 | 53 | applyTheme(next); |
38 | 54 | } |
39 | 55 |
|
|
77 | 93 | btn.addEventListener("click", function () { |
78 | 94 | var code = pre.querySelector("code"); |
79 | 95 | var text = code ? code.textContent : pre.textContent; |
80 | | - navigator.clipboard.writeText(text).then(function () { |
81 | | - btn.textContent = "Copied"; |
82 | | - btn.classList.add("copied"); |
| 96 | + if (!navigator.clipboard || !navigator.clipboard.writeText) { |
| 97 | + btn.textContent = "Unavailable"; |
83 | 98 | setTimeout(function () { |
84 | 99 | btn.textContent = "Copy"; |
85 | | - btn.classList.remove("copied"); |
86 | 100 | }, 1500); |
87 | | - }); |
| 101 | + return; |
| 102 | + } |
| 103 | + navigator.clipboard.writeText(text) |
| 104 | + .then(function () { |
| 105 | + btn.textContent = "Copied"; |
| 106 | + btn.classList.add("copied"); |
| 107 | + setTimeout(function () { |
| 108 | + btn.textContent = "Copy"; |
| 109 | + btn.classList.remove("copied"); |
| 110 | + }, 1500); |
| 111 | + }) |
| 112 | + .catch(function () { |
| 113 | + btn.textContent = "Failed"; |
| 114 | + setTimeout(function () { |
| 115 | + btn.textContent = "Copy"; |
| 116 | + }, 1500); |
| 117 | + }); |
88 | 118 | }); |
89 | 119 | pre.appendChild(btn); |
90 | 120 | }); |
|
166 | 196 | a.href = SITE_BASE + m.url; |
167 | 197 | a.className = "search-result"; |
168 | 198 | if (i === 0) a.classList.add("active"); |
169 | | - a.innerHTML = |
170 | | - '<span class="result-title">' + |
171 | | - m.title + |
172 | | - ' <span class="result-crumb">' + |
173 | | - m.section + |
174 | | - "</span></span>" + |
175 | | - '<span class="result-excerpt">' + |
176 | | - m.excerpt + |
177 | | - "</span>"; |
| 199 | + var title = document.createElement("span"); |
| 200 | + title.className = "result-title"; |
| 201 | + title.appendChild(document.createTextNode(m.title + " ")); |
| 202 | + |
| 203 | + var crumb = document.createElement("span"); |
| 204 | + crumb.className = "result-crumb"; |
| 205 | + crumb.textContent = m.section; |
| 206 | + title.appendChild(crumb); |
| 207 | + |
| 208 | + var excerpt = document.createElement("span"); |
| 209 | + excerpt.className = "result-excerpt"; |
| 210 | + excerpt.textContent = m.excerpt; |
| 211 | + |
| 212 | + a.appendChild(title); |
| 213 | + a.appendChild(excerpt); |
178 | 214 | results.appendChild(a); |
179 | 215 | }); |
180 | 216 | results.classList.add("open"); |
|
0 commit comments