fix: "Uncaught TypeError: Cannot read property 'insertRule' of null" caused by CSP style-src (#79)

This commit is contained in:
Lucky Feng
2021-06-09 18:55:03 +08:00
committed by GitHub
parent 6710e2c0e3
commit 079ecd4972

View File

@@ -169,6 +169,15 @@ function buildNode(
// as setting them triggers a console.error (which shows up despite the try/catch) // as setting them triggers a console.error (which shows up despite the try/catch)
// Assumption: these attributes are not used to css // Assumption: these attributes are not used to css
node.setAttribute('_' + name, value); node.setAttribute('_' + name, value);
} else if (
tagName === 'meta' &&
n.attributes['http-equiv'] === 'Content-Security-Policy' &&
name == 'content'
) {
// If CSP contains style-src and inline-style is disabled, there will be an error "Refused to apply inline style because it violates the following Content Security Policy directive: style-src '*'".
// And the function insertStyleRules in rrweb replayer will throw an error "Uncaught TypeError: Cannot read property 'insertRule' of null".
node.setAttribute('csp-content', value);
continue;
} else { } else {
node.setAttribute(name, value); node.setAttribute(name, value);
} }
@@ -255,7 +264,7 @@ export function buildNodeWithSN(
} }
if (n.rootId) { if (n.rootId) {
console.assert( console.assert(
((map[n.rootId] as unknown) as Document) === doc, (map[n.rootId] as unknown as Document) === doc,
'Target document should has the same root id.', 'Target document should has the same root id.',
); );
} }
@@ -318,7 +327,7 @@ function handleScroll(node: INode) {
if (n.type !== NodeType.Element) { if (n.type !== NodeType.Element) {
return; return;
} }
const el = (node as Node) as HTMLElement; const el = node as Node as HTMLElement;
for (const name in n.attributes) { for (const name in n.attributes) {
if (!(n.attributes.hasOwnProperty(name) && name.startsWith('rr_'))) { if (!(n.attributes.hasOwnProperty(name) && name.startsWith('rr_'))) {
continue; continue;