dom-to-image icon indicating copy to clipboard operation
dom-to-image copied to clipboard

SecurityError: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules

Open ris0 opened this issue 6 years ago • 13 comments

Use case: description, code

Hello everyone,

I have been experimenting with this library over the past few weeks. It was working perfectly fine until this week. I'm able to download the image but the CSS rules do not get applied.

domtoimage.toPng(parentElement).then(function (dataUrl) { console.log("downloading image"); let imgElement = document.getElementById("downloadtest"); imgElement.setAttribute("href", dataUrl); imgElement.setAttribute("download", "Test file.png"); imgElement.click(); }).catch((error) => { console.error(error); }); I get the following error message in my console: Error while reading CSS rules from chrome-extension://liecbddmkiiihnedobmlmillhodjkdmb/css/content.css SecurityError: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules dom-to-image.js?d424:684

jsfiddle

Expected behavior

Download image with CSS rules applied

Actual behavior (stack traces, console logs etc)

Image downloaded without CSS rules applied

Library version

2.6.0

Browsers

Chrome Version 66.0.3359.117 (Official Build) (64-bit)

ris0 avatar Apr 25 '18 19:04 ris0

I was able to find a solution. If you modify the getCssRules method in the dom-to-image so that it looks like this:

            function getCssRules(styleSheets) {
                var cssRules = [];
                styleSheets.forEach(function (sheet) {
                    if (sheet.hasOwnProperty("cssRules")) {
                        try {
                            util.asArray(sheet.cssRules || []).forEach(cssRules.push.bind(cssRules));
                        } catch (e) {
                            console.log('Error while reading CSS rules from ' + sheet.href, e.toString());
                        }
                    }
                });
                return cssRules;
            }

Notice the usage of if (sheet.hasOwnProperty("cssRules")) Please take a look at these posts for reference: https://github.com/HdrHistogram/HdrHistogram/pull/135/commits/2831ebdcf1c7507e8941e29cd3b5d8c35c2cfd9c https://github.com/odoo/odoo/issues/22517

ris0 avatar Apr 25 '18 21:04 ris0

Anyway to give it access to the material style sheet? Mine won't render material icons

Warriormuffin avatar May 21 '18 21:05 Warriormuffin

I'll pull request that solution @ris0. Hope ok.

andytwoods avatar Jul 31 '18 08:07 andytwoods

Looks like the fix did not pass the tests...

carttrac avatar Aug 01 '18 03:08 carttrac

the if statement solution fixes the error from appearing in console but with it my web fonts don't render.

impactcolor avatar Oct 05 '18 22:10 impactcolor

Just add crossorigin="anonymous" to all your link files to fix this. Had the same error, fixed!

IAmRC1 avatar May 14 '20 17:05 IAmRC1

In firefox, I am able to capture the screen with the applied css.

However, i am not able to capture the screen with the applied CSS on Chrome v84.0.4147.

How to fix this ?

mitesh-agarwal-bh avatar Sep 02 '20 14:09 mitesh-agarwal-bh

Thank you. It works for me!!

zvvysotskaya avatar Jan 10 '21 02:01 zvvysotskaya

@IAmRC1 Thank you. It works for me too

BespalkoSergey avatar Jan 28 '22 18:01 BespalkoSergey

I started having this problem today. I added crossorigin="anonymous" in the link tags but that did not fix the problem. I traced it to the presence of some SVG icons in the source html. They are stored locally (i.e. same domain) and were being loaded as src="..." . I replaced these img tags with the straight code and the problem was fixed.

DavidThomas48 avatar Jul 26 '22 06:07 DavidThomas48

Just add crossorigin="anonymous" to all your link files to fix this. Had the same error, fixed!

Thanks man, ily 💖, struggling for this for hours

kelvinkn17 avatar Aug 27 '22 09:08 kelvinkn17

same issue... please assist :(

born2net avatar Jan 07 '23 06:01 born2net

same issue... please assist :(

@born2net more details please

DavidThomas48 avatar Jan 07 '23 13:01 DavidThomas48