-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcritical-css-widget.min.js
15 lines (15 loc) · 7.2 KB
/
critical-css-widget.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(function(k){function x(){function a(c){return c.reduce(function(c,e){c.push(b(e));e=e.children;var m=[],h;for(h=0;h<e.length;h++)m[h]=e[h];return c=c.concat(a(m))},[])}function b(a){var c=document.styleSheets,e=[];a.matches=a.matches||a.webkitMatchesSelector||a.mozMatchesSelector||a.msMatchesSelector||a.oMatchesSelector;for(var g in c){try{var h=c[g].rules||c[g].cssRules}catch(d){continue}for(var n in h){var f=h[n].selectorText?h[n].selectorText.split(" ").map(function(a){return a?a.split(/(:|::)/)[0]:
!1}).join(" "):!1;try{a.matches(f)&&e.push(h[n])}catch(d){}}}return e}var g=[];g=function(a){function c(a,e){return e.filter(function(e){return e.b===a})}var e=[];a.forEach(function(a){a.forEach(function(a){var h=a.selectorText;var f=c(h,e);h=0<f.length?f[0]:{b:h,c:{}};for(f=0;f<a.style.length;f++){var d=a.style[f];h.c[d]=a.style.getPropertyValue(d)}0===c(h.b,e).length&&e.push(h)})});return e}(a([document.body]));return g.reduce(function(a,g){a+=g.b+" {\n";a+=Object.keys(g.c).reduce(function(a,c){a.push(" "+
c+": "+g.c[c]+";");return a},[]).join("\n");return a+"\n}\n"},"")}function y(a,b,g){function c(a){var h=a.parentStyleSheet.href;h||(k++,h="inline");!1===!!e[h]&&(e[h]={media:a.parentStyleSheet.media,a:{}});!1===!!e[h].a[a.selectorText]&&(e[h].a[a.selectorText]={});for(var f=a.style.cssText.split(/;(?![A-Za-z0-9])/),d=0;d<f.length;d++)if(!1!==!!f[d]){var c=f[d].split(": ");c[0]=c[0].trim();c[1]=c[1].trim();e[h].a[a.selectorText][c[0]]=c[1]}}var m=g||{},e={},k=0;this.g=function(){var a="",c=console&&
console.groupCollapsed,f=[];console.clear&&console.clear();c&&(console.log("%cSimple Critical CSS Extraction","font-size:24px;font-weight:bold"),console.log("For professional Critical CSS generators, see https://github.com/addyosmani/critical-path-css-tools"));for(var d in e){if(c){var g="inline"===d?"Inline":"File: "+d;console.groupCollapsed(g);g=""}var b=a.split(/\r\n|\r|\n/).length;f.push([d,b]);a+="/**\n * @file "+d;if(e[d].media&&(1<e[d].media.length||"all"!==e[d].media[0])){b=[];for(var m=0;m<
e[d].media.length;m++)e[d].media[m]&&b.push(e[d].media[m]);0<b.length&&(b=b.join(" "),a+="\n * @media "+b)}a+="\n */\n";for(var l in e[d].a){b=l+" { ";for(var k in e[d].a[l])b+=k+": "+e[d].a[l][k]+"; ";b+="}\n";a+=b;c&&(g+=b)}a+="\n";c&&(console.log(g),console.groupEnd())}c?console.groupCollapsed("All Extracted Critical CSS ("+t(a.length)+")"):console.log("%cAll:","font-weight:bold");console.log(a);c&&console.groupEnd();return[a,f]};(function(){for(var e=a.innerHeight,g=b.createTreeWalker(b,NodeFilter.SHOW_ELEMENT,
function(){return NodeFilter.FILTER_ACCEPT},!0);g.nextNode();){var f=g.currentNode;if(f.getBoundingClientRect().top<e||m.o)if(f=a.getMatchedCSSRules(f))for(var d=0;d<f.length;d++)c(f[d])}})()}function t(a){var b=Math.floor(Math.log(a)/Math.log(1024));return 1*(a/Math.pow(1024,b)).toFixed(2)+" "+["B","kB","MB"][b]}function v(){}(function(a,b){b.getMatchedCSSRules=a()})(function(){function a(a,c){var b=a.matchesSelector||a.webkitMatchesSelector||a.mozMatchesSelector||a.msMatchesSelector;if(b)try{return b.call(a,
c)}catch(e){}else for(c=a.ownerDocument.querySelectorAll(c),b=c.length;b&&b--;)if(c[b]===a)return!0;return!1}function b(b){var c=[],g=b.ownerDocument.styleSheets,e=g.length,q,h;if(1===b.nodeType)for(;e&&e--;){try{var n=g[e].cssRules||g[e].rules}catch(r){continue}for(q=n.length;q&&q--;){var f=n[q];if(f instanceof CSSStyleRule&&a(b,f.selectorText))c.push(f);else if(f instanceof CSSMediaRule&&k.matchMedia(f.media.mediaText).matches){try{var d=f.cssRules||f.rules}catch(r){continue}for(h=d.length;h&&h--;)f=
d[h],f instanceof CSSStyleRule&&a(b,f.selectorText)&&c.push(f)}}}return c}return function(){return k.getMatchedCSSRules?k.getMatchedCSSRules:b}}(k),this);var u=u||function(a){if(!("undefined"===typeof a||"undefined"!==typeof navigator&&/MSIE [1-9]\./.test(navigator.userAgent))){var b=a.document.createElementNS("http://www.w3.org/1999/xhtml","a"),g="download"in b,c=/constructor/i.test(a.HTMLElement)||a.f,k=/CriOS\/[\d]+/.test(navigator.userAgent),e=function(d){(a.setImmediate||a.setTimeout)(function(){throw d;
},0)},q=function(d){setTimeout(function(){"string"===typeof d?(a.URL||a.webkitURL||a).revokeObjectURL(d):d.remove()},4E4)},h=function(a){return/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(a.type)?new Blob([String.fromCharCode(65279),a],{type:a.type}):a},n=function(d,f,m){function n(){var a=["writestart","progress","write","writeend"];a=[].concat(a);for(var b=a.length;b--;){var d=l["on"+a[b]];if("function"===typeof d)try{d.call(l,l)}catch(z){e(z)}}}m||(d=h(d));
var l=this,r="application/octet-stream"===d.type;l.readyState=l.INIT;if(g){var p=(a.URL||a.webkitURL||a).createObjectURL(d);setTimeout(function(){b.href=p;b.download=f;var a=new MouseEvent("click");b.dispatchEvent(a);n();q(p);l.readyState=l.DONE})}else(function(){if((k||r&&c)&&a.FileReader){var b=new FileReader;b.onloadend=function(){var d=k?b.result:b.result.replace(/^data:[^;]*;/,"data:attachment/file;");a.open(d,"_blank")||(a.location.href=d);l.readyState=l.DONE;n()};b.readAsDataURL(d);l.readyState=
l.INIT}else p||(p=(a.URL||a.webkitURL||a).createObjectURL(d)),r?a.location.href=p:a.open(p,"_blank")||(a.location.href=p),l.readyState=l.DONE,n(),q(p)})()},f=n.prototype;if("undefined"!==typeof navigator&&navigator.msSaveOrOpenBlob)return function(a,b,c){b=b||a.name||"download";c||(a=h(a));return navigator.msSaveOrOpenBlob(a,b)};f.abort=function(){};f.readyState=f.INIT=0;f.f=1;f.DONE=2;f.error=f.m=f.i=f.j=f.onabort=f.h=f.l=null;return function(a,b,c){return new n(a,b||a.name||"download",c)}}}(k);
if(k.critical)var w=k.critical.constructor.prototype;else k.critical=new v,w=v.prototype;w.extract=function(a,b){if("full"===a){a=x();var g=!0;console.clear&&console.clear();console.log("%cFull CSS Extraction","font-size:24px;font-weight:bold");console.groupCollapsed&&console.groupCollapsed("Extracted Full CSS ("+t(a.length)+")");console.log(a);console.groupCollapsed&&console.groupEnd();g?(a="/**\n * Full CSS\n *\n * @url "+document.location.href+"\n * @title "+document.title+"\n * @size "+t(a.length)+
"\n *\n * Extracted using Critical CSS Widget.\n * @link https://github.com/critical-x/critical-css-widget\n */\n\n"+a,b?b(a):(b=new Blob([a],{type:"text/css;charset=utf-8"}),a=(a=k.location.pathname)&&"/"!==a&&-1!==a.indexOf("/")?"-"+a.replace(/\/$/,"").split("/").pop():"-front-page",u(b,"full-css"+a+".css"))):alert("Your browser does not support javascript based file download. The full CSS is printed in the console.")}else{g=(new y(k,document)).g();a=g[0];g=g[1];var c="/**\n * Simple Critical CSS\n *\n * @url "+
document.location.href+"\n * @title "+document.title+"\n * @viewport "+k.innerWidth+"x"+k.innerHeight+"\n * @size "+t(a.length)+"\n *\n * Extracted using Critical CSS Widget.\n * @link https://github.com/critical-x/critical-css-widget\n *\n * For professional Critical CSS generators see https://github.com/addyosmani/critical-path-css-tools\n *\n * @sources",m=c.split(/\r\n|\r|\n/).length;m+=g.length+3;for(var e=0;e<g.length;e++)c+="\n * @line "+(g[e][1]+m)+"\t @file "+g[e][0];c=c+"\n */\n\n"+a;b?
b(c):(b=new Blob([c],{type:"text/css;charset=utf-8"}),a=(a=k.location.pathname)&&"/"!==a&&-1!==a.indexOf("/")?"-"+a.replace(/\/$/,"").split("/").pop():"-front-page",u(b,"critical-css"+a+".css"))}return"Starting extract..."}})(window);