UNPKG

html-screen-capture-js

Version:
3 lines (2 loc) 6.67 kB
var e,t;!function(e){e.OBJECT="object",e.STRING="string"}(e||(e={})),function(e){e.DEBUG="debug",e.INFO="info",e.WARN="warn",e.ERROR="error",e.FATAL="fatal",e.OFF="off"}(t||(t={}));let o=t.WARN;const n=(e,n)=>{const s=[t.DEBUG,t.INFO,t.WARN,t.ERROR,t.FATAL,t.OFF];s.indexOf(n)>=s.indexOf(o)&&console.log("|html-screen-capture-js|"+n+"| "+e)},s=()=>o===t.DEBUG,r=(e=t.WARN)=>{o=e},l=e=>{n(e,t.DEBUG)},a=e=>{n(e,t.INFO)},c=e=>{n(e,t.WARN)},i=e=>{n(e,t.ERROR)},d=e=>{let t="";const o="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";let n,s,r,l,a,c,i,d=0;for(e=(e=>{const t=e.replace(/\r\n/g,"\n");let o="";for(let e=0;e<t.length;e++){const n=t.charCodeAt(e);n<128?o+=String.fromCharCode(n):n>127&&n<2048?(o+=String.fromCharCode(n>>6|192),o+=String.fromCharCode(63&n|128)):(o+=String.fromCharCode(n>>12|224),o+=String.fromCharCode(n>>6&63|128),o+=String.fromCharCode(63&n|128))}return o})(e);d<e.length;)n=e.charCodeAt(d++),s=e.charCodeAt(d++),r=e.charCodeAt(d++),l=n>>2,a=(3&n)<<4|s>>4,c=(15&s)<<2|r>>6,i=63&r,isNaN(s)?c=i=64:isNaN(r)&&(i=64),t=t+o.charAt(l)+o.charAt(a)+o.charAt(c)+o.charAt(i);return t},u=e=>{const t=e.className,o="[object SVGAnimatedString]"===t.toString()?t.baseVal:t;return"string"==typeof o?o:""},g=(e,t,o)=>{(e=>{const t=u(e);return(t?t.split(" "):[]).reduce(((e,t)=>(t&&e.push(t),e)),[])})(o).length>0&&(o.setAttribute(e.options.attrKeyForSavingElementOrigClass,u(o)),o.removeAttribute("class")),o.getAttribute("style")&&(o.setAttribute(e.options.attrKeyForSavingElementOrigStyle,o.getAttribute("style")||""),o.removeAttribute("style"));let n=(()=>{let o=`${e.options.prefixForNewGeneratedClasses}0 `;const n=getComputedStyle(t);for(let t=0;t<n.length;t++){const s=n.item(t),r=n.getPropertyValue(s);if(r!==e.baseClass.get(s)){const t=s+":"+r;let n=e.classMap.get(t)||"";n||(e.classCount++,n=`${e.options.prefixForNewGeneratedClasses}${e.classCount}`,e.classMap.set(t,n)),o+=n+" "}}return o})();n+=(()=>{let o="";for(const n of["::before","::after"]){const s=getComputedStyle(t,n);if(!["none","normal"].includes(s.content)){e.pseudoClassCount++;const t=`${e.options.prefixForNewGeneratedPseudoClasses}${e.pseudoClassCount}`;o+=t+" ",e.pseudoStyles.push(`.${t}${n}{`);for(let t=0;t<s.length;t++){const o=s.item(t),n=s.getPropertyValue(o);e.pseudoStyles.push(`${o}:${n};`)}e.pseudoStyles.push("}")}}return o})(),o.setAttribute("class",n.trim())},p=(e,t)=>{let o="";try{e.canvas||(e.canvas=e.doc.createElement("canvas")),e.canvas.width=t instanceof HTMLImageElement?t.naturalWidth:t.offsetWidth,e.canvas.height=t instanceof HTMLImageElement?t.naturalHeight:t.offsetHeight;const n=e.canvas.getContext("2d");n&&n.drawImage(t,0,0),o=e.canvas.toDataURL(e.options.imageFormatForDataUrl,e.options.imageQualityForDataUrl)}catch(e){c(`getCanvasDataUrl() - ${e.message}`)}return o},m=(e,t)=>{if(e.ignoredElms.includes(t))return!0;const o=Object.entries(e.options.computedStyleKeyValuePairsOfIgnoredElements);if(e.isBody&&o.length>0){const e=getComputedStyle(t);for(const[t,n]of o)if(e.getPropertyValue(t)===n)return!0}return!1},f=(e,t,o,n)=>{if(e.isBody&&(t instanceof HTMLInputElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement?((e,t)=>{t.setAttribute("value",e.value);const o=e.getAttribute("type");if(e instanceof HTMLInputElement&&("checkbox"===o||"radio"===o))e.checked?t.setAttribute("checked","checked"):t.removeAttribute("checked");else if(e instanceof HTMLTextAreaElement&&e.value)t.innerText=e.value;else if(e instanceof HTMLSelectElement&&e.value&&e.children)for(let o=e.children.length-1;o>=0;o--)e.children[o].getAttribute("value")===e.value?t.children[o].setAttribute("selected",""):t.children[o].removeAttribute("selected")})(t,o):t instanceof HTMLImageElement?((e,t,o)=>{if(e.shouldHandleImgDataUrl){const n=p(e,t);n&&o.setAttribute("src",n)}})(e,t,o):t instanceof HTMLCanvasElement&&((e,t,o)=>{const n=p(e,t);n&&o.setAttribute("src",n),o.outerHTML=o.outerHTML.replace(/<canvas/g,"<img")})(e,t,o)),n&&(g(e,t,o),e.options.tagsOfSkippedElementsForChildTreeCssHandling.includes(t.tagName.toLowerCase())&&(n=!1)),t.children)for(let s=t.children.length-1;s>=0;s--)m(e,t.children[s])?o.removeChild(o.children[s]):f(e,t.children[s],o.children[s],n)},h=e=>{e.ignoredElms=(e=>{if(!e.options.cssSelectorsOfIgnoredElements||!e.options.cssSelectorsOfIgnoredElements.length)return[];const t=[];return e.options.cssSelectorsOfIgnoredElements.forEach((o=>{e.doc.documentElement.querySelectorAll(o).forEach((e=>{t.push(e)}))})),t})(e);const t=(()=>{const t=e.doc.documentElement.cloneNode(!1);return g(e,e.doc.documentElement,t),t})();return(t=>{const o=e.doc.head.cloneNode(!0);e.isBody=!1,f(e,e.doc.head,o,!1),t.appendChild(o)})(t),(t=>{(e=>{const t=e.doc.createElement("div");t.id="html-screen-capture-dummy-div",t.style.display="none",e.doc.body.appendChild(t);const o=getComputedStyle(t);for(let t=0;t<o.length;t++){const n=o.item(t),s=o.getPropertyValue(n);e.baseClass.set(n,s)}e.baseClass.set("display","block"),t.parentNode&&t.parentNode.removeChild(t)})(e);const o=e.doc.body.cloneNode(!0);e.isBody=!0,f(e,e.doc.body,o,!0),t.appendChild(o)})(t),(t=>{const o=e.doc.createElement("style");let n=`.${e.options.prefixForNewGeneratedClasses}0{`;e.baseClass.forEach(((e,t)=>{n+=`${t}:${e};`})),n+="}",e.classMap.forEach(((e,t)=>{n+=`.${e}{${t}}`})),n+=e.pseudoStyles.join(""),n+=e.options.rulesToAddToDocStyle.join(""),o.appendChild(e.doc.createTextNode(n)),t.children[0].appendChild(o)})(t),t},C=(t,o)=>{let n=null;if(o===e.OBJECT)n=t;else{const s=(t?t.outerHTML:"")||"";s&&(o===e.STRING?n=s:"uri"===o?n=((r=s)?encodeURI(r):"")||"":"base64"===o&&(n=d(s))),n=n||""}var r;return s()&&l(`output: ${n instanceof HTMLElement?n.outerHTML:n}`),n},y=(o,n,s)=>((o,n,s)=>{const l=(new Date).getTime();let c=null;const d={isBody:!1,baseClass:new Map,classMap:new Map,classCount:0,pseudoStyles:[],pseudoClassCount:0,shouldHandleImgDataUrl:!0,canvas:null,doc:n||document,ignoredElms:[],options:{rulesToAddToDocStyle:[],cssSelectorsOfIgnoredElements:["script","link","style"],computedStyleKeyValuePairsOfIgnoredElements:{display:"none"},tagsOfSkippedElementsForChildTreeCssHandling:["svg"],attrKeyForSavingElementOrigClass:"_class",attrKeyForSavingElementOrigStyle:"_style",prefixForNewGeneratedClasses:"c",prefixForNewGeneratedPseudoClasses:"p",imageFormatForDataUrl:"image/png",imageQualityForDataUrl:.92,logLevel:t.WARN,...s||{}}};try{r(d.options.logLevel);const t=h(d);c=C(t,o||e.OBJECT)}catch(e){i(`goCapture() - error - ${e.message}`)}finally{a(`goCapture() - end - ${(new Date).getTime()-l}ms`)}return c})(o,n,s);export{t as LogLevel,e as OutputType,y as capture}; //# sourceMappingURL=html-screen-capture.esm.js.map