// ==UserScript== // @name Google Sheets Theme Toggle // @namespace https://cyojkoy.github.io/Payment/ // @version 1.0.13 // @description 在 Google 表格(/spreadsheets/d/*)编辑界面右侧工具栏加一个深浅主题切换按钮;深色同时调整界面外壳与表格画布,提亮文字、压暗刺眼的浅色填充,切换工作表时即时着色、浅→深切换时折叠中性及浅色抗锯齿亮噪点(保留色相、不整体反色),并在网格 blit 之前接管与之同源的游离、离屏与暂存格子缓存(含挂在隐藏容器里的画布),按录制的文字色重建文字边缘抗锯齿、避免切换后表格内部残留浅色噪点且文字不发灰、无需手动刷新;同时通过压矮网格容器 1px、轻推滚动与补发 resize 触发 Google 立即整块重绘,保护 drawImage 图片像素(按 3/5/9 参数与变换解析目标矩形)、按角色曲线映射渐变与离屏上下文色标,并保护 save/restore 状态,偏好持久化并跟随系统。 // @author Yoko // @updateURL https://cdn.jsdelivr.net/gh/CYoJkoY/Yoko-ScriptCat-Scripts@release/sheets-theme-toggle.user.js // @downloadURL https://ghfast.top/https://raw.githubusercontent.com/CYoJkoY/Yoko-ScriptCat-Scripts/release/sheets-theme-toggle.user.js // @match https://docs.google.com/spreadsheets/* // @run-at document-start // @grant GM_getValue // @grant GM_setValue // @grant unsafeWindow // @noframes // @icon data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2064%2064%22%20role%3D%22img%22%20aria-label%3D%22Google%20Sheets%20Theme%20Toggle%22%3E%20%3Cdefs%3E%20%3ClinearGradient%20id%3D%22bg%22%20x1%3D%220%22%20y1%3D%220%22%20x2%3D%220%22%20y2%3D%2264%22%20gradientUnits%3D%22userSpaceOnUse%22%3E%20%3Cstop%20offset%3D%220%22%20stop-color%3D%22%230b3d2e%22%2F%3E%20%3Cstop%20offset%3D%221%22%20stop-color%3D%22%23062018%22%2F%3E%20%3C%2FlinearGradient%3E%20%3C%2Fdefs%3E%20%3Crect%20width%3D%2264%22%20height%3D%2264%22%20rx%3D%2214%22%20fill%3D%22url(%23bg)%22%2F%3E%20%3Crect%20x%3D%2214%22%20y%3D%2210%22%20width%3D%2228%22%20height%3D%2244%22%20rx%3D%224%22%20fill%3D%22none%22%20stroke%3D%22%2334a853%22%20stroke-width%3D%222.5%22%2F%3E%20%3Cpath%20d%3D%22M20%2020h16M20%2028h16M20%2036h16M20%2044h16%22%20stroke%3D%22%2334a853%22%20stroke-width%3D%221.6%22%2F%3E%20%3Ccircle%20cx%3D%2246%22%20cy%3D%2242%22%20r%3D%229%22%20fill%3D%22%23fbbc04%22%2F%3E%20%3Cpath%20d%3D%22M46%2036.5v-3M46%2050.5v-3M39.5%2042h-3M55.5%2042h-3M41.5%2037.5l-2-2M52.5%2048.5l-2-2M41.5%2046.5l-2%202M52.5%2035.5l-2%202%22%20stroke%3D%22%23f9ab00%22%20stroke-width%3D%221.6%22%20stroke-linecap%3D%22round%22%2F%3E%20%3C%2Fsvg%3E // @license MIT // ==/UserScript== "use strict"; (() => { // scripts/sheets-theme-toggle/src/config.ts var SCRIPT_MARKER_ATTRIBUTE = "data-yoko-sheets-theme-ready"; var THEME_ATTRIBUTE = "data-yoko-sheets-theme"; var STYLE_ID = "yoko-sheets-theme-style"; var BUTTON_ID = "yoko-sheets-theme-toggle"; var BUTTON_CLASS = "yoko-sheets-theme-button"; var FLOATING_ATTRIBUTE = "data-yoko-sheets-theme-floating"; var STORAGE_KEY = "yoko_sheets_theme_mode"; var THEME_MODE = Object.freeze({ LIGHT: "light", DARK: "dark" }); var SYSTEM_DARK_QUERY = "(prefers-color-scheme: dark)"; var MOUNT_TARGET_SELECTORS = Object.freeze([ "#docs-side-toolbar", "#docs-toolbar" ]); var REMOUNT_DELAY_MS = 120; var BUTTON_LABELS = Object.freeze({ toDark: "切换到深色主题", toLight: "切换到浅色主题" }); // scripts/sheets-theme-toggle/src/core/page.ts var SHEET_DOCUMENT_PATH = /^\/spreadsheets\/(?:u\/\d+\/)?d\/[^/]+/; function isSheetEditorPath(pathname) { return SHEET_DOCUMENT_PATH.test(pathname); } // scripts/sheets-theme-toggle/src/core/apply.ts function applyTheme(doc, mode) { const root2 = doc.documentElement; if (root2 === null) return; if (mode === THEME_MODE.DARK) { root2.setAttribute(THEME_ATTRIBUTE, THEME_MODE.DARK); return; } root2.removeAttribute(THEME_ATTRIBUTE); } function releaseTheme(doc) { const root2 = doc.documentElement; root2?.removeAttribute(THEME_ATTRIBUTE); } // scripts/sheets-theme-toggle/src/core/canvasColor.ts function clampByte(value) { if (!Number.isFinite(value)) return 0; if (value <= 0) return 0; if (value >= 255) return 255; return Math.round(value); } function rgba(r, g, b, a = 255) { return { r, g, b, a }; } var CANVAS_PALETTE = Object.freeze([ // Cell surface. { light: rgba(255, 255, 255), dark: rgba(32, 33, 36) }, // Row/column header band and the select-all corner. { light: rgba(248, 249, 250), dark: rgba(41, 42, 45) }, // Default body text. { light: rgba(68, 71, 70), dark: rgba(232, 234, 237) }, // Row/column header text. { light: rgba(4, 30, 73), dark: rgba(168, 199, 250) }, // Selection highlight over the surface. { light: rgba(211, 227, 253), dark: rgba(57, 68, 87) }, // Grid line stroke. { light: rgba(196, 199, 197), dark: rgba(95, 99, 104) }, // Thick/frozen divider drawn as a filled band. { light: rgba(192, 192, 192), dark: rgba(97, 101, 106) }, { light: rgba(199, 199, 199), dark: rgba(93, 97, 102) }, // Translucent grid line (rgb 31 31 31 at 13.3% alpha). { light: rgba(31, 31, 31, 34), dark: rgba(255, 255, 255, 34) } ]); var DARK_SURFACE = rgba(32, 33, 36); var FRINGE_MIN_CHANNEL = 220; var FRINGE_MAX_SPREAD = 8; var NEUTRAL_SPREAD = 24; var NEUTRAL_LIGHTNESS_MIN = 0.5; var LIGHT_FOLD_MIN_LIGHTNESS = 0.42; var BRIDGE_TOLERANCE = 14; var BRIDGE_T_MAX = 1; var PACKED_NONE = -1; var quantKey = (r, g, b) => r >> 2 << 12 | g >> 2 << 6 | b >> 2; var packRgb = (r, g, b) => clampByte(r) << 16 | clampByte(g) << 8 | clampByte(b); var colorKey = (color) => `${color.r},${color.g},${color.b},${color.a}`; function buildTable(from, to) { const table = /* @__PURE__ */ new Map(); for (const pair of CANVAS_PALETTE) { const source = colorKey(pair[from]); if (table.has(source)) continue; table.set(source, pair[to]); } return table; } var DARK_TABLE = buildTable("light", "dark"); var LIGHT_TABLE = buildTable("dark", "light"); var pixelKey = (r, g, b, a) => (r << 24 | g << 16 | b << 8 | a) >>> 0; function pixelKeyOf(color) { return pixelKey(color.r, color.g, color.b, color.a); } function buildPalettePixels(from, to) { const table = /* @__PURE__ */ new Map(); for (const pair of CANVAS_PALETTE) { const source = pair[from]; const key = pixelKey(source.r, source.g, source.b, source.a); if (table.has(key)) continue; table.set(key, pair[to]); } return table; } var DARK_PIXELS = buildPalettePixels("light", "dark"); var LIGHT_PIXELS = buildPalettePixels("dark", "light"); var PAPER_LIGHTNESS_BASE = 0.04; var PAPER_LIGHTNESS_GAIN = 0.095; var PAPER_MAX_SATURATION = 0.6; var INK_LIGHTNESS_BASE = 0.86; var INK_LIGHTNESS_GAIN = 0.12; var INK_MAX_SATURATION = 0.85; var EDGE_LIGHTNESS_BASE = 0.24; var EDGE_BRIGHT_GAIN = 0.2; var EDGE_DARK_GAIN = 0.24; var EDGE_MAX_SATURATION = 0.6; var EDGE_SPLIT_LIGHTNESS = 0.55; function clampUnit(value) { if (value <= 0) return 0; if (value >= 1) return 1; return value; } function rgbToHsl(color) { const red = color.r / 255; const green = color.g / 255; const blue = color.b / 255; const maximum = Math.max(red, green, blue); const minimum = Math.min(red, green, blue); const lightness = (maximum + minimum) / 2; if (maximum === minimum) return [0, 0, lightness]; const delta = maximum - minimum; const saturation = lightness > 0.5 ? delta / (2 - maximum - minimum) : delta / (maximum + minimum); let hue = 0; if (maximum === red) hue = (green - blue) / delta + (green < blue ? 6 : 0); if (maximum === green) hue = (blue - red) / delta + 2; if (maximum === blue) hue = (red - green) / delta + 4; return [hue / 6, saturation, lightness]; } function hueToChannel(first, second, hue) { let value = hue; if (value < 0) value += 1; if (value > 1) value -= 1; if (value < 1 / 6) return first + (second - first) * 6 * value; if (value < 1 / 2) return second; if (value < 2 / 3) return first + (second - first) * (2 / 3 - value) * 6; return first; } function hslToRgba(hue, saturation, lightness, alpha) { if (saturation === 0) { const channel = Math.round(lightness * 255); return rgba(channel, channel, channel, alpha); } const second = lightness < 0.5 ? lightness * (1 + saturation) : lightness + saturation - lightness * saturation; const first = 2 * lightness - second; return rgba( Math.round(hueToChannel(first, second, hue + 1 / 3) * 255), Math.round(hueToChannel(first, second, hue) * 255), Math.round(hueToChannel(first, second, hue - 1 / 3) * 255), alpha ); } function roleLightness(role, lightness) { const clamped = clampUnit(lightness); if (role === "paper") return PAPER_LIGHTNESS_BASE + PAPER_LIGHTNESS_GAIN * clamped; if (role === "ink") return INK_LIGHTNESS_BASE + INK_LIGHTNESS_GAIN * clamped; if (clamped >= 0.5) return EDGE_LIGHTNESS_BASE + (clamped - 0.5) * EDGE_BRIGHT_GAIN * 2; return EDGE_LIGHTNESS_BASE + (0.5 - clamped) * EDGE_DARK_GAIN * 2; } function roleMaxSaturation(role) { if (role === "ink") return INK_MAX_SATURATION; if (role === "edge") return EDGE_MAX_SATURATION; return PAPER_MAX_SATURATION; } var neutralDarkLutCache = null; function neutralDarkLut() { if (neutralDarkLutCache !== null) return neutralDarkLutCache; const lut = new Uint8Array(256); for (let value = 0; value < 256; value += 1) { const lightness = value / 255; lut[value] = lightness >= NEUTRAL_LIGHTNESS_MIN ? Math.round(roleLightness("paper", lightness) * 255) : value; } neutralDarkLutCache = lut; return lut; } function roleForPathFill(color) { const [, , lightness] = rgbToHsl(color); return lightness >= EDGE_SPLIT_LIGHTNESS ? "paper" : "edge"; } function roleColor(color, role) { if (color.a === 0) return color; const palette = DARK_TABLE.get(colorKey(color)); if (palette !== void 0) return palette; const [hue, saturation, lightness] = rgbToHsl(color); return hslToRgba( hue, Math.min(saturation, roleMaxSaturation(role)), roleLightness(role, lightness), color.a ); } var HEX_PATTERN = /^#[0-9a-f]{3,8}$/u; var HEX_DIGIT_COUNTS = /* @__PURE__ */ new Set([3, 4, 6, 8]); function parseHex(hex) { if (!HEX_PATTERN.test(hex)) return null; const digits = hex.slice(1); if (!HEX_DIGIT_COUNTS.has(digits.length)) return null; const pairAt = (index) => Number.parseInt(digits.slice(index, index + 2), 16); if (digits.length === 3 || digits.length === 4) { const expanded = [...digits].map((digit) => Number.parseInt(digit + digit, 16)); const [r2, g2, b2, a2] = expanded; if (r2 === void 0 || g2 === void 0 || b2 === void 0) return null; return rgba(r2, g2, b2, a2 ?? 255); } const r = pairAt(0); const g = pairAt(2); const b = pairAt(4); const a = digits.length === 8 ? pairAt(6) : 255; if ([r, g, b, a].some((value) => Number.isNaN(value))) return null; return rgba(r, g, b, a); } var NAMED_COLORS = Object.freeze({ transparent: rgba(0, 0, 0, 0), white: rgba(255, 255, 255), black: rgba(0, 0, 0) }); function parseChannel(segment) { const percent = segment.endsWith("%"); const value = Number.parseFloat(percent ? segment.slice(0, -1) : segment); if (!Number.isFinite(value)) return null; return clampByte(percent ? value * 2.55 : value); } function parseAlpha(segment) { if (segment.endsWith("%")) { const percent = Number.parseFloat(segment.slice(0, -1)); return Number.isFinite(percent) ? clampByte(percent / 100 * 255) : null; } const value = Number.parseFloat(segment); if (!Number.isFinite(value)) return null; return clampByte(value <= 1 ? value * 255 : value); } function parseFunction(body) { const segments = body.replace(/\//gu, " ").split(/[\s,]+/u).filter((segment) => segment.length > 0); if (segments.length < 3 || segments.length > 4) return null; const [rs, gs, bs, as] = segments; if (rs === void 0 || gs === void 0 || bs === void 0) return null; const r = parseChannel(rs); const g = parseChannel(gs); const b = parseChannel(bs); if (r === null || g === null || b === null) return null; if (as === void 0) return rgba(r, g, b); const a = parseAlpha(as); if (a === null) return null; return rgba(r, g, b, a); } function parseColor(input) { const value = input.trim().toLowerCase(); const named = NAMED_COLORS[value]; if (named !== void 0) return named; if (value.startsWith("#")) return parseHex(value); const open = value.indexOf("("); if (open === -1 || !value.endsWith(")")) return null; const name = value.slice(0, open); if (name !== "rgb" && name !== "rgba") return null; return parseFunction(value.slice(open + 1, -1)); } var hexByte = (value) => value.toString(16).padStart(2, "0"); function formatColor(color) { const { r, g, b, a } = color; if (a >= 255) return `#${hexByte(r)}${hexByte(g)}${hexByte(b)}`; return `rgba(${r}, ${g}, ${b}, ${Number((a / 255).toFixed(3))})`; } function normalizeColor(input) { const parsed = parseColor(input); return parsed === null ? input : formatColor(parsed); } function buildPixelTable(pairs) { const table = /* @__PURE__ */ new Map(); for (const [from, to] of pairs) { const key = pixelKeyOf(from); if (table.has(key)) continue; table.set(key, to); } return table; } function bridgePixel(r, g, b, lines) { let result = PACKED_NONE; let bestResidual = Number.POSITIVE_INFINITY; let bestDistance = Number.POSITIVE_INFINITY; for (const line of lines) { const from = line.from; const dr = 255 - from.r; const dg = 255 - from.g; const db = 255 - from.b; const lengthSquared = dr * dr + dg * dg + db * db; if (lengthSquared === 0) continue; const raw = ((r - from.r) * dr + (g - from.g) * dg + (b - from.b) * db) / lengthSquared; if (raw > BRIDGE_T_MAX || raw < -0.25) continue; const t = raw < 0 ? 0 : raw; const residual = Math.max( Math.abs(r - (from.r + dr * t)), Math.abs(g - (from.g + dg * t)), Math.abs(b - (from.b + db * t)) ); if (residual > BRIDGE_TOLERANCE) continue; const distance = (r - from.r) ** 2 + (g - from.g) ** 2 + (b - from.b) ** 2; if (residual > bestResidual + 2) continue; if (residual < bestResidual - 2 || distance < bestDistance) { bestResidual = residual; bestDistance = distance; const to = line.to; result = packRgb( to.r + (DARK_SURFACE.r - to.r) * t, to.g + (DARK_SURFACE.g - to.g) * t, to.b + (DARK_SURFACE.b - to.b) * t ); } } return result; } function foldPixel(r, g, b, spread, lut) { if (spread <= NEUTRAL_SPREAD) { const grey = Math.round((r + g + b) / 3); const value = lut[grey] ?? grey; if (value === grey) return PACKED_NONE; return packRgb(value, value, value); } const lightness = (Math.max(r, g, b) + Math.min(r, g, b)) / 510; if (lightness < LIGHT_FOLD_MIN_LIGHTNESS) return PACKED_NONE; const ratio = roleLightness("paper", lightness) / lightness; return packRgb(r * ratio, g * ratio, b * ratio); } function mapPixels(data, table, sweep, neutralLut = null, mask = null, lines = null) { let changed = false; const ramps = lines !== null && lines.length > 0 ? lines : null; const rampCache = ramps === null ? null : /* @__PURE__ */ new Map(); const foldCache = neutralLut === null ? null : /* @__PURE__ */ new Map(); for (let index = 0; index + 3 < data.length; index += 4) { if (mask !== null && mask[index >> 2] === 1) continue; const alpha = data[index + 3] ?? 0; if (alpha === 0) continue; const r = data[index] ?? 0; const g = data[index + 1] ?? 0; const b = data[index + 2] ?? 0; const mapped = table.get(pixelKey(r, g, b, alpha)); if (mapped !== void 0) { data[index] = mapped.r; data[index + 1] = mapped.g; data[index + 2] = mapped.b; data[index + 3] = mapped.a; changed = true; continue; } const max = Math.max(r, g, b); const min = Math.min(r, g, b); const spread = max - min; if (sweep !== null && min >= FRINGE_MIN_CHANNEL && max - min <= FRINGE_MAX_SPREAD) { data[index] = sweep.r; data[index + 1] = sweep.g; data[index + 2] = sweep.b; changed = true; continue; } const key = quantKey(r, g, b); if (ramps !== null && rampCache !== null) { let packed = rampCache.get(key); if (packed === void 0) { packed = bridgePixel(r, g, b, ramps); rampCache.set(key, packed); } if (packed !== PACKED_NONE) { data[index] = packed >> 16 & 255; data[index + 1] = packed >> 8 & 255; data[index + 2] = packed & 255; changed = true; continue; } } if (neutralLut === null || foldCache === null) continue; let folded = foldCache.get(key); if (folded === void 0) { folded = foldPixel(r, g, b, spread, neutralLut); foldCache.set(key, folded); } if (folded === PACKED_NONE) continue; data[index] = folded >> 16 & 255; data[index + 1] = folded >> 8 & 255; data[index + 2] = folded & 255; changed = true; } return changed; } var FRINGE_SWEEP = DARK_SURFACE; // scripts/sheets-theme-toggle/src/core/canvasTheme.ts var GRID_CONTAINER_SELECTOR = "#waffle-grid-container"; var GRID_CANVAS_SELECTOR = "#waffle-grid-container canvas"; var CHART_SELECTOR = ".waffle-chart, [class*='chart']"; var COLOR_CACHE_LIMIT = 512; var ROLE_RECORD_LIMIT = 512; var IMAGE_RECT_LIMIT = 64; var BRIDGE_LINE_LIMIT = 12; var TILE_PAPER_MIN_CHANNEL = 224; var TILE_PAPER_MAX_SPREAD = 24; var TILE_PAPER_RATIO = 0.35; var TILE_SAMPLE_LIMIT = 4096; var SOURCE_LIMIT = 24; var REPAINT_NUDGE_PX = 1; var REPAINT_RETRY_MS = 120; var REPAINT_LATE_MS = 400; function scrollBoxOf(node) { if (!("scrollTop" in node) || !("clientHeight" in node)) return null; const element = node; const scrollsDown = element.scrollHeight > element.clientHeight + REPAINT_NUDGE_PX; const scrollsAcross = element.scrollWidth > element.clientWidth + REPAINT_NUDGE_PX; return scrollsDown || scrollsAcross ? element : null; } function isRecord(value) { return typeof value === "object" && value !== null; } function isCanvasElement(value) { return isRecord(value) && Reflect.get(value, "nodeType") === 1 && typeof Reflect.get(value, "getContext") === "function"; } function resolveContextConstructor(candidates, key = "CanvasRenderingContext2D") { for (const host of candidates) { if (!isRecord(host)) continue; const candidate = host[key]; if (typeof candidate !== "function") continue; const prototype = candidate.prototype; if (isRecord(prototype)) return { prototype }; } return null; } function hookStyleProperty(prototype, property, remap) { const descriptor = Object.getOwnPropertyDescriptor(prototype, property); if (descriptor === void 0) return null; if (descriptor.get === void 0 && descriptor.set === void 0 && !("value" in descriptor)) return null; const assigned = /* @__PURE__ */ new WeakMap(); const readOriginal = (self) => { if (assigned.has(self)) return assigned.get(self); if (descriptor.get !== void 0) return descriptor.get.call(self); return descriptor.value; }; const writeOriginal = (self, value) => { if (descriptor.set !== void 0) { descriptor.set.call(self, value); return; } Reflect.defineProperty(self, property, { value, writable: true, configurable: true, enumerable: true }); }; const accessor = { configurable: true, enumerable: descriptor.enumerable ?? false, get() { const original = readOriginal(this); return typeof original === "string" ? normalizeColor(original) : original; }, set(value) { assigned.set(this, value); writeOriginal(this, remap(this, value)); } }; try { Reflect.defineProperty(prototype, property, accessor); } catch (error) { if (error instanceof TypeError) return null; throw error; } return { assignedValue: (self) => readOriginal(self), hasAssigned: (self) => assigned.has(self), setAssigned: (self, value) => { assigned.set(self, value); }, deleteAssigned: (self) => { assigned.delete(self); }, writeRaw: (self, value) => writeOriginal(self, value), restore: () => { const current = Object.getOwnPropertyDescriptor(prototype, property); if (current === void 0 || current.get !== accessor.get) return; Reflect.defineProperty(prototype, property, descriptor); } }; } function hookDrawMethod(prototype, method, draw, role, fill, stroke, themed, record, darkColor, dark) { const original = Object.getOwnPropertyDescriptor(prototype, method); if (original?.value !== draw || typeof draw !== "function") return null; const wrapper = function(...args) { const isGrid = themed(this); const style = method === "fillText" || method === "fillRect" || method === "fill" ? fill : stroke; const defaultRole = style === fill ? "paper" : "edge"; const current = style.assignedValue(this); const currentColor = typeof current === "string" ? parseColor(current) : null; const currentRole = role(this, args); if (currentColor !== null) record(this, currentColor, currentRole); if (!isGrid || currentColor === null || !dark()) return draw.apply(this, args); style.writeRaw(this, darkColor(currentColor, currentRole)); try { return draw.apply(this, args); } finally { style.writeRaw(this, darkColor(currentColor, defaultRole)); } }; try { Reflect.defineProperty(prototype, method, { ...original, value: wrapper }); } catch (error) { if (error instanceof TypeError) return null; throw error; } return () => { const current = Object.getOwnPropertyDescriptor(prototype, method); if (current?.value !== wrapper) return; Reflect.defineProperty(prototype, method, original); }; } function wrapPrototypeMethod(prototype, name, wrap) { const descriptor = Object.getOwnPropertyDescriptor(prototype, name); if (descriptor === void 0) return null; const original = descriptor.value; if (typeof original !== "function") return null; const wrapper = wrap(original); try { Reflect.defineProperty(prototype, name, { ...descriptor, value: wrapper }); } catch (error) { if (error instanceof TypeError) return null; throw error; } return () => { const current = Object.getOwnPropertyDescriptor(prototype, name); if (current?.value !== wrapper) return; Reflect.defineProperty(prototype, name, descriptor); }; } function installCanvasTheme(options = {}) { const doc = options.doc ?? document; const scope = options.scope ?? globalThis; const page = options.page ?? (isRecord(scope) ? Reflect.get(scope, "unsafeWindow") : null); const constructor = resolveContextConstructor([page, scope]); let mode = THEME_MODE.LIGHT; let applied = null; let disposed = false; const themed = /* @__PURE__ */ new WeakSet(); const canvases = /* @__PURE__ */ new Set(); const sources = /* @__PURE__ */ new Set(); const foreignSources = /* @__PURE__ */ new WeakSet(); const rendered = /* @__PURE__ */ new WeakMap(); const records = /* @__PURE__ */ new WeakMap(); const imageRects = /* @__PURE__ */ new WeakMap(); const gridFlags = /* @__PURE__ */ new WeakMap(); const colorCache = /* @__PURE__ */ new Map(); const restores = []; const repaintTimers = []; const styleStack = /* @__PURE__ */ new WeakMap(); const themedColor = (color, role) => { const key = `${role}|${pixelKeyOf(color)}`; const cached = colorCache.get(key); if (cached !== void 0) return cached; const value = formatColor(roleColor(color, role)); if (colorCache.size >= COLOR_CACHE_LIMIT) colorCache.clear(); colorCache.set(key, value); return value; }; const contextCanvas = (self) => { const canvas = Reflect.get(self, "canvas"); return isCanvasElement(canvas) ? canvas : null; }; const contextSurface = (self) => { const surface = Reflect.get(self, "canvas"); return isRecord(surface) ? surface : null; }; const classifyGrid = (canvas) => { const cached = gridFlags.get(canvas); if (cached !== void 0) return cached; const isChart = canvas.closest(CHART_SELECTOR) !== null; const isGrid = !isChart && canvas.closest(GRID_CONTAINER_SELECTOR) !== null; if (isGrid || canvas.isConnected) gridFlags.set(canvas, isGrid); return isGrid; }; const adopt = (canvas) => { if (canvases.has(canvas)) return; canvases.add(canvas); themed.add(canvas); rendered.set(canvas, mode); }; const isThemedContext = (self) => { const surface = contextSurface(self); if (surface === null) return false; if (themed.has(surface)) return true; if (!isCanvasElement(surface)) return false; if (!classifyGrid(surface)) return false; adopt(surface); return true; }; const remember = (self, color, role) => { const canvas = contextCanvas(self); if (canvas === null || !themed.has(canvas)) return; let canvasRecords = records.get(canvas); if (canvasRecords === void 0) { canvasRecords = /* @__PURE__ */ new Map(); records.set(canvas, canvasRecords); } const key = pixelKeyOf(color); if (!canvasRecords.has(key) && canvasRecords.size >= ROLE_RECORD_LIMIT) { const first = canvasRecords.keys().next().value; if (typeof first === "number") canvasRecords.delete(first); } if (!canvasRecords.has(key)) canvasRecords.set(key, { color, role }); }; const remap = (role) => (self, value) => { if (typeof value !== "string") return value; const parsed = parseColor(value); if (parsed === null) return value; if (mode !== THEME_MODE.DARK || !isThemedContext(self)) return value; return themedColor(parsed, role); }; const fill = constructor === null ? null : hookStyleProperty(constructor.prototype, "fillStyle", remap("paper")); const stroke = constructor === null ? null : hookStyleProperty(constructor.prototype, "strokeStyle", remap("edge")); if (fill !== null) restores.push(fill.restore); if (stroke !== null) restores.push(stroke.restore); const offscreenConstructor = resolveContextConstructor([page, scope], "OffscreenCanvasRenderingContext2D"); if (offscreenConstructor !== null) { for (const [property, role] of [ ["fillStyle", "paper"], ["strokeStyle", "edge"] ]) { const binding = hookStyleProperty(offscreenConstructor.prototype, property, remap(role)); if (binding !== null) restores.push(binding.restore); } } const roleForMethod = (method, self, args) => { if (method === "fillText" || method === "strokeText") return "ink"; if (method === "fillRect") return "paper"; if (method === "fill") { const current = fill?.assignedValue(self); const color = typeof current === "string" ? parseColor(current) : null; return color === null ? "paper" : roleForPathFill(color); } return "edge"; }; const imageDestinationOf = (self, args, canvas) => { const num = (value, fallback) => typeof value === "number" && Number.isFinite(value) ? value : fallback; const dim = (value, key) => typeof value === "object" && value !== null ? num(Reflect.get(value, key), 0) : 0; const image = args[0]; let dx = 0; let dy = 0; let dw = dim(image, "width") || canvas.width; let dh = dim(image, "height") || canvas.height; if (args.length >= 9) { dx = num(args[5], 0); dy = num(args[6], 0); dw = num(args[7], dw); dh = num(args[8], dh); } else if (args.length >= 5) { dx = num(args[1], 0); dy = num(args[2], 0); dw = num(args[3], dw); dh = num(args[4], dh); } else if (args.length === 3) { dx = num(args[1], 0); dy = num(args[2], 0); } if (dw <= 0 || dh <= 0) return null; let corners = [ [dx, dy], [dx + dw, dy], [dx, dy + dh], [dx + dw, dy + dh] ]; const getTransform = Reflect.get(self, "getTransform"); if (typeof getTransform === "function") { try { const matrix = getTransform.call(self); if (isRecord(matrix)) { const [a, b, c, d, e, f] = ["a", "b", "c", "d", "e", "f"].map( (key) => typeof Reflect.get(matrix, key) === "number" ? Reflect.get(matrix, key) : 0 ); corners = corners.map(([x, y]) => [a * x + c * y + e, b * x + d * y + f]); } } catch { } } const xs = corners.map(([x]) => x); const ys = corners.map(([, y]) => y); const left = Math.min(...xs); const top = Math.min(...ys); return [left, top, Math.max(...xs) - left, Math.max(...ys) - top]; }; const decorateGradient = (gradient, self) => { if (!isRecord(gradient)) return; const addColorStop = Reflect.get(gradient, "addColorStop"); if (typeof addColorStop !== "function") return; const original = addColorStop; const wrapped = function(offset, color) { let mapped = color; if (typeof color === "string" && mode === THEME_MODE.DARK && isThemedContext(self)) { const parsed = parseColor(color); if (parsed !== null) mapped = themedColor(parsed, roleForPathFill(parsed)); } return original.call(this, offset, mapped); }; try { Reflect.defineProperty(gradient, "addColorStop", { configurable: true, writable: true, enumerable: false, value: wrapped }); } catch { } }; if (constructor !== null && fill !== null && stroke !== null) { const prototype = constructor.prototype; const methods = ["fillText", "strokeText", "fillRect", "fill", "stroke", "strokeRect"]; for (const method of methods) { const draw = Reflect.get(prototype, method); if (typeof draw !== "function") continue; const restore = hookDrawMethod( prototype, method, draw, (self, args) => roleForMethod(method, self, args), fill, stroke, isThemedContext, remember, themedColor, () => mode === THEME_MODE.DARK ); if (restore !== null) restores.push(restore); } if (typeof Reflect.get(prototype, "save") === "function" && typeof Reflect.get(prototype, "restore") === "function") { const saveRestore = wrapPrototypeMethod(prototype, "save", (next) => function(...args) { const stack = styleStack.get(this) ?? []; if (stack.length < 256) { stack.push({ fill: { present: fill.hasAssigned(this), value: fill.assignedValue(this) }, stroke: { present: stroke.hasAssigned(this), value: stroke.assignedValue(this) } }); styleStack.set(this, stack); } return next.apply(this, args); }); if (saveRestore !== null) restores.push(saveRestore); const restoreRestore = wrapPrototypeMethod(prototype, "restore", (next) => function(...args) { const stack = styleStack.get(this); const saved = stack?.pop(); if (saved === void 0) return next.apply(this, args); if (saved.fill.present) fill.setAssigned(this, saved.fill.value); else fill.deleteAssigned(this); if (saved.stroke.present) stroke.setAssigned(this, saved.stroke.value); else stroke.deleteAssigned(this); return next.apply(this, args); }); if (restoreRestore !== null) restores.push(restoreRestore); } if (typeof Reflect.get(prototype, "drawImage") === "function") { const drawImageRestore = wrapPrototypeMethod(prototype, "drawImage", (next) => function(...args) { const source = adoptSourceImage(args[0]); const result = next.apply(this, args); const canvas = contextCanvas(this); if (canvas === null || !isThemedContext(this)) return result; if (source !== null) return result; const rect = imageDestinationOf(this, args, canvas); if (rect === null) return result; const rects = imageRects.get(canvas) ?? []; if (rects.length >= IMAGE_RECT_LIMIT) rects.shift(); rects.push(rect); imageRects.set(canvas, rects); return result; }); if (drawImageRestore !== null) restores.push(drawImageRestore); } for (const factory of ["createLinearGradient", "createRadialGradient", "createConicGradient"]) { if (typeof Reflect.get(prototype, factory) !== "function") continue; const restore = wrapPrototypeMethod(prototype, factory, (next) => function(...args) { const gradient = next.apply(this, args); decorateGradient(gradient, this); return gradient; }); if (restore !== null) restores.push(restore); } } const readPixels = (canvas) => { const context = canvas.getContext("2d"); if (context === null || canvas.width === 0 || canvas.height === 0) return null; try { return context.getImageData(0, 0, canvas.width, canvas.height); } catch (error) { if (error instanceof DOMException && error.name === "SecurityError") return null; throw error; } }; const recordsFor = (canvas) => { const own = records.get(canvas); if (own !== void 0 && own.size > 0) return own; for (const grid of canvases) { if (grid === canvas) continue; const other = records.get(grid); if (other !== void 0 && other.size > 0) return other; } return void 0; }; const pixelTable = (canvas, target) => { const pairs = CANVAS_PALETTE.map( (pair) => target === THEME_MODE.DARK ? [pair.light, pair.dark] : [pair.dark, pair.light] ); const remembered = recordsFor(canvas); if (remembered !== void 0) { for (const { color, role } of remembered.values()) { pairs.push( target === THEME_MODE.DARK ? [color, roleColor(color, role)] : [roleColor(color, role), color] ); } } return buildPixelTable(pairs); }; const inkLinesOf = (canvas) => { const remembered = recordsFor(canvas); if (remembered === void 0 || remembered.size === 0) return []; const painted = /* @__PURE__ */ new Set(); for (const { color, role } of remembered.values()) { if (role !== "ink") painted.add(pixelKeyOf(color)); } const lines = []; for (const { color, role } of remembered.values()) { if (role !== "ink") continue; if (painted.has(pixelKeyOf(color))) continue; lines.push({ from: color, to: roleColor(color, role) }); if (lines.length >= BRIDGE_LINE_LIMIT) break; } return lines; }; const bridgeLines = (canvas) => { const own = inkLinesOf(canvas); if (own.length > 0) return own; for (const grid of canvases) { if (grid === canvas) continue; const lines = inkLinesOf(grid); if (lines.length > 0) return lines; } return null; }; const applyPixels = (canvas, target, useMask = true) => { const image = readPixels(canvas); if (image === null) return false; const rects = useMask ? imageRects.get(canvas) : void 0; let mask = null; if (rects !== void 0 && rects.length > 0 && image.width > 0 && image.height > 0) { mask = new Uint8Array(image.width * image.height); const total = image.width * image.height; for (const [rx, ry, rw, rh] of rects) { const left = Math.max(0, Math.floor(rx)); const top = Math.max(0, Math.floor(ry)); const right = Math.min(image.width, Math.ceil(rx + rw)); const bottom = Math.min(image.height, Math.ceil(ry + rh)); if (right <= left || bottom <= top) continue; if ((right - left) * (bottom - top) * 2 >= total) continue; for (let y = top; y < bottom; y += 1) { mask.fill(1, y * image.width + left, y * image.width + right); } } } const changed = mapPixels( image.data, pixelTable(canvas, target), target === THEME_MODE.DARK ? FRINGE_SWEEP : null, target === THEME_MODE.DARK ? neutralDarkLut() : null, mask, target === THEME_MODE.DARK ? bridgeLines(canvas) : null ); if (!changed) return false; const context = canvas.getContext("2d"); if (context === null) return false; try { context.putImageData(image, 0, 0); } catch (error) { if (error instanceof DOMException && error.name === "SecurityError") return false; throw error; } return true; }; const gridTileVerdict = (canvas) => { const image = readPixels(canvas); if (image === null) return null; const { data } = image; const total = Math.floor(data.length / 4); if (total === 0) return null; const step = Math.max(1, Math.floor(total / TILE_SAMPLE_LIMIT)); let sampled = 0; let paper = 0; for (let pixel = 0; pixel < total; pixel += step) { const index = pixel * 4; if ((data[index + 3] ?? 0) === 0) continue; sampled += 1; const r = data[index] ?? 0; const g = data[index + 1] ?? 0; const b = data[index + 2] ?? 0; const max = Math.max(r, g, b); if (Math.min(r, g, b) >= TILE_PAPER_MIN_CHANNEL && max - Math.min(r, g, b) <= TILE_PAPER_MAX_SPREAD) { paper += 1; } } if (sampled === 0) return true; return paper / sampled >= TILE_PAPER_RATIO; }; const adoptSourceImage = (image) => { if (!isRecord(image) || typeof Reflect.get(image, "getContext") !== "function") return null; if (themed.has(image)) { const candidate = image; return sources.has(candidate) ? "cache" : "grid"; } const canvas = image; const element = isCanvasElement(canvas); if (element && classifyGrid(canvas)) return "grid"; if (element && canvas.closest(CHART_SELECTOR) !== null) return null; if (foreignSources.has(canvas)) return null; const verdict = gridTileVerdict(canvas); if (verdict === null) return null; if (!verdict) { foreignSources.add(canvas); return null; } themed.add(canvas); sources.add(canvas); applyPixels(canvas, mode, false); rendered.set(canvas, mode); if (sources.size > SOURCE_LIMIT) { for (const oldest of sources) { if (sources.size <= SOURCE_LIMIT) break; sources.delete(oldest); themed.delete(oldest); rendered.delete(oldest); } } return "cache"; }; function gridBoxOf(canvas) { const box = canvas.closest(GRID_CONTAINER_SELECTOR); if (box === null || !("style" in box)) return null; return box; } const nudgeScroll = (host, view) => { const top = host.scrollTop; const left = host.scrollLeft; const down = host.scrollHeight > host.clientHeight + REPAINT_NUDGE_PX; const across = host.scrollWidth > host.clientWidth + REPAINT_NUDGE_PX; const restore = () => { try { host.scrollTop = top; host.scrollLeft = left; host.dispatchEvent(new Event("scroll")); } catch { } }; try { if (down) host.scrollTop = top + REPAINT_NUDGE_PX; if (across) host.scrollLeft = left + REPAINT_NUDGE_PX; host.dispatchEvent(new Event("scroll")); } catch { return; } if (view !== null && typeof view.requestAnimationFrame === "function") { view.requestAnimationFrame(restore); return; } restore(); }; const squeezeLayout = (box, view) => { const height = box.clientHeight; if (height <= 2) return; const previous = box.style.getPropertyValue("height"); const priority = box.style.getPropertyPriority("height"); const restore = () => { try { if (previous === "") box.style.removeProperty("height"); else box.style.setProperty("height", previous, priority); } catch { } }; try { box.style.setProperty("height", `${height - 1}px`, "important"); } catch { return; } if (view === null || typeof view.requestAnimationFrame !== "function") { restore(); return; } view.requestAnimationFrame(() => { view.requestAnimationFrame(restore); }); }; const requestRepaint = (view) => { if (view === null) return; for (const canvas of canvases) { if (!canvas.isConnected) continue; const box = gridBoxOf(canvas); if (box !== null) squeezeLayout(box, view); let host = null; for (let node = canvas.parentElement; node !== null && host === null; node = node.parentElement) { host = scrollBoxOf(node); } if (host !== null) { nudgeScroll(host, view); break; } if (box !== null) break; } const ping = () => { try { view.dispatchEvent(new Event("resize")); } catch { } }; ping(); if (typeof view.requestAnimationFrame === "function") { view.requestAnimationFrame(ping); } if (typeof view.setTimeout !== "function") return; for (const delay of [REPAINT_RETRY_MS, REPAINT_LATE_MS]) { const timer = view.setTimeout(() => { const index = repaintTimers.indexOf(timer); if (index !== -1) repaintTimers.splice(index, 1); ping(); }, delay); repaintTimers.push(timer); } }; const register = (canvas) => { if (canvases.has(canvas)) return; adopt(canvas); if (mode === THEME_MODE.DARK && applyPixels(canvas, THEME_MODE.DARK)) { requestRepaint(doc.defaultView); } }; const scan = () => { if (disposed) return; for (const canvas of Array.from(canvases)) { if (canvas.isConnected) continue; canvases.delete(canvas); themed.delete(canvas); } for (const canvas of Array.from(doc.querySelectorAll(GRID_CANVAS_SELECTOR))) { if (canvas.closest(CHART_SELECTOR) === null) register(canvas); } }; let scanQueued = false; const scheduleScan = () => { if (scanQueued || disposed) return; scanQueued = true; queueMicrotask(() => { scanQueued = false; scan(); }); }; const observer = doc.documentElement === null || typeof MutationObserver !== "function" ? null : new MutationObserver(scheduleScan); observer?.observe(doc.documentElement, { childList: true, subtree: true }); scan(); return { setMode: (next) => { if (disposed) return; const changed = applied !== null && applied !== next; mode = next; if (next === THEME_MODE.DARK) scan(); for (const canvas of canvases) { if (rendered.get(canvas) === next) continue; applyPixels(canvas, next); rendered.set(canvas, next); } for (const source of sources) { if (rendered.get(source) === next) continue; applyPixels(source, next, false); rendered.set(source, next); } applied = next; if (changed) requestRepaint(doc.defaultView); }, dispose: () => { if (disposed) return; const win = doc.defaultView; if (win !== null) { for (const timer of repaintTimers) win.clearTimeout(timer); } repaintTimers.length = 0; for (const canvas of canvases) { if (rendered.get(canvas) !== THEME_MODE.DARK) continue; applyPixels(canvas, THEME_MODE.LIGHT); rendered.set(canvas, THEME_MODE.LIGHT); } for (const source of sources) { if (rendered.get(source) !== THEME_MODE.DARK) continue; applyPixels(source, THEME_MODE.LIGHT, false); rendered.set(source, THEME_MODE.LIGHT); } disposed = true; observer?.disconnect(); for (const restore of restores) restore(); canvases.clear(); sources.clear(); colorCache.clear(); } }; } // shared/storage/keyValue.ts var DEFAULT_MESSAGES = { read: "[shared/storage] read failed", write: "[shared/storage] write failed", remove: "[shared/storage] remove failed", parse: "[shared/storage] invalid JSON" }; function reportFailure(options, operation, error) { if (options.onError !== void 0) { options.onError({ operation, error }); return; } console.error(DEFAULT_MESSAGES[operation], error); } function createStringStorage(storage, options = {}) { const memory = /* @__PURE__ */ new Map(); const useCache = options.cache !== false; const read = (key) => { if (useCache) { const cached = memory.get(key); if (cached !== void 0) return cached; } try { const value = storage.getItem(key); if (useCache && value !== null) memory.set(key, value); return value; } catch (error) { reportFailure(options, "read", error); return null; } }; const write = (key, value) => { if (useCache) memory.set(key, value); try { storage.setItem(key, value); return true; } catch (error) { reportFailure(options, "write", error); return false; } }; const remove = (key) => { if (useCache) memory.delete(key); try { storage.removeItem(key); return true; } catch (error) { reportFailure(options, "remove", error); return false; } }; return { read, write, readJson: (key, fallback) => { const raw = read(key); if (raw === null) return fallback; try { return JSON.parse(raw); } catch (error) { reportFailure(options, "parse", error); return fallback; } }, writeJson: (key, value) => { let serialized; try { serialized = JSON.stringify(value); } catch (error) { reportFailure(options, "write", error); return false; } return write(key, serialized); }, remove }; } function isStorageArea(value) { if (typeof value !== "object" || value === null) return false; return "getItem" in value && "setItem" in value && "removeItem" in value; } function resolveArea(kind) { const area = kind === "localStorage" ? window.localStorage : window.sessionStorage; if (!isStorageArea(area)) { throw new Error(kind + " is unavailable in this frame"); } return area; } function getBrowserStorage(kind, options = {}) { return createStringStorage( { getItem: (key) => resolveArea(kind).getItem(key), setItem: (key, value) => resolveArea(kind).setItem(key, value), removeItem: (key) => resolveArea(kind).removeItem(key) }, options ); } // shared/storage/gmStorage.ts function asGranted(value) { if (typeof value !== "function") return null; return value; } function callGranted(granted, scope, args) { const result = Reflect.apply(granted, scope, args); return result; } function resolveApi(scope) { const rawGet = asGranted(Reflect.get(scope, "GM_getValue")); const rawSet = asGranted(Reflect.get(scope, "GM_setValue")); const rawDelete = asGranted(Reflect.get(scope, "GM_deleteValue")); return { getValue: rawGet === null ? void 0 : (key, fallback) => callGranted(rawGet, scope, [key, fallback]), setValue: rawSet === null ? void 0 : (key, value) => { callGranted(rawSet, scope, [key, value]); }, deleteValue: rawDelete === null ? void 0 : (key) => { callGranted(rawDelete, scope, [key]); } }; } function createGmStorage(scope = globalThis, options = {}) { const api = resolveApi(scope); const getValue = api.getValue; const setValue = api.setValue; if (getValue === void 0 || setValue === void 0) { return options.fallback ?? createStringStorage(createMemoryStorage(), options); } const deleteValue = api.deleteValue; const backing = { getItem: (key) => { const value = getValue(key, null); if (value === null || value === void 0) return null; return typeof value === "string" ? value : JSON.stringify(value); }, setItem: (key, value) => { setValue(key, value); }, removeItem: (key) => { if (deleteValue === void 0) { throw new Error("GM_deleteValue is not granted"); } deleteValue(key); } }; return createStringStorage(backing, options); } function createMemoryStorage() { const values = /* @__PURE__ */ new Map(); return { getItem: (key) => values.get(key) ?? null, setItem: (key, value) => { values.set(key, value); }, removeItem: (key) => { values.delete(key); } }; } // scripts/sheets-theme-toggle/src/core/theme.ts function normalizeThemeMode(value) { if (value === THEME_MODE.DARK) return THEME_MODE.DARK; if (value === THEME_MODE.LIGHT) return THEME_MODE.LIGHT; return null; } function resolveThemeMode(preference, systemPrefersDark) { if (preference !== null) return preference; return systemPrefersDark ? THEME_MODE.DARK : THEME_MODE.LIGHT; } function toggleThemeMode(current) { return current === THEME_MODE.DARK ? THEME_MODE.LIGHT : THEME_MODE.DARK; } function createSystemDarkQuery(view) { if (view === null || typeof view.matchMedia !== "function") { return { matches: false, subscribe: () => () => void 0 }; } const matcher = view.matchMedia(SYSTEM_DARK_QUERY); return { get matches() { return matcher.matches === true; }, subscribe: (listener) => { const handler = () => listener(); if (typeof matcher.addEventListener === "function") { matcher.addEventListener("change", handler); return () => matcher.removeEventListener("change", handler); } matcher.addListener(handler); return () => matcher.removeListener(handler); } }; } // scripts/sheets-theme-toggle/src/core/preference.ts function createThemePreference(scope = globalThis) { const storage = createGmStorage(scope, { fallback: getBrowserStorage("localStorage", { onError: () => void 0 }) }); return { read: () => normalizeThemeMode(storage.read(STORAGE_KEY)), write: (mode) => { storage.write(STORAGE_KEY, mode); } }; } // scripts/sheets-theme-toggle/src/core/style.ts var SURFACE_DARK = "#202124"; var SURFACE_RAISED_DARK = "#292a2d"; var SURFACE_TAB_DARK = "#2d2f31"; var SURFACE_TAB_ACTIVE_DARK = "#3c4043"; var SURFACE_CONTAINER_HIGHEST_DARK = "#303134"; var TEXT_DARK = "#e8eaed"; var TEXT_ACCENT_DARK = "#8ab4f8"; var TEXT_MUTED_DARK = "#9aa0a6"; var BORDER_DARK = "#5f6368"; var ICON_LIGHT = "#444746"; var HOVER_OVERLAY_LIGHT = "rgba(68, 71, 70, 0.08)"; var ACTIVE_OVERLAY_LIGHT = "rgba(68, 71, 70, 0.16)"; var HOVER_OVERLAY_DARK = "rgba(255, 255, 255, 0.08)"; var ACTIVE_OVERLAY_DARK = "rgba(255, 255, 255, 0.16)"; function darkGate() { return `html[${THEME_ATTRIBUTE}="${THEME_MODE.DARK}"]`; } function darkRule(selectors, declarations) { const gate = darkGate(); return [ selectors.map((selector) => `${gate} ${selector}`).join(",\n") + " {", ...declarations.map((declaration) => ` ${declaration};`), "}" ].join("\n"); } var CHROME_SURFACES = Object.freeze([ "#docs-chrome", "#docs-header", "#docs-titlebar-container", "#docs-bars", "#docs-toolbar", "#docs-toolbar-wrapper", "#docs-additional-bars", "#docs-menubar", "#formula-bar-name-box-wrapper", "table#grid-bottom-bar" ]); var CHROME_ICONS = Object.freeze([ "#docs-chrome .docs-icon-img", "#docs-chrome .docs-icon-img-container", "#formula-bar .docs-icon-img", "#formula-bar .docs-icon-img-container", "#grid-bottom-bar .docs-icon-img", "#grid-bottom-bar .docs-icon-img-container", ".goog-menu .docs-icon-img", ".goog-menu .docs-icon-img-container" ]); var CHROME_TOKEN_HOSTS = Object.freeze([ "#docs-chrome", "#grid-bottom-bar", ".goog-menu" ]); var DARK_TOKENS = Object.freeze([ `--gm3-sys-color-surface: ${SURFACE_DARK}`, `--gm3-sys-color-surface-container-lowest: ${SURFACE_DARK}`, `--gm3-sys-color-surface-container-low: ${SURFACE_DARK}`, `--gm3-sys-color-surface-container: ${SURFACE_DARK}`, `--gm3-sys-color-surface-container-high: ${SURFACE_RAISED_DARK}`, `--gm3-sys-color-surface-container-highest: ${SURFACE_CONTAINER_HIGHEST_DARK}`, `--gm3-sys-color-on-surface: ${TEXT_DARK}`, "--gm3-sys-color-on-surface-variant: #c4c7c5", `--gm3-sys-color-outline: ${BORDER_DARK}`, "--gm3-sys-color-outline-variant: #444746" ]); var TOOLBAR_CONTROLS = Object.freeze([ ".menu-button.goog-control", ".goog-control", ".goog-toolbar-button", ".goog-toolbar-menu-button", ".goog-toolbar-toggle-button" ]); var TOOLBAR_LABELS = Object.freeze([ ".docs-title-input", ".docs-title-input-label", ".docs-title-input-label-inner", ".docs-title-untitled" ]); function buildThemeCss() { const gate = darkGate(); const button = `button.${BUTTON_CLASS}`; const floating = `${button}[${FLOATING_ATTRIBUTE}]`; return [ "/* Dark theme restyles the editor chrome only; the sheet keeps its pixels. */", `${gate} {`, ` background-color: ${SURFACE_DARK};`, "}", "", darkRule(CHROME_TOKEN_HOSTS, DARK_TOKENS), "", darkRule(CHROME_SURFACES, [ `background-color: ${SURFACE_DARK}`, `color: ${TEXT_DARK}`, `border-color: ${BORDER_DARK}` ]), "", // The name box and the formula input do not inherit colour from the bar. darkRule(["#formula-bar", "#t-name-box", "#t-formula-bar-input"], [ `background-color: ${SURFACE_RAISED_DARK}`, `color: ${TEXT_DARK}`, `border-color: ${BORDER_DARK}` ]), "", darkRule([...TOOLBAR_CONTROLS, ...TOOLBAR_LABELS], [`color: ${TEXT_DARK}`]), "", // Google paints hover states with a light overlay that would turn into a // bright block on a dark toolbar. Checked toggles keep Google's tonal pill, // so their glyph is left uninverted below for contrast. darkRule( [ ".goog-toolbar-button-hover", ".goog-toolbar-menu-button-hover" ], [`background-color: ${HOVER_OVERLAY_DARK}`] ), "", darkRule( [".goog-toolbar-button-active", ".goog-toolbar-menu-button-active"], [`background-color: ${ACTIVE_OVERLAY_DARK}`] ), "", darkRule( [ ".goog-toolbar-button .goog-toolbar-button-inner-box", ".goog-toolbar-menu-button .goog-toolbar-menu-button-inner-box" ], ["background-color: transparent"] ), "", darkRule([".goog-toolbar-separator"], [`border-color: ${BORDER_DARK}`]), "", darkRule([".docs-sheet-tab"], [ `background-color: ${SURFACE_TAB_DARK}`, `color: ${TEXT_DARK}` ]), "", darkRule([".docs-sheet-tab.docs-sheet-active-tab"], [ `background-color: ${SURFACE_TAB_ACTIVE_DARK}` ]), "", darkRule([".docs-sheet-tab-name"], [`color: ${TEXT_DARK}`]), "", darkRule([".docs-sheet-tab.docs-sheet-active-tab .docs-sheet-tab-name"], [ `color: ${TEXT_ACCENT_DARK}` ]), "", // Menus render in their own layer above the page, so they need the same // treatment; the palette popup keeps its swatch colours and only its frame // is darkened. darkRule([".goog-menu", ".goog-menu.goog-menu-vertical"], [ `background-color: ${SURFACE_RAISED_DARK}`, `color: ${TEXT_DARK}`, `border-color: ${BORDER_DARK}` ]), "", darkRule([".goog-menuitem", ".goog-menuitem-content"], [`color: ${TEXT_DARK}`]), "", darkRule([".goog-menuitem-highlight"], [ `background-color: ${SURFACE_TAB_ACTIVE_DARK}` ]), "", darkRule([".goog-menuitem-disabled .goog-menuitem-content"], [ `color: ${TEXT_MUTED_DARK}` ]), "", darkRule([".goog-menuseparator"], [`border-color: ${BORDER_DARK}`]), "", // The formula bar's divider is a 1px white strip, not a border. darkRule(["#docs-chrome .formula-bar-separator"], [ `background-color: ${BORDER_DARK}` ]), "", darkRule([".goog-palette"], [`background-color: ${SURFACE_DARK}`]), "", darkRule([".docs-sheet-comment-indicator-text"], [`color: ${TEXT_DARK}`]), "", // Toolbar glyphs are dark images rather than `currentColor` masks, so the // dark surfaces above would hide them. Inverting only these icon boxes (never // the sheet, its media or the colour swatches) restores them in light grey. darkRule(CHROME_ICONS, ["filter: invert(1)"]), "", // A checked toggle keeps Google's light tonal pill, so its glyph stays dark // instead of being inverted into a washed-out light grey. darkRule(["#docs-chrome .goog-toolbar-button-checked .docs-icon-img"], [ "filter: none" ]), "", "/* Toggle button: a compact icon button sized for Google's toolbar row. */", `${button} {`, " display: inline-flex;", " align-items: center;", " justify-content: center;", " box-sizing: border-box;", " width: 28px;", " height: 28px;", " margin: 0 2px;", " padding: 0;", " border: 0;", " border-radius: 50%;", " background: transparent;", ` color: ${ICON_LIGHT};`, " font: inherit;", " line-height: 1;", " vertical-align: middle;", " cursor: pointer;", "}", "", `${button}:hover {`, ` background-color: ${HOVER_OVERLAY_LIGHT};`, "}", "", `${button}:active {`, ` background-color: ${ACTIVE_OVERLAY_LIGHT};`, "}", "", `${button}:focus-visible {`, " outline: 2px solid #1a73e8;", " outline-offset: 2px;", "}", "", `${button} > svg {`, " display: block;", " width: 18px;", " height: 18px;", " pointer-events: none;", "}", "", `${gate} ${button} {`, ` color: ${TEXT_DARK};`, "}", "", `${gate} ${button}:hover {`, ` background-color: ${HOVER_OVERLAY_DARK};`, "}", "", `${gate} ${button}:active {`, ` background-color: ${ACTIVE_OVERLAY_DARK};`, "}", "", "/* Fallback: the editor chrome never rendered, so pin the button to the view. */", `${floating} {`, " position: fixed;", " right: 16px;", " bottom: 16px;", " z-index: 2147483000;", " background-color: #ffffff;", " box-shadow: 0 1px 3px rgba(60, 64, 67, 0.3), 0 4px 8px 3px rgba(60, 64, 67, 0.15);", "}", "", `${gate} ${floating} {`, ` background-color: ${SURFACE_DARK};`, "}", "" ].join("\n"); } function ensureThemeStyle(doc) { if (doc.getElementById(STYLE_ID) !== null) return; const style = doc.createElement("style"); style.id = STYLE_ID; style.textContent = buildThemeCss(); const parent = doc.head ?? doc.documentElement; if (parent === null) return; parent.append(style); } function removeThemeStyle(doc) { doc.getElementById(STYLE_ID)?.remove(); } // scripts/sheets-theme-toggle/src/ui/button.ts var SVG_NAMESPACE = "http://www.w3.org/2000/svg"; var SUN_SHAPES = Object.freeze([ { tag: "circle", attributes: { cx: "12", cy: "12", r: "4.2" } }, { tag: "path", attributes: { d: "M12 2.4v2.2M12 19.4v2.2M4.2 4.2l1.6 1.6M18.2 18.2l1.6 1.6M2.4 12h2.2M19.4 12h2.2M4.2 19.8l1.6-1.6M18.2 5.8l1.6-1.6" } } ]); var MOON_SHAPES = Object.freeze([ { tag: "path", attributes: { d: "M20.2 14.6A8.6 8.6 0 0 1 9.4 3.8a8.6 8.6 0 1 0 10.8 10.8Z" } } ]); function createIcon(doc, mode) { const icon = doc.createElementNS(SVG_NAMESPACE, "svg"); icon.setAttribute("viewBox", "0 0 24 24"); icon.setAttribute("aria-hidden", "true"); icon.setAttribute("focusable", "false"); const shapes = mode === THEME_MODE.DARK ? SUN_SHAPES : MOON_SHAPES; for (const shape of shapes) { const element = doc.createElementNS(SVG_NAMESPACE, shape.tag); element.setAttribute("fill", "none"); element.setAttribute("stroke", "currentColor"); element.setAttribute("stroke-width", "1.6"); element.setAttribute("stroke-linecap", "round"); element.setAttribute("stroke-linejoin", "round"); for (const [name, value] of Object.entries(shape.attributes)) { element.setAttribute(name, value); } icon.append(element); } return icon; } function describeThemeButton(mode) { return mode === THEME_MODE.DARK ? BUTTON_LABELS.toLight : BUTTON_LABELS.toDark; } function updateThemeButton(button, mode) { const label = describeThemeButton(mode); button.setAttribute("aria-label", label); button.setAttribute("title", label); button.setAttribute("aria-pressed", mode === THEME_MODE.DARK ? "true" : "false"); button.replaceChildren(createIcon(button.ownerDocument, mode)); } function createThemeButton(doc, mode) { const button = doc.createElement("button"); button.type = "button"; button.id = BUTTON_ID; button.className = BUTTON_CLASS; updateThemeButton(button, mode); return button; } // scripts/sheets-theme-toggle/src/ui/mount.ts function findToolbar(doc) { for (const selector of MOUNT_TARGET_SELECTORS) { const element = doc.querySelector(selector); if (element instanceof HTMLElement) return element; } return null; } function createThemeMount(options) { const { doc, button, isParseComplete } = options; const view = doc.defaultView; let timer = null; let observer = null; const mount = () => { const toolbar = findToolbar(doc); if (toolbar !== null) { if (button.parentElement !== toolbar) toolbar.append(button); button.removeAttribute(FLOATING_ATTRIBUTE); return; } if (button.isConnected) return; if (!isParseComplete() || doc.body === null) return; button.setAttribute(FLOATING_ATTRIBUTE, ""); doc.body.append(button); }; const schedule = () => { if (timer !== null) return; timer = setTimeout(() => { timer = null; mount(); }, REMOUNT_DELAY_MS); }; const isSettled = () => { if (!button.isConnected) return false; const toolbar = findToolbar(doc); if (toolbar !== null) return button.parentElement === toolbar; return button.hasAttribute(FLOATING_ATTRIBUTE); }; if (view !== null) { observer = new view.MutationObserver(() => { if (isSettled()) return; schedule(); }); const root2 = doc.documentElement; if (root2 !== null) observer.observe(root2, { childList: true, subtree: true }); } return { ensureMounted: mount, dispose: () => { if (timer !== null) clearTimeout(timer); timer = null; observer?.disconnect(); observer = null; } }; } // scripts/sheets-theme-toggle/src/install.ts function installSheetsTheme(options = {}) { const doc = options.doc ?? document; const preference = createThemePreference(options.scope ?? globalThis); const system = createSystemDarkQuery(doc.defaultView); let mode = resolveThemeMode(preference.read(), system.matches); ensureThemeStyle(doc); applyTheme(doc, mode); const canvasTheme = installCanvasTheme({ doc, ...options.scope === void 0 ? {} : { scope: options.scope } }); canvasTheme.setMode(mode); const button = createThemeButton(doc, mode); const paint = (next, persist) => { mode = next; applyTheme(doc, mode); canvasTheme.setMode(mode); updateThemeButton(button, mode); if (persist) preference.write(mode); }; const onClick = () => paint(toggleThemeMode(mode), true); button.addEventListener("click", onClick); const unsubscribe = system.subscribe(() => { if (preference.read() !== null) return; paint(resolveThemeMode(null, system.matches), false); }); const mount = createThemeMount({ doc, button, isParseComplete: () => doc.readyState !== "loading" }); mount.ensureMounted(); const onParseDone = () => mount.ensureMounted(); const waitsForParse = doc.readyState === "loading"; if (waitsForParse) doc.addEventListener("DOMContentLoaded", onParseDone, { once: true }); return { mode: () => mode, setMode: (next) => paint(next, true), dispose: () => { button.removeEventListener("click", onClick); if (waitsForParse) doc.removeEventListener("DOMContentLoaded", onParseDone); unsubscribe(); mount.dispose(); button.remove(); canvasTheme.dispose(); releaseTheme(doc); removeThemeStyle(doc); } }; } // scripts/sheets-theme-toggle/src/index.ts function bootstrap() { const root2 = document.documentElement; if (root2 === null) return; if (root2.hasAttribute(SCRIPT_MARKER_ATTRIBUTE)) return; if (!isSheetEditorPath(window.location.pathname)) return; root2.setAttribute(SCRIPT_MARKER_ATTRIBUTE, ""); installSheetsTheme(); } var root = document.documentElement; if (root === null) { document.addEventListener("DOMContentLoaded", bootstrap, { once: true }); } else { bootstrap(); } })();