import { acceleratedCanvasTrackerSource } from "../pageMotionBlur.js";
import { parseHTML } from "linkedom";
export const RUNTIME_BOOTSTRAP_ATTR = "data-hyperframes-preview-runtime";
const RUNTIME_FILES = [
"hyperframe.runtime.iife.js",
"hyperframes-runtime.modular.inline.js",
"hyperframe-runtime.modular-runtime.inline.js",
];
const SIMPLE_RUNTIME_FLAG_ASSIGNMENTS = [
/^window\.__playerReady\s*=\s*(?:true|false)\s*;?$/,
/^window\.__renderReady\s*=\s*(?:true|false)\s*;?$/,
];
const LEADING_COMMENTS = /^(?:\s|))*/;
export function isTemplateEntry(html: string): boolean {
return /^/]/i.test(html.replace(LEADING_COMMENTS, ""));
}
export function isFullHtmlDocument(html: string): boolean {
return /^(?:/])/i.test(html.replace(LEADING_COMMENTS, ""));
}
/**
* Parse a full HTML document or wrap a fragment so linkedom consistently puts
* fragment content under document.body.
*/
export function parseHTMLContent(html: string): Document {
if (isFullHtmlDocument(html)) {
return parseHTML(html).document;
}
return parseHTML(`${html}`).document;
}
/** ASCII-only chunks bound match arrays; starting at uppercase preserves unchanged spans. */
function lowerAscii(text: string): string {
return text.replace(/[A-Z][\s\S]{0,65535}/g, (chunk) =>
chunk.replace(/[A-Z]+/g, (letters) => letters.toLowerCase()),
);
}
export function stripEmbeddedRuntimeScripts(html: string): string {
if (!html) return html;
const loweredHtml = lowerAscii(html);
let output = "";
let cursor = 0;
while (cursor < html.length) {
const scriptStart = findScriptStart(loweredHtml, cursor);
if (scriptStart === -1) {
output += html.slice(cursor);
break;
}
output += html.slice(cursor, scriptStart);
const startTagEnd = findTagEnd(html, scriptStart + 1);
if (startTagEnd === -1) {
output += html.slice(scriptStart);
break;
}
const closeTagEnd = findScriptCloseTagEnd(loweredHtml, startTagEnd + 1);
const scriptEnd = closeTagEnd === -1 ? html.length : closeTagEnd;
const block = html.slice(scriptStart, scriptEnd);
if (!shouldStripRuntimeScriptBlock(block, html.slice(scriptStart, startTagEnd + 1))) {
output += block;
}
cursor = scriptEnd;
}
return output;
}
function findScriptStart(loweredHtml: string, from: number): number {
let index = loweredHtml.indexOf("" ? cursor + 1 : -1;
}
function shouldStripRuntimeScriptBlock(block: string, startTag: string): boolean {
const script = parseHTML(`${startTag}`).document.querySelector("script");
if (
script?.hasAttribute(RUNTIME_BOOTSTRAP_ATTR) ||
isRuntimeFileUrl(script?.getAttribute("src"))
) {
return true;
}
const scriptSource = getScriptSource(block).trim();
return SIMPLE_RUNTIME_FLAG_ASSIGNMENTS.some((pattern) => pattern.test(scriptSource));
}
export function isRuntimeFileUrl(src: string | null | undefined): boolean {
const path = lowerAscii(src?.split(/[?#]/, 1)[0] ?? "");
return RUNTIME_FILES.some((file) => path === file || path.endsWith(`/${file}`));
}
function getScriptSource(block: string): string {
const startTagEnd = findTagEnd(block, 1);
if (startTagEnd === -1) return "";
const loweredBlock = lowerAscii(block);
const closeTagStart = loweredBlock.lastIndexOf("" || char === "/" || isHtmlWhitespace(char);
}
function isHtmlWhitespace(char: string): boolean {
return char === " " || char === "\n" || char === "\t" || char === "\r" || char === "\f";
}
export function escapeInlineScriptSource(source: string): string {
return escapeCaseInsensitiveToken(
escapeCaseInsensitiveToken(source, " `<\\/${match.slice(2)}`),
"", open) || lowered.startsWith("", open)) {
return lowered.indexOf(">", open) + 1;
}
COMMENT_END.lastIndex = open + 4;
const end = COMMENT_END.exec(lowered);
return end ? end.index + end[0].length : -1;
}
function skipRawText(
lowered: string,
open: number,
tagEnd: number,
unclosedRawText: Set