try to read style sheet content from JS API

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent 49ebbc577e
commit ba60763030

View File

@@ -347,6 +347,14 @@ function onceIframeLoaded(
iframeEl.addEventListener('load', listener);
}
function stringifyStyleSheet(sheet: CSSStyleSheet): string {
return sheet.cssRules
? Array.from(sheet.cssRules)
.map((rule) => rule.cssText || '')
.join('')
: '';
}
function serializeNode(
n: Node,
options: {
@@ -538,6 +546,16 @@ function serializeNode(
const isStyle = parentTagName === 'STYLE' ? true : undefined;
const isScript = parentTagName === 'SCRIPT' ? true : undefined;
if (isStyle && textContent) {
try {
// try to read style sheet
if ((n.parentNode as HTMLStyleElement).sheet?.cssRules) {
textContent = stringifyStyleSheet(
(n.parentNode as HTMLStyleElement).sheet!,
);
}
} catch {
// ignore error
}
textContent = absoluteToStylesheet(textContent, getHref());
}
if (isScript) {