refactor: move bakeStyles from view-level to per-URL setting
This commit is contained in:
+34
-21
@@ -175,6 +175,10 @@
|
|||||||
background: rgba(255, 193, 7, 0.12);
|
background: rgba(255, 193, 7, 0.12);
|
||||||
color: var(--yellow);
|
color: var(--yellow);
|
||||||
}
|
}
|
||||||
|
.badge-bake {
|
||||||
|
background: rgba(46, 204, 113, 0.1);
|
||||||
|
color: var(--green);
|
||||||
|
}
|
||||||
.card-url-preview {
|
.card-url-preview {
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
color: var(--muted);
|
color: var(--muted);
|
||||||
@@ -314,6 +318,21 @@
|
|||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
accent-color: var(--accent);
|
accent-color: var(--accent);
|
||||||
}
|
}
|
||||||
|
.bake-toggle {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 3px;
|
||||||
|
font-size: 13px;
|
||||||
|
cursor: pointer;
|
||||||
|
white-space: nowrap;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
.bake-toggle input[type="checkbox"] {
|
||||||
|
width: 14px;
|
||||||
|
height: 14px;
|
||||||
|
cursor: pointer;
|
||||||
|
accent-color: var(--green);
|
||||||
|
}
|
||||||
.hint {
|
.hint {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
color: var(--muted);
|
color: var(--muted);
|
||||||
@@ -716,11 +735,13 @@
|
|||||||
.map((v) => {
|
.map((v) => {
|
||||||
const viewUrl = origin + "/v/" + v.id;
|
const viewUrl = origin + "/v/" + v.id;
|
||||||
const urlPreview = v.urls.map((u, i) =>
|
const urlPreview = v.urls.map((u, i) =>
|
||||||
`${u.url} (${u.durationSec}s)${u.forceDarkMode ? " 🌙" : ""}`
|
`${u.url} (${u.durationSec}s)${u.forceDarkMode ? " 🌙" : ""}${u.bakeStyles ? " 🍞" : ""}`
|
||||||
).join(" → ");
|
).join(" → ");
|
||||||
|
const anyBake = v.urls.some((u) => u.bakeStyles);
|
||||||
const badges = [
|
const badges = [
|
||||||
`<span class="badge badge-${v.method}">${v.method.toUpperCase()}</span>`,
|
`<span class="badge badge-${v.method}">${v.method.toUpperCase()}</span>`,
|
||||||
v.metaRefreshEnabled ? '<span class="badge badge-refresh">META-REFRESH</span>' : "",
|
v.metaRefreshEnabled ? '<span class="badge badge-refresh">META-REFRESH</span>' : "",
|
||||||
|
anyBake ? '<span class="badge badge-bake">BAKE</span>' : "",
|
||||||
]
|
]
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.join(" ");
|
.join(" ");
|
||||||
@@ -812,7 +833,7 @@
|
|||||||
const idAttr = isEdit ? ` data-id="${existing.id}"` : "";
|
const idAttr = isEdit ? ` data-id="${existing.id}"` : "";
|
||||||
const urls = existing ? existing.urls : [{ url: "", durationSec: 30 }];
|
const urls = existing ? existing.urls : [{ url: "", durationSec: 30 }];
|
||||||
let urlRowsHTML = urls
|
let urlRowsHTML = urls
|
||||||
.map((u, i) => buildUrlRowHTML(u.url, u.durationSec, i, urls.length, u.forceDarkMode))
|
.map((u, i) => buildUrlRowHTML(u.url, u.durationSec, i, urls.length, u.forceDarkMode, u.bakeStyles))
|
||||||
.join("");
|
.join("");
|
||||||
const method = existing ? existing.method : "ssr";
|
const method = existing ? existing.method : "ssr";
|
||||||
const metaChecked = existing ? existing.metaRefreshEnabled : false;
|
const metaChecked = existing ? existing.metaRefreshEnabled : false;
|
||||||
@@ -820,8 +841,6 @@
|
|||||||
const cacheTtl = existing ? existing.cacheTtlSec : 60;
|
const cacheTtl = existing ? existing.cacheTtlSec : 60;
|
||||||
const vpW = existing ? existing.viewportWidth : 1920;
|
const vpW = existing ? existing.viewportWidth : 1920;
|
||||||
const vpH = existing ? existing.viewportHeight : 1080;
|
const vpH = existing ? existing.viewportHeight : 1080;
|
||||||
const bakeStylesChecked = existing ? existing.bakeStyles : false;
|
|
||||||
const bakeSectionStyle = method === "ssr" ? "" : "display:none;";
|
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<h2>${title}</h2>
|
<h2>${title}</h2>
|
||||||
@@ -855,14 +874,6 @@
|
|||||||
<div class="hint" style="margin-top:-8px;margin-bottom:16px;">TV reloads when rotation advances. Required for multi-URL on non-JS browsers.</div>
|
<div class="hint" style="margin-top:-8px;margin-bottom:16px;">TV reloads when rotation advances. Required for multi-URL on non-JS browsers.</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="bake-section" style="${bakeSectionStyle}">
|
|
||||||
<div class="checkbox-row">
|
|
||||||
<input type="checkbox" id="bake-styles" ${bakeStylesChecked ? "checked" : ""}>
|
|
||||||
<label for="bake-styles">Bake computed styles into HTML (Samsung TV compatibility)</label>
|
|
||||||
</div>
|
|
||||||
<div class="hint" style="margin-top:-8px;margin-bottom:16px;">Fixes styling issues on browsers with limited CSS support (e.g., DuckDuckGo). Increases HTML size.</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label>Cache TTL (seconds)</label>
|
<label>Cache TTL (seconds)</label>
|
||||||
<input type="number" id="view-cache-ttl" value="${cacheTtl}" min="0" max="86400" required>
|
<input type="number" id="view-cache-ttl" value="${cacheTtl}" min="0" max="86400" required>
|
||||||
@@ -889,11 +900,12 @@
|
|||||||
</form>`;
|
</form>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildUrlRowHTML(url, durationSec, index, total, forceDarkMode) {
|
function buildUrlRowHTML(url, durationSec, index, total, forceDarkMode, bakeStyles) {
|
||||||
const isSingle = total <= 1;
|
const isSingle = total <= 1;
|
||||||
const disabledAttr = isSingle ? "disabled" : "";
|
const disabledAttr = isSingle ? "disabled" : "";
|
||||||
const grayStyle = isSingle ? 'style="opacity:0.4;"' : "";
|
const grayStyle = isSingle ? 'style="opacity:0.4;"' : "";
|
||||||
const darkChecked = forceDarkMode ? "checked" : "";
|
const darkChecked = forceDarkMode ? "checked" : "";
|
||||||
|
const bakeChecked = bakeStyles ? "checked" : "";
|
||||||
return `
|
return `
|
||||||
<div class="url-row" data-index="${index}">
|
<div class="url-row" data-index="${index}">
|
||||||
<input type="url" placeholder="https://example.com/slideshow" value="${esc(url || "")}" class="url-input" required>
|
<input type="url" placeholder="https://example.com/slideshow" value="${esc(url || "")}" class="url-input" required>
|
||||||
@@ -902,6 +914,10 @@
|
|||||||
<input type="checkbox" class="dark-mode-input" ${darkChecked}>
|
<input type="checkbox" class="dark-mode-input" ${darkChecked}>
|
||||||
🌙
|
🌙
|
||||||
</label>
|
</label>
|
||||||
|
<label class="bake-toggle" title="Bake computed styles (Samsung TV compatibility)">
|
||||||
|
<input type="checkbox" class="bake-styles-input" ${bakeChecked}>
|
||||||
|
🍞
|
||||||
|
</label>
|
||||||
<button type="button" class="btn btn-icon btn-danger btn-sm" onclick="removeUrlRow(this)" ${isSingle ? "disabled" : ""}>✕</button>
|
<button type="button" class="btn btn-icon btn-danger btn-sm" onclick="removeUrlRow(this)" ${isSingle ? "disabled" : ""}>✕</button>
|
||||||
</div>`;
|
</div>`;
|
||||||
}
|
}
|
||||||
@@ -914,7 +930,7 @@
|
|||||||
const container = document.getElementById("url-rows");
|
const container = document.getElementById("url-rows");
|
||||||
const rows = container.querySelectorAll(".url-row");
|
const rows = container.querySelectorAll(".url-row");
|
||||||
const div = document.createElement("div");
|
const div = document.createElement("div");
|
||||||
div.innerHTML = buildUrlRowHTML("", 30, rows.length, rows.length + 1, false);
|
div.innerHTML = buildUrlRowHTML("", 30, rows.length, rows.length + 1, false, false);
|
||||||
container.appendChild(div.firstElementChild);
|
container.appendChild(div.firstElementChild);
|
||||||
updateUrlRowStates();
|
updateUrlRowStates();
|
||||||
}
|
}
|
||||||
@@ -946,7 +962,6 @@
|
|||||||
function onMethodChange() {
|
function onMethodChange() {
|
||||||
const method = document.getElementById("view-method").value;
|
const method = document.getElementById("view-method").value;
|
||||||
document.getElementById("meta-section").style.display = method === "ssr" ? "" : "none";
|
document.getElementById("meta-section").style.display = method === "ssr" ? "" : "none";
|
||||||
document.getElementById("bake-section").style.display = method === "ssr" ? "" : "none";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ═══════════════════════════════════════════════
|
// ═══════════════════════════════════════════════
|
||||||
@@ -962,7 +977,6 @@
|
|||||||
const name = document.getElementById("view-name").value.trim();
|
const name = document.getElementById("view-name").value.trim();
|
||||||
const method = document.getElementById("view-method").value;
|
const method = document.getElementById("view-method").value;
|
||||||
const metaRefreshEnabled = document.getElementById("meta-refresh").checked;
|
const metaRefreshEnabled = document.getElementById("meta-refresh").checked;
|
||||||
const bakeStyles = document.getElementById("bake-styles")?.checked ?? false;
|
|
||||||
const cacheTtlSec = parseInt(document.getElementById("view-cache-ttl").value) || 60;
|
const cacheTtlSec = parseInt(document.getElementById("view-cache-ttl").value) || 60;
|
||||||
const viewportWidth = parseInt(document.getElementById("viewport-width").value) || 1920;
|
const viewportWidth = parseInt(document.getElementById("viewport-width").value) || 1920;
|
||||||
const viewportHeight = parseInt(document.getElementById("viewport-height").value) || 1080;
|
const viewportHeight = parseInt(document.getElementById("viewport-height").value) || 1080;
|
||||||
@@ -972,10 +986,12 @@
|
|||||||
const urlVal = row.querySelector(".url-input").value.trim();
|
const urlVal = row.querySelector(".url-input").value.trim();
|
||||||
if (!urlVal) continue;
|
if (!urlVal) continue;
|
||||||
const forceDarkMode = row.querySelector(".dark-mode-input")?.checked ?? false;
|
const forceDarkMode = row.querySelector(".dark-mode-input")?.checked ?? false;
|
||||||
|
const bakeStyles = row.querySelector(".bake-styles-input")?.checked ?? false;
|
||||||
urls.push({
|
urls.push({
|
||||||
url: urlVal,
|
url: urlVal,
|
||||||
durationSec: parseInt(row.querySelector(".duration-input").value) || 30,
|
durationSec: parseInt(row.querySelector(".duration-input").value) || 30,
|
||||||
forceDarkMode,
|
forceDarkMode,
|
||||||
|
bakeStyles,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -988,11 +1004,7 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = { name, urls, method, cacheTtlSec, viewportWidth, viewportHeight };
|
const body = { name, urls, method, cacheTtlSec, viewportWidth, viewportHeight, metaRefreshEnabled };
|
||||||
if (method === "ssr") {
|
|
||||||
body.metaRefreshEnabled = metaRefreshEnabled;
|
|
||||||
body.bakeStyles = bakeStyles;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (isEdit) {
|
if (isEdit) {
|
||||||
@@ -1049,6 +1061,7 @@
|
|||||||
debugSocket.close();
|
debugSocket.close();
|
||||||
debugSocket = null;
|
debugSocket = null;
|
||||||
}
|
}
|
||||||
|
if (debugTickInterval) { clearInterval(debugTickInterval); debugTickInterval = null; }
|
||||||
}
|
}
|
||||||
|
|
||||||
function setDebugInterval(ms) {
|
function setDebugInterval(ms) {
|
||||||
|
|||||||
+61
-45
@@ -26,6 +26,7 @@ interface URLItem {
|
|||||||
url: string;
|
url: string;
|
||||||
durationSec: number;
|
durationSec: number;
|
||||||
forceDarkMode?: boolean;
|
forceDarkMode?: boolean;
|
||||||
|
bakeStyles?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface View {
|
interface View {
|
||||||
@@ -37,7 +38,6 @@ interface View {
|
|||||||
cacheTtlSec: number;
|
cacheTtlSec: number;
|
||||||
viewportWidth: number;
|
viewportWidth: number;
|
||||||
viewportHeight: number;
|
viewportHeight: number;
|
||||||
bakeStyles: boolean;
|
|
||||||
createdAt: number;
|
createdAt: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +50,6 @@ interface ViewRow {
|
|||||||
cache_ttl_sec: number;
|
cache_ttl_sec: number;
|
||||||
viewport_width: number;
|
viewport_width: number;
|
||||||
viewport_height: number;
|
viewport_height: number;
|
||||||
bake_styles: number;
|
|
||||||
created_at: number;
|
created_at: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +61,6 @@ interface CreateViewBody {
|
|||||||
cacheTtlSec?: number;
|
cacheTtlSec?: number;
|
||||||
viewportWidth?: number;
|
viewportWidth?: number;
|
||||||
viewportHeight?: number;
|
viewportHeight?: number;
|
||||||
bakeStyles?: boolean;
|
|
||||||
}
|
}
|
||||||
type UpdateViewBody = Partial<CreateViewBody>;
|
type UpdateViewBody = Partial<CreateViewBody>;
|
||||||
|
|
||||||
@@ -141,7 +139,6 @@ function rowToView(row: ViewRow): View {
|
|||||||
cacheTtlSec: row.cache_ttl_sec ?? 60,
|
cacheTtlSec: row.cache_ttl_sec ?? 60,
|
||||||
viewportWidth: row.viewport_width ?? 1920,
|
viewportWidth: row.viewport_width ?? 1920,
|
||||||
viewportHeight: row.viewport_height ?? 1080,
|
viewportHeight: row.viewport_height ?? 1080,
|
||||||
bakeStyles: row.bake_styles === 1,
|
|
||||||
createdAt: row.created_at,
|
createdAt: row.created_at,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -159,8 +156,8 @@ function dbLoad(id: string): View | null {
|
|||||||
|
|
||||||
function dbInsert(v: View): void {
|
function dbInsert(v: View): void {
|
||||||
db.run(
|
db.run(
|
||||||
`INSERT INTO views (id,name,urls,method,meta_refresh_enabled,cache_ttl_sec,viewport_width,viewport_height,bake_styles,created_at)
|
`INSERT INTO views (id,name,urls,method,meta_refresh_enabled,cache_ttl_sec,viewport_width,viewport_height,created_at)
|
||||||
VALUES (?,?,?,?,?,?,?,?,?,?)`,
|
VALUES (?,?,?,?,?,?,?,?,?)`,
|
||||||
[
|
[
|
||||||
v.id,
|
v.id,
|
||||||
v.name,
|
v.name,
|
||||||
@@ -170,7 +167,6 @@ function dbInsert(v: View): void {
|
|||||||
v.cacheTtlSec,
|
v.cacheTtlSec,
|
||||||
v.viewportWidth,
|
v.viewportWidth,
|
||||||
v.viewportHeight,
|
v.viewportHeight,
|
||||||
v.bakeStyles ? 1 : 0,
|
|
||||||
v.createdAt,
|
v.createdAt,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
@@ -178,7 +174,7 @@ function dbInsert(v: View): void {
|
|||||||
|
|
||||||
function dbUpdate(v: View): void {
|
function dbUpdate(v: View): void {
|
||||||
db.run(
|
db.run(
|
||||||
"UPDATE views SET name=?,urls=?,method=?,meta_refresh_enabled=?,cache_ttl_sec=?,viewport_width=?,viewport_height=?,bake_styles=? WHERE id=?",
|
"UPDATE views SET name=?,urls=?,method=?,meta_refresh_enabled=?,cache_ttl_sec=?,viewport_width=?,viewport_height=? WHERE id=?",
|
||||||
[
|
[
|
||||||
v.name,
|
v.name,
|
||||||
JSON.stringify(v.urls),
|
JSON.stringify(v.urls),
|
||||||
@@ -187,7 +183,6 @@ function dbUpdate(v: View): void {
|
|||||||
v.cacheTtlSec,
|
v.cacheTtlSec,
|
||||||
v.viewportWidth,
|
v.viewportWidth,
|
||||||
v.viewportHeight,
|
v.viewportHeight,
|
||||||
v.bakeStyles ? 1 : 0,
|
|
||||||
v.id,
|
v.id,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
@@ -521,9 +516,11 @@ function drainRenderQueue(): void {
|
|||||||
inFlightRenders.set(next.key, { promise: renderPromise, urlIndex: next.urlIndex });
|
inFlightRenders.set(next.key, { promise: renderPromise, urlIndex: next.urlIndex });
|
||||||
renderPromise.then(
|
renderPromise.then(
|
||||||
(body) => {
|
(body) => {
|
||||||
|
inFlightRenders.delete(next.key);
|
||||||
next.resolve(body);
|
next.resolve(body);
|
||||||
},
|
},
|
||||||
(err) => {
|
(err) => {
|
||||||
|
inFlightRenders.delete(next.key);
|
||||||
next.reject(err);
|
next.reject(err);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -533,7 +530,7 @@ function drainRenderQueue(): void {
|
|||||||
async function renderViewToCache(view: View, urlIndex: number, key: string): Promise<string> {
|
async function renderViewToCache(view: View, urlIndex: number, key: string): Promise<string> {
|
||||||
const urlItem = view.urls[urlIndex];
|
const urlItem = view.urls[urlIndex];
|
||||||
const forceDarkMode = urlItem.forceDarkMode ?? false;
|
const forceDarkMode = urlItem.forceDarkMode ?? false;
|
||||||
const bakeStyles = view.bakeStyles ?? false;
|
const bakeStyles = urlItem.bakeStyles ?? false;
|
||||||
|
|
||||||
let body: string;
|
let body: string;
|
||||||
if (view.method === "mjpeg") {
|
if (view.method === "mjpeg") {
|
||||||
@@ -620,7 +617,7 @@ function cacheStats(): Array<{
|
|||||||
}> = [];
|
}> = [];
|
||||||
|
|
||||||
for (const [key, entry] of renderCache) {
|
for (const [key, entry] of renderCache) {
|
||||||
const [viewId, idxStr] = key.split(":");
|
const [viewId] = key.split(":");
|
||||||
const view = viewCacheGet(viewId);
|
const view = viewCacheGet(viewId);
|
||||||
result.push({
|
result.push({
|
||||||
key,
|
key,
|
||||||
@@ -729,15 +726,11 @@ async function resolveCSSImports(cssText: string, baseUrl: string, depth = 0): P
|
|||||||
/**
|
/**
|
||||||
* Bake computed styles into inline style attributes.
|
* Bake computed styles into inline style attributes.
|
||||||
* This ensures styles work on browsers with limited CSS support (e.g., Samsung TV).
|
* This ensures styles work on browsers with limited CSS support (e.g., Samsung TV).
|
||||||
* Only bakes styles that differ from the element's default computed styles.
|
* Compares each element against a same-tagname fresh element so tag-specific defaults
|
||||||
|
* (e.g. block display on div, inline on span, bold on h1) are accounted for.
|
||||||
*/
|
*/
|
||||||
async function bakeComputedStyles(page: Page): Promise<void> {
|
async function bakeComputedStyles(page: Page): Promise<void> {
|
||||||
await page.evaluate((): void => {
|
await page.evaluate((): void => {
|
||||||
// Create a dummy element to get default styles for comparison
|
|
||||||
const dummy = document.createElement("div");
|
|
||||||
dummy.style.cssText = "position:absolute;visibility:hidden;";
|
|
||||||
document.body.appendChild(dummy);
|
|
||||||
|
|
||||||
const computedProperties = [
|
const computedProperties = [
|
||||||
// Layout
|
// Layout
|
||||||
"display", "position", "top", "right", "bottom", "left",
|
"display", "position", "top", "right", "bottom", "left",
|
||||||
@@ -749,64 +742,86 @@ async function bakeComputedStyles(page: Page): Promise<void> {
|
|||||||
"justify-content", "align-items", "align-content", "align-self", "gap", "row-gap", "column-gap",
|
"justify-content", "align-items", "align-content", "align-self", "gap", "row-gap", "column-gap",
|
||||||
"grid", "grid-template", "grid-template-columns", "grid-template-rows", "grid-area",
|
"grid", "grid-template", "grid-template-columns", "grid-template-rows", "grid-area",
|
||||||
// Box model
|
// Box model
|
||||||
"box-sizing", "border", "border-radius", "border-top", "border-right", "border-bottom", "border-left",
|
"box-sizing", "border-top-width", "border-right-width", "border-bottom-width", "border-left-width",
|
||||||
"border-width", "border-style", "border-color",
|
"border-top-style", "border-right-style", "border-bottom-style", "border-left-style",
|
||||||
|
"border-top-color", "border-right-color", "border-bottom-color", "border-left-color",
|
||||||
|
"border-top-left-radius", "border-top-right-radius", "border-bottom-right-radius", "border-bottom-left-radius",
|
||||||
// Colors & Background
|
// Colors & Background
|
||||||
"color", "background", "background-color", "background-image", "background-size", "background-position",
|
"color", "background-color", "background-image", "background-size", "background-position",
|
||||||
"background-repeat", "background-attachment",
|
"background-repeat", "background-attachment", "background-clip", "background-origin",
|
||||||
// Typography
|
// Typography
|
||||||
"font", "font-family", "font-size", "font-weight", "font-style", "font-variant",
|
"font-family", "font-size", "font-weight", "font-style", "font-variant",
|
||||||
"line-height", "text-align", "text-decoration", "text-transform", "letter-spacing", "word-spacing",
|
"line-height", "text-align", "text-decoration-line", "text-decoration-color",
|
||||||
|
"text-transform", "letter-spacing", "word-spacing",
|
||||||
"white-space", "overflow-wrap", "word-break",
|
"white-space", "overflow-wrap", "word-break",
|
||||||
// Visual
|
// Visual
|
||||||
"opacity", "visibility", "z-index", "overflow", "overflow-x", "overflow-y",
|
"opacity", "visibility", "z-index", "overflow-x", "overflow-y",
|
||||||
"box-shadow", "text-shadow", "transform", "transform-origin",
|
"box-shadow", "text-shadow", "transform", "transform-origin",
|
||||||
// List/Table
|
// List/Table
|
||||||
"list-style", "border-collapse", "border-spacing",
|
"list-style-type", "list-style-position", "border-collapse", "border-spacing",
|
||||||
|
// Cursor/pointer
|
||||||
|
"cursor", "pointer-events",
|
||||||
];
|
];
|
||||||
|
|
||||||
function getDefaultValue(prop: string): string {
|
// Cache of default computed values keyed by tagName
|
||||||
return window.getComputedStyle(dummy).getPropertyValue(prop);
|
const defaultCache = new Map<string, Map<string, string>>();
|
||||||
|
const hiddenContainer = document.createElement("div");
|
||||||
|
hiddenContainer.style.cssText = "position:absolute;visibility:hidden;pointer-events:none;top:-9999px;";
|
||||||
|
document.body.appendChild(hiddenContainer);
|
||||||
|
|
||||||
|
function getTagDefaults(tag: string): Map<string, string> {
|
||||||
|
let cache = defaultCache.get(tag);
|
||||||
|
if (cache) return cache;
|
||||||
|
cache = new Map<string, string>();
|
||||||
|
try {
|
||||||
|
const el = document.createElement(tag);
|
||||||
|
hiddenContainer.appendChild(el);
|
||||||
|
const cs = window.getComputedStyle(el);
|
||||||
|
for (const prop of computedProperties) {
|
||||||
|
cache.set(prop, cs.getPropertyValue(prop));
|
||||||
|
}
|
||||||
|
el.remove();
|
||||||
|
} catch { /* unknown tag, cache will be empty */ }
|
||||||
|
defaultCache.set(tag, cache);
|
||||||
|
return cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
function shouldBake(prop: string, value: string): boolean {
|
function shouldBake(value: string, defaultValue: string): boolean {
|
||||||
if (!value || value === "none" || value === "auto" || value === "normal") return false;
|
if (!value) return false;
|
||||||
if (value === "0px" || value === "0" || value === "rgba(0, 0, 0, 0)") return false;
|
// Skip browser keywords that cannot be serialized as inline style values
|
||||||
if (value.includes("initial") || value.includes("inherit")) return false;
|
if (value === "initial" || value === "inherit" || value === "unset" || value === "revert") return false;
|
||||||
return value !== getDefaultValue(prop);
|
return value !== defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Walk all elements and bake computed styles
|
// Walk all elements and bake computed styles
|
||||||
const elements = document.querySelectorAll<HTMLElement>("*");
|
const elements = document.querySelectorAll<HTMLElement>("*");
|
||||||
for (const el of elements) {
|
for (const el of elements) {
|
||||||
// Skip script, style, meta, link tags
|
if (el === hiddenContainer) continue;
|
||||||
|
// Skip non-visual tags
|
||||||
const tag = el.tagName.toLowerCase();
|
const tag = el.tagName.toLowerCase();
|
||||||
if (["script", "style", "meta", "link", "noscript", "template"].includes(tag)) continue;
|
if (["script", "style", "meta", "link", "noscript", "template", "head", "html"].includes(tag)) continue;
|
||||||
|
|
||||||
const computed = window.getComputedStyle(el);
|
const computed = window.getComputedStyle(el);
|
||||||
|
const defaults = getTagDefaults(tag);
|
||||||
const stylesToBake: string[] = [];
|
const stylesToBake: string[] = [];
|
||||||
|
|
||||||
for (const prop of computedProperties) {
|
for (const prop of computedProperties) {
|
||||||
const value = computed.getPropertyValue(prop);
|
const value = computed.getPropertyValue(prop);
|
||||||
if (shouldBake(prop, value)) {
|
const defaultValue = defaults.get(prop) ?? "";
|
||||||
|
if (shouldBake(value, defaultValue)) {
|
||||||
stylesToBake.push(`${prop}:${value}`);
|
stylesToBake.push(`${prop}:${value}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stylesToBake.length > 0) {
|
if (stylesToBake.length > 0) {
|
||||||
// Merge with existing inline styles, preserving user-set inline styles
|
// Existing inline styles take precedence — put them after baked styles
|
||||||
const existingInline = el.getAttribute("style") || "";
|
const existingInline = el.getAttribute("style") || "";
|
||||||
const bakedStyles = stylesToBake.join(";");
|
const bakedStyles = stylesToBake.join(";");
|
||||||
if (existingInline) {
|
el.setAttribute("style", existingInline ? `${bakedStyles};${existingInline}` : bakedStyles);
|
||||||
// User inline styles take precedence - put them last
|
|
||||||
el.setAttribute("style", `${bakedStyles};${existingInline}`);
|
|
||||||
} else {
|
|
||||||
el.setAttribute("style", bakedStyles);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dummy.remove();
|
hiddenContainer.remove();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1169,13 +1184,13 @@ async function handleAPI(req: Request, path: string): Promise<Response> {
|
|||||||
url: u.url.trim(),
|
url: u.url.trim(),
|
||||||
durationSec: Math.max(1, Math.round(u.durationSec)),
|
durationSec: Math.max(1, Math.round(u.durationSec)),
|
||||||
forceDarkMode: u.forceDarkMode ?? false,
|
forceDarkMode: u.forceDarkMode ?? false,
|
||||||
|
bakeStyles: u.bakeStyles ?? false,
|
||||||
})),
|
})),
|
||||||
method: body.method,
|
method: body.method,
|
||||||
metaRefreshEnabled: body.metaRefreshEnabled ?? false,
|
metaRefreshEnabled: body.metaRefreshEnabled ?? false,
|
||||||
cacheTtlSec: Math.max(0, body.cacheTtlSec ?? 60),
|
cacheTtlSec: Math.max(0, body.cacheTtlSec ?? 60),
|
||||||
viewportWidth: Math.max(320, Math.min(7680, body.viewportWidth ?? 1920)),
|
viewportWidth: Math.max(320, Math.min(7680, body.viewportWidth ?? 1920)),
|
||||||
viewportHeight: Math.max(240, Math.min(4320, body.viewportHeight ?? 1080)),
|
viewportHeight: Math.max(240, Math.min(4320, body.viewportHeight ?? 1080)),
|
||||||
bakeStyles: body.bakeStyles ?? false,
|
|
||||||
createdAt: Math.floor(Date.now() / 1000),
|
createdAt: Math.floor(Date.now() / 1000),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1212,6 +1227,7 @@ async function handleAPI(req: Request, path: string): Promise<Response> {
|
|||||||
url: u.url.trim(),
|
url: u.url.trim(),
|
||||||
durationSec: Math.max(1, Math.round(u.durationSec)),
|
durationSec: Math.max(1, Math.round(u.durationSec)),
|
||||||
forceDarkMode: u.forceDarkMode ?? false,
|
forceDarkMode: u.forceDarkMode ?? false,
|
||||||
|
bakeStyles: u.bakeStyles ?? false,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
if (body.method !== undefined) {
|
if (body.method !== undefined) {
|
||||||
@@ -1226,7 +1242,6 @@ async function handleAPI(req: Request, path: string): Promise<Response> {
|
|||||||
view.viewportWidth = Math.max(320, Math.min(7680, body.viewportWidth));
|
view.viewportWidth = Math.max(320, Math.min(7680, body.viewportWidth));
|
||||||
if (body.viewportHeight !== undefined)
|
if (body.viewportHeight !== undefined)
|
||||||
view.viewportHeight = Math.max(240, Math.min(4320, body.viewportHeight));
|
view.viewportHeight = Math.max(240, Math.min(4320, body.viewportHeight));
|
||||||
if (body.bakeStyles !== undefined) view.bakeStyles = body.bakeStyles;
|
|
||||||
|
|
||||||
dbUpdate(view);
|
dbUpdate(view);
|
||||||
cacheInvalidate(viewId);
|
cacheInvalidate(viewId);
|
||||||
@@ -1258,6 +1273,7 @@ const STATIC_DIR = new URL("../public", import.meta.url).pathname;
|
|||||||
|
|
||||||
async function serveStatic(path: string): Promise<Response | null> {
|
async function serveStatic(path: string): Promise<Response | null> {
|
||||||
const filePath = path === "/" ? "/index.html" : path;
|
const filePath = path === "/" ? "/index.html" : path;
|
||||||
|
if (!filePath.startsWith("/") || filePath.includes("..")) return null;
|
||||||
try {
|
try {
|
||||||
const file = Bun.file(STATIC_DIR + filePath);
|
const file = Bun.file(STATIC_DIR + filePath);
|
||||||
await file.slice(0, 0).text();
|
await file.slice(0, 0).text();
|
||||||
|
|||||||
Reference in New Issue
Block a user