Skip to content

Commit a01c97f

Browse files
committed
try to read style sheet content from JS API
1 parent 5066806 commit a01c97f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

packages/rrweb-snapshot/src/snapshot.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,14 @@ function onceIframeLoaded(
347347
iframeEl.addEventListener('load', listener);
348348
}
349349

350+
function stringifyStyleSheet(sheet: CSSStyleSheet): string {
351+
return sheet.cssRules
352+
? Array.from(sheet.cssRules)
353+
.map((rule) => rule.cssText || '')
354+
.join('')
355+
: '';
356+
}
357+
350358
function serializeNode(
351359
n: Node,
352360
options: {
@@ -538,6 +546,16 @@ function serializeNode(
538546
const isStyle = parentTagName === 'STYLE' ? true : undefined;
539547
const isScript = parentTagName === 'SCRIPT' ? true : undefined;
540548
if (isStyle && textContent) {
549+
try {
550+
// try to read style sheet
551+
if ((n.parentNode as HTMLStyleElement).sheet?.cssRules) {
552+
textContent = stringifyStyleSheet(
553+
(n.parentNode as HTMLStyleElement).sheet!,
554+
);
555+
}
556+
} catch {
557+
// ignore error
558+
}
541559
textContent = absoluteToStylesheet(textContent, getHref());
542560
}
543561
if (isScript) {

0 commit comments

Comments
 (0)