Skip to content

Commit

Permalink
fix: check for stylesheets on current domain too (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Jan 20, 2025
1 parent 876e779 commit 5fc8029
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ function serializeTextNode(
};
}

function findStylesheet(doc: Document, href: string) {
return Array.from(doc.styleSheets).find((s) => s.href === href);
}

function serializeElementNode(
n: HTMLElement,
options: {
Expand Down Expand Up @@ -592,9 +596,14 @@ function serializeElementNode(
// remote css
if (tagName === 'link' && inlineStylesheet) {
//TODO: maybe replace this `.styleSheets` with original one
const stylesheet = Array.from(doc.styleSheets).find((s) => {
return s.href === (n as HTMLLinkElement).href;
});
const href = (n as HTMLLinkElement).href;
let stylesheet = findStylesheet(doc, href);
if (!stylesheet && href.includes('.css')) {
const rootDomain = window.location.origin;
const stylesheetPath = href.replace(window.location.href, '');
const potentialStylesheetHref = rootDomain + '/' + stylesheetPath;
stylesheet = findStylesheet(doc, potentialStylesheetHref);
}
let cssText: string | null = null;
if (stylesheet) {
cssText = stringifyStylesheet(stylesheet);
Expand Down

0 comments on commit 5fc8029

Please sign in to comment.