From a01c97f70da6583400d2759f26a518037d4c2adf Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Tue, 16 Nov 2021 02:26:49 +0800 Subject: [PATCH] try to read style sheet content from JS API --- packages/rrweb-snapshot/src/snapshot.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/rrweb-snapshot/src/snapshot.ts b/packages/rrweb-snapshot/src/snapshot.ts index ca548332..142f2b08 100644 --- a/packages/rrweb-snapshot/src/snapshot.ts +++ b/packages/rrweb-snapshot/src/snapshot.ts @@ -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) {