I am getting DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules . Any suggestion how can I resolve the same.
Sample-Code
function copyStyles(source, target) {
Array.from(source.styleSheets).forEach(styleSheet => {
let rules
try {
rules = styleSheet.cssRules
} catch (err) {
console.error(err)
}
if (rules) {
const newStyleEl = source.createElement('style')
Array.from(styleSheet.cssRules).forEach(cssRule => {
.......
})
target.head.appendChild(newStyleEl)
} else if (styleSheet.href) {
..........
}
})
}
I suggest you avoid calling cssRules that may be protected via CSP, and instead just add a new stylesheet with whatever rules you want that override the existing ones.
Like this...