forked from rpdasilva/print-html-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-html-element.min.js
1 lines (1 loc) · 1.78 KB
/
print-html-element.min.js
1
var PrintElement=function(){function a(a,b){var d=a.outerHTML;c(d,b)}function b(a,b){c(a,b)}function c(a,b){b=b||{},b={printMode:b.printMode||"",pageTitle:b.pageTitle||"",templateString:b.templateString||""};var c,e,g,h,i=f(a,b);"popup"==b.printMode.toLowerCase()?(c=window.open("about:blank","printElementWindow"),g=c.document):(h="printElement_"+Math.round(99999*Math.random()).toString(),e=document.createElement("iframe"),e.setAttribute("id",h),e.setAttribute("src","about:blank"),e.setAttribute("frameBorder","0"),e.setAttribute("scrolling","no"),e.setAttribute("style","position:fixed;bottom:100%;right:100%;"),document.body.appendChild(e),g=e.contentWindow||e.contentDocument,g.document&&(g=g.document),e=document.frames?document.frames[h]:document.getElementById(h),c=e.contentWindow||e),focus(),g.open(),g.write(i),g.close(),d(c,e)}function d(a,b){a&&a.printPage?(a.printPage(),b&&document.body.removeChild(b)):setTimeout(function(){d(a,b)},50)}function e(){var a=window.location.port?":"+window.location.port:"";return window.location.protocol+"//"+window.location.hostname+a+window.location.pathname}function f(a,b){var c,d=b.templateString,f=new RegExp(/{{\s*printBody\s*}}/gi),g=[];return d&&f.test(d)&&(a=d.replace(f,a)),g.push("<html><head><title>"+(b.pageTitle||"")+"</title>"),c=Array.prototype.slice.call(document.getElementsByTagName("link")),c.forEach(function(a){g.push('<link rel="stylesheet" href="'+a.href+'">')}),g.push('<base href="'+e()+'" />'),g.push('</head><body class="pe-body">'),g.push(a),g.push('<script type="text/javascript">function printPage(){focus();print();'+("popup"==b.printMode.toLowerCase()?"close();":"")+"}</script>"),g.push("</body></html>"),g.join("")}return{printElement:a,printHtml:b}};"object"==typeof module&&module.exports===exports&&(module.exports=PrintElement());