light-print
Version:
Lightweight HTML element printing for browsers.
7 lines (6 loc) • 4.93 kB
JavaScript
/*!
* light-print v2.5.1
* (c) 2020-2025 xunmi
* Released under the MIT License.
*/
var lightPrint=function(){"use strict";var t=function(t,e){return t.appendChild(e)};function e(t,e){return t.localName===e}var n=window.NodeFilter.SHOW_ELEMENT;function r(t,e){return window.document.createNodeIterator(t,n,null,!1)}function o(t,e,n,r){t.style.setProperty(e,String(n),r)}function i(t){var e;return function(t){return t instanceof Element}(t)?t:"string"==typeof t&&null!==(e=window.document.querySelector(t))&&void 0!==e?e:void 0}function u(t,e,n,r){var o=function(r){n(r),t.removeEventListener(e,o)};t.addEventListener(e,o,r)}function a(){return null!=Promise.withResolvers?Promise.withResolvers():{promise:new Promise(function(n,r){t=n,e=r}),resolve:t,reject:e};var t,e}function c(){}function l(t,e){return t.ownerDocument.defaultView.getComputedStyle(t,e)}var f=["img","audio","video","iframe","object","embed","image"];function d(t){return e(t,"object")?t.data:e(t,"iframe")||e(t,"embed")?t.src:e(t,"image")?t.href.baseVal:t.currentSrc||t.src}function m(t){var n=f.join(","),r=Array.from(t.querySelectorAll(n)).filter(function(t){return!!d(t)}).map(function(t){return t.setAttribute("loading","eager"),function(t){if(!e(t,"img")||!t.complete){var n=a(),r=n.promise,o=n.resolve,i=n.reject;return u(t,"load",function(){return o()}),u(t,"error",function(){return i(new Error("Failed to load resource (".concat(t.localName,": ").concat(d(t),").")))}),r}}(t)});return r.push(function(t){var e;return null===(e=t.fonts)||void 0===e?void 0:e.ready.then(c)}(t)),Promise.all(r).then(c)}var s=["::before","::after","::marker","::first-letter","::first-line","::placeholder","::file-selector-button","::details-content"],v=["block","inline-block","list-item","flow-root","table-caption","table-cell"];function p(t,e){for(var n="",r=0;r<e.length;r++){var o=e[r],i=e.getPropertyValue(o);i&&i!==t.getPropertyValue(o)&&(n+="".concat(o,":").concat(i,";"))}return n}function w(t,n,r){if("::placeholder"===r){if(!e(n,"input")&&!e(n,"textarea"))return}else if("::file-selector-button"===r){if(!e(n,"input")||"file"!==n.type)return}else if("::details-content"===r){if(!e(n,"details"))return}else if("::marker"===r){if("list-item"!==(o=l(n).display))return}else if("::first-letter"===r||"::first-line"===r){var o=l(n).display;if(v.indexOf(o)<0)return}var i=l(n,r);if("::before"===r||"::after"===r){var u=i.content;if(!u||"normal"===u||"none"===u)return}return p(l(t,r),i)}function y(t,e){var n,r=function(t,e){return p(l(t),l(e))}(t,e);if(r){var o=null!==(n=t.getAttribute("style"))&&void 0!==n?n:"";t.setAttribute("style","".concat(r).concat(o))}}function b(t,n,r){y(t,n),n instanceof SVGElement||function(t,e,n){for(var r="",o=0,i=s;o<i.length;o++){var u=i[o],a=w(t,e,u);if(a){var c=n.getSelector(t);r+="".concat(c).concat(u,"{").concat(a,"}")}}n.appendStyle(r)}(t,n,r),e(t,"canvas")&&function(t,e){t.getContext("2d").drawImage(e,0,0)}(t,n)}function g(){var e,n=1;var r={window:void 0,get document(){return this.window.document},appendStyle:function(t){t&&(null!=e||(e=r.document.createElement("style")),e.textContent+=t)},mountStyle:function(){e&&t(r.document.head,e)},getSelector:function(t){return'[data-print-id="'.concat(function(t){var e=t.getAttribute("data-print-id");return e||(e=(n++).toString(),t.setAttribute("data-print-id",e)),e}(t),'"]')}};return r}function h(t){var e=a(),n=e.promise,r=e.resolve;if(t.focus(),u(t,"afterprint",function(){return r()}),/msie|trident/i.test(window.navigator.userAgent))try{t.document.execCommand("print",!1)}catch(e){t.print()}else t.print();return n}return function(e,n){void 0===n&&(n={});var c=i(e);if(!c)return Promise.reject(new Error("Invalid HTML element."));var l=function(){var t=window.document.createElement("iframe");return t.srcdoc="<!DOCTYPE html>",o(t,"position","absolute","important"),o(t,"top","-9999px","important"),o(t,"visibility","hidden","important"),o(t,"transform","scale(0)","important"),t}(),f=g();return function(e,n){var r=a(),o=r.promise,i=r.resolve,c=r.reject;return u(e,"load",function(){return i()}),u(e,"error",function(){return c(new Error("Failed to mount document."))}),t(n,e),o}(l,window.document.body).then(function(){var e,i;f.window=l.contentWindow;var u=f.document;u.title=null!==(e=n.documentTitle)&&void 0!==e?e:window.document.title,o(u.documentElement,"zoom",null!==(i=n.zoom)&&void 0!==i?i:1),o(u.body,"margin",0),function(t){if(t.fonts)try{for(var e=window.document.fonts.values();;){var n=e.next().value;if(!n)break;t.fonts.add(n)}}catch(t){}}(u),function(e,n){var o=e.document;t(o.body,o.importNode(n,!0));var i=r(n),u=r(o.body);for(u.nextNode();;){var a=u.nextNode(),c=i.nextNode();if(!a||!c)break;b(a,c,e)}}(f,c),f.appendStyle(n.mediaPrintStyle),f.mountStyle()}).then(function(){return m(f.document)}).then(function(){return h(f.window)}).finally(function(){return null===(e=(t=l).parentNode)||void 0===e?void 0:e.removeChild(t);var t,e})}}();