mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
* fix(studio): a service worker a project left behind is gone before a preview frame loads * fix(cli): the player page's inlined script list is escaped for a script tag
103 lines
3.9 KiB
TypeScript
103 lines
3.9 KiB
TypeScript
import { isWebKit } from "./runtime/mediaClock.js";
|
|
|
|
export const STUDIO_PREVIEW_MARK_META = "hyperframes-studio-preview";
|
|
export const PREVIEW_SEEK_HOLD_ATTR = "data-hf-preview-seek-hold";
|
|
export const PREVIEW_VIDEO_FRAME_ATTR = "data-hf-preview-video-frame";
|
|
export const PREVIEW_VIDEO_SOURCE_ATTR = "data-hf-preview-video-source";
|
|
export const PREVIEW_VIDEO_CANVAS_ATTR = "data-hf-preview-video-canvas";
|
|
export const STUDIO_PREVIEW_CAPTURE_MODE = "capture";
|
|
export const PREVIEW_FOR_ANOTHER_SITE_META = "hyperframes-preview-for-another-site";
|
|
|
|
export function servedToAnotherSite(doc: Document): boolean {
|
|
return doc.querySelector(`meta[name="${PREVIEW_FOR_ANOTHER_SITE_META}"]`) !== null;
|
|
}
|
|
|
|
export function markedForStudio(doc: Document): boolean {
|
|
return (
|
|
doc.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`) !== null &&
|
|
!servedToAnotherSite(doc)
|
|
);
|
|
}
|
|
|
|
export function getLogicalOpacity(el: Element, computed: CSSStyleDeclaration): string {
|
|
const overlay = el.previousElementSibling;
|
|
if (
|
|
el.hasAttribute(PREVIEW_VIDEO_FRAME_ATTR) &&
|
|
overlay?.localName === "canvas" &&
|
|
overlay.hasAttribute(PREVIEW_VIDEO_CANVAS_ATTR)
|
|
)
|
|
return el.ownerDocument.defaultView?.getComputedStyle(overlay).opacity ?? computed.opacity;
|
|
return computed.opacity;
|
|
}
|
|
export const PLAYER_FRAME_ATTR = "data-hf-player-frame";
|
|
|
|
export function inPlayerFrame(doc: Document): boolean {
|
|
return isWebKit() && doc.defaultView?.frameElement?.hasAttribute(PLAYER_FRAME_ATTR) === true;
|
|
}
|
|
|
|
export function defersMediaIn(doc: Document): boolean {
|
|
return (
|
|
doc.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`) !== null || inPlayerFrame(doc)
|
|
);
|
|
}
|
|
export function capturePageIn(doc: Document): boolean {
|
|
return (
|
|
doc.querySelector(`meta[name="${STUDIO_PREVIEW_MARK_META}"]`)?.getAttribute("content") ===
|
|
STUDIO_PREVIEW_CAPTURE_MODE
|
|
);
|
|
}
|
|
export const STUDIO_PREVIEW_UPCOMING_ATTR = "data-hf-upcoming";
|
|
export const STUDIO_PREVIEW_LAZY_ATTR = "data-hf-preview-lazy";
|
|
export const STUDIO_PREVIEW_ERRORS = "__hfPreviewErrors";
|
|
export const STUDIO_PREVIEW_ERROR_KINDS = "__hfPreviewErrorKinds";
|
|
export function previewConsoleErrorText(args: readonly unknown[]): string | null {
|
|
const text = args.map((a) => (a instanceof Error ? a.message : String(a))).join(" ");
|
|
return text.includes("favicon") ? null : text;
|
|
}
|
|
|
|
export const refusedPreviewMessage = (status: number) =>
|
|
`The preview page could not be loaded (HTTP ${status}).`;
|
|
export function refusedPreviewStatus(message: string): number | null {
|
|
const status = /^The preview page could not be loaded \(HTTP (\d{3})\)\.$/.exec(message)?.[1];
|
|
return status ? Number(status) : null;
|
|
}
|
|
export const PREVIEW_RASTER_ATTR = "data-hf-preview-raster";
|
|
export const RUNTIME_STAMPED_ATTR = "data-hf-autostamped";
|
|
export const CLIP_HIDDEN_ATTR = "data-hf-clip-hidden";
|
|
export const STUDIO_PREVIEW_FAR_SCENE_ATTR = "data-hf-far-scene";
|
|
export const STUDIO_PREVIEW_PARKED_SCENE_ATTR = "data-hf-parked-scene";
|
|
export const PREVIEW_ONLY_ATTRS = [
|
|
PREVIEW_VIDEO_FRAME_ATTR,
|
|
PREVIEW_VIDEO_SOURCE_ATTR,
|
|
PREVIEW_VIDEO_CANVAS_ATTR,
|
|
STUDIO_PREVIEW_UPCOMING_ATTR,
|
|
STUDIO_PREVIEW_FAR_SCENE_ATTR,
|
|
STUDIO_PREVIEW_PARKED_SCENE_ATTR,
|
|
STUDIO_PREVIEW_LAZY_ATTR,
|
|
PREVIEW_RASTER_ATTR,
|
|
RUNTIME_STAMPED_ATTR,
|
|
] as const;
|
|
|
|
export const isClipHiddenByRuntime = (el: HTMLElement) =>
|
|
el.hasAttribute(CLIP_HIDDEN_ATTR) && el.style.visibility === "hidden";
|
|
|
|
export function applyAuthoredTiming(
|
|
el: Element,
|
|
attrs: ReadonlyArray<readonly [name: string, value: string]>,
|
|
): void {
|
|
for (const [name, value] of attrs) el.setAttribute(name, value);
|
|
el.removeAttribute(RUNTIME_STAMPED_ATTR);
|
|
}
|
|
|
|
export function dropServiceWorkers(): Promise<void> {
|
|
const workers = typeof navigator === "undefined" ? undefined : navigator.serviceWorker;
|
|
if (!workers) return Promise.resolve();
|
|
return workers
|
|
.getRegistrations()
|
|
.then((all) => Promise.all(all.map((each) => each.unregister())))
|
|
.then(
|
|
() => undefined,
|
|
() => undefined,
|
|
);
|
|
}
|