print-mini-html
Version:
2 lines (1 loc) • 1.67 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).printHTML=t()}(this,function(){"use strict";function y(e,t){for(var n=Array.prototype.slice.call(e),o=n.length,r=0;r<o;r++)t(n[r],r)}return function(e,t,n,o,r){o=o||0,e=function(e){"object"==typeof e&&(e=e.outerHTML);var t=(new DOMParser).parseFromString(e,"text/html");return y(t.images,function(e){"lazy"===e.loading&&(e.loading="eager")}),t.body.outerHTML}(e);var i,a="",d=0,u=!1;y(document.styleSheets,function(e){a+=e.ownerNode.outerHTML}),n&&(a+="<style>"+n+"</style>");var c=document.createElement("iframe");c.width=1,c.height=1,c.style.position="absolute",c.style.top="0",c.style.left="-999px",c.setAttribute("frameborder",0),document.body.appendChild(c);var f,s,l=c.contentWindow,m=l.document;function h(){--d<0&&!u&&(u=!0,i=setTimeout(p,o,o))}function p(e){!function(t,e,n){var o=t.contentWindow;if(o.matchMedia){var r=o.matchMedia("print");r.addEventListener?r.addEventListener("change",i):r.addListener(i)}else setTimeout(i,e+1);function i(e){e&&e.matches||(r&&(r.removeEventListener?r.removeEventListener("change",i):r.removeListener(i)),t.parentNode.removeChild(t),n())}}(c,e,function(){var t=function(){};return y(arguments,function(e){"function"==typeof e&&(t=e)}),t}(t,n,e,r)),l.print()}return m.write((f=a,s=e,"<html><head><title>"+(t||document.title)+"</title>"+f+"</head>"+s+"</html>")),y(m.images,function(e){++d,e.onload=h,e.onerror=h}),y(m.styleSheets,function(e){e.ownerNode instanceof HTMLLinkElement&&(++d,e.ownerNode.onload=h,e.ownerNode.onerror=h)}),h(),function(){clearTimeout(i),u=!0,p(0)}}});