Skip to content
This repository has been archived by the owner on Mar 6, 2019. It is now read-only.

Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules #31

Open
alexlenail opened this issue Apr 13, 2018 · 2 comments

Comments

@alexlenail
Copy link

I'm having an issue with SVG crowbar starting in Chrome 64/65, for which I get the above error message in the console. For some reason, this is only broken for some svgs.

The line breaking is the second one here:

https://github.com/NYTimes/svg-crowbar/blob/f41f811dba84c6e522c5c213361e6fe65a0b8313/svg-crowbar.js#L223-L224

This might lead towards a fix

@robsdedude
Copy link

See also
Modernizr/Modernizr#2296 and odoo/odoo#22517

@zzolo
Copy link

zzolo commented Sep 13, 2018

Changing that to ss.hasOwnProperty('cssRules') fixed the issue for me.

Here's an easy fixed bookmarklet to add manually:

javascript:javascript: !function(){var t='<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';window.URL=window.URL||window.webkitURL;var e,n,o=document.body,l={xmlns:"http://www.w3.org/2000/xmlns/",xlink:"http://www.w3.org/1999/xlink",svg:"http://www.w3.org/2000/svg"};function s(t){var e="untitled";t.id?e=t.id:t.class?e=t.class:window.document.title&&(e=window.document.title.replace(/[^a-z0-9]/gi,"-").toLowerCase());var n=window.URL.createObjectURL(new Blob(t.source,{type:"text/xml"})),l=document.createElement("a");o.appendChild(l),l.setAttribute("class","svg-crowbar"),l.setAttribute("download",e+".svg"),l.setAttribute("href",n),l.style.display="none",l.click(),setTimeout(function(){window.URL.revokeObjectURL(n)},10)}e=[window.document],n=[],iframes=document.querySelectorAll("iframe"),objects=document.querySelectorAll("object"),[].forEach.call(iframes,function(t){try{t.contentDocument&&e.push(t.contentDocument)}catch(t){console.log(t)}}),[].forEach.call(objects,function(t){try{t.contentDocument&&e.push(t.contentDocument)}catch(t){console.log(t)}}),e.forEach(function(e){for(var o,s,r,i=function(t){var e="",n=t.styleSheets;if(n)for(var o=0;o<n.length;o++)l(n[o]);function l(t){if(t.hasOwnProperty("cssRules"))for(var n=0;n<t.cssRules.length;n++){var o=t.cssRules[n];3===o.type?l(o.styleSheet):o.selectorText&&-1===o.selectorText.indexOf(">")&&(e+="\n"+o.cssText)}}return e}(e),c=(o=i,s=[],r=e.querySelectorAll("svg"),o=void 0===o?"":o,[].forEach.call(r,function(e){e.setAttribute("version","1.1");var n=document.createElement("defs");e.insertBefore(n,e.firstChild);var r=document.createElement("style");n.appendChild(r),r.setAttribute("type","text/css"),e.removeAttribute("xmlns"),e.removeAttribute("xlink"),e.hasAttributeNS(l.xmlns,"xmlns")||e.setAttributeNS(l.xmlns,"xmlns",l.svg),e.hasAttributeNS(l.xmlns,"xmlns:xlink")||e.setAttributeNS(l.xmlns,"xmlns:xlink",l.xlink);var i=(new XMLSerializer).serializeToString(e).replace("</style>","<![CDATA["+o+"]]></style>"),c=e.getBoundingClientRect();s.push({top:c.top,left:c.left,width:c.width,height:c.height,class:e.getAttribute("class"),id:e.getAttribute("id"),childElementCount:e.childElementCount,source:[t+i]})}),s),a=0;a<c.length;a++)n.push(c[a])}),n.length>1?function(t){var e;e=document.querySelectorAll(".svg-crowbar"),[].forEach.call(e,function(t){t.parentNode.removeChild(t)}),t.forEach(function(e){t.forEach(function(t){e!==t&&Math.abs(e.top-t.top)<38&&Math.abs(e.left-t.left)<38&&(t.top+=38,t.left+=38)})});var n=document.createElement("div");o.appendChild(n),n.setAttribute("class","svg-crowbar"),n.style["z-index"]=1e7,n.style.position="absolute",n.style.top=0,n.style.left=0;var l=document.createElement("div");o.appendChild(l),l.setAttribute("class","svg-crowbar"),l.style.background="rgba(255, 255, 255, 0.7)",l.style.position="fixed",l.style.left=0,l.style.top=0,l.style.width="100%",l.style.height="100%",t.forEach(function(t,e){var o=document.createElement("div");n.appendChild(o),o.setAttribute("class","svg-crowbar"),o.style.position="absolute",o.style.top=t.top+document.body.scrollTop+"px",o.style.left=document.body.scrollLeft+t.left+"px",o.style.padding="4px",o.style["border-radius"]="3px",o.style.color="white",o.style["text-align"]="center",o.style["font-family"]="'Helvetica Neue'",o.style.background="rgba(0, 0, 0, 0.8)",o.style["box-shadow"]="0px 4px 18px rgba(0, 0, 0, 0.4)",o.style.cursor="move",o.textContent="SVG #"+e+": "+(t.id?"#"+t.id:"")+(t.class?"."+t.class:"");var l=document.createElement("button");o.appendChild(l),l.setAttribute("data-source-id",e),l.style.width="150px",l.style["font-size"]="12px",l.style["line-height"]="1.4em",l.style.margin="5px 0 0 0",l.textContent="Download",l.onclick=function(e){s(t)}})}(n):n.length>0?s(n[0]):alert("The Crowbar couldn’t find any SVG nodes.")}();

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants