2 lines
9.0 KiB
JavaScript
2 lines
9.0 KiB
JavaScript
/* 2020-9-22 15:23:28 | 版权所有 军懋国兴 */
|
||
!function(e){"use strict";var g={escape:function(e){return e.replace(/([.*+?^${}()|\[\]\/\\])/g,"\\$1")},parseExtension:n,mimeType:function(e){var t=n(e).toLowerCase();return function(){var e="application/font-woff",t="image/jpeg";return{woff:e,woff2:e,ttf:"application/font-truetype",eot:"application/vnd.ms-fontobject",png:"image/png",jpg:t,jpeg:t,gif:"image/gif",tiff:"image/tiff",svg:"image/svg+xml"}}()[t]||""},dataAsUrl:function(e,t){return"data:"+t+";base64,"+e},isDataUrl:function(e){return-1!==e.search(/^(data:)/)},canvasToBlob:function(t){return t.toBlob?new Promise(function(e){t.toBlob(e)}):function(i){return new Promise(function(e){for(var t=window.atob(i.toDataURL().split(",")[1]),n=t.length,r=new Uint8Array(n),o=0;o<n;o++)r[o]=t.charCodeAt(o);e(new Blob([r],{type:"image/png"}))})}(t)},resolveUrl:function(e,t){var n=document.implementation.createHTMLDocument(),r=n.createElement("base");n.head.appendChild(r);var o=n.createElement("a");return n.body.appendChild(o),r.href=t,o.href=e,o.href},getAndEncode:function(i){m.impl.options.cacheBust&&(i+=(/\?/.test(i)?"&":"?")+(new Date).getTime());i.startsWith("http")&&-1==i.indexOf(location.hostname)&&(m.proxy?i=m.proxy(i):m.preUrl&&(i=m.preUrl+i));return new Promise(function(n){var e,t,r=new XMLHttpRequest;function o(e){console.log(e),n("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAABlBMVEXMzMwAov9iAAKCAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAACklEQVQImWNgAAAAAgAB9HFkpgAAAABJRU5ErkJggg==")}r.onreadystatechange=function(){if(4!==r.readyState)return;{var t;200!==r.status?e?n(e):o("无法获取资源: "+i+", 状态: "+r.status):((t=new FileReader).onloadend=function(){var e=t.result.split(/,/)[1];n(e)},t.readAsDataURL(r.response))}},r.ontimeout=function(){e?n(e):o("抓取资源时发生超时(100000ms): "+i)},r.responseType="blob",r.timeout=1e5,r.open("GET",i,!0),r.send(),!m.impl.options.imagePlaceholder||(t=m.impl.options.imagePlaceholder.split(/,/))&&t[1]&&(e=t[1])})},uid:function(){var e=0;return function(){return"u"+("0000"+(Math.random()*Math.pow(36,4)<<0).toString(36)).slice(-4)+e++}}(),delay:function(n){return function(t){return new Promise(function(e){setTimeout(function(){e(t)},n)})}},asArray:function(e){for(var t=[],n=e.length,r=0;r<n;r++)t.push(e[r]);return t},escapeXhtml:function(e){return e.replace(/#/g,"%23").replace(/\n/g,"%0A")},makeImage:function(r){return new Promise(function(e,t){var n=new Image;n.crossOrigin="*",n.onload=function(){e(n)},n.onerror=t,n.src=r})},width:function(e){var t=r(e,"border-left-width"),n=r(e,"border-right-width");return e.scrollWidth+t+n},height:function(e){var t=r(e,"border-top-width"),n=r(e,"border-bottom-width");return e.scrollHeight+t+n}};function n(e){var t=/\.([^\.\/]*?)$/g.exec(e);return t?t[1]:""}function r(e,t){var n=window.getComputedStyle(e).getPropertyValue(t);return parseFloat(n.replace("px",""))}var o,i={inlineAll:function(t,r,o){return u(t)?Promise.resolve(t).then(a).then(function(e){var n=Promise.resolve(t);return e.forEach(function(t){n=n.then(function(e){return c(e,t,r,o)})}),n}):Promise.resolve(t)},shouldProcess:u,impl:{readUrls:a,inline:c}};function u(e){return-1!==e.search(o)}function a(e){for(var t,n=[];null!==(t=o.exec(e));)n.push(t[1]);return n.filter(function(e){return!g.isDataUrl(e)})}function c(n,r,t,e){return Promise.resolve(r).then(function(e){return t?g.resolveUrl(e,t):e}).then(e||g.getAndEncode).then(function(e){return g.dataAsUrl(e,g.mimeType(r))}).then(function(e){return n.replace((t=r,new RegExp("(url\\(['\"]?)("+g.escape(t)+")(['\"]?\\))","g")),"$1"+e+"$3");var t})}var t={resolveAll:function(){return l(document).then(function(e){return Promise.all(e.map(function(e){return e.resolve()}))}).then(function(e){return e.join("\n")})},impl:{readAll:l}};function l(){return Promise.resolve(g.asArray(document.styleSheets)).then(function(e){var n=[];return e.forEach(function(t){try{g.asArray(t.cssRules||[]).forEach(n.push.bind(n))}catch(e){console.log("Error while reading CSS rules from "+t.href,e.toString())}}),n}).then(function(e){return e.filter(function(e){return e.type===CSSRule.FONT_FACE_RULE}).filter(function(e){return i.shouldProcess(e.style.getPropertyValue("src"))})}).then(function(e){return e.map(t)});function t(t){return{resolve:function(){var e=(t.parentStyleSheet||{}).href;return i.inlineAll(t.cssText,e)},src:function(){return t.style.getPropertyValue("src")}}}}var s={inlineAll:function t(e){if(!(e instanceof Element))return Promise.resolve(e);return n(e).then(function(){return e instanceof HTMLImageElement?f(e).inline():Promise.all(g.asArray(e.childNodes).map(function(e){return t(e)}))});function n(t){var e=t.style.getPropertyValue("background");return e?i.inlineAll(e).then(function(e){t.style.setProperty("background",e,t.style.getPropertyPriority("background"))}).then(function(){return t}):Promise.resolve(t)}},impl:{newImage:f}};function f(r){return{inline:function(e){return g.isDataUrl(r.src)?Promise.resolve():Promise.resolve(r.src).then(e||g.getAndEncode).then(function(e){return g.dataAsUrl(e,g.mimeType(r.src))}).then(function(n){return new Promise(function(e,t){r.onload=e,r.onerror=t,r.src=n})})}}}var h={imagePlaceholder:void 0,cacheBust:!(o=/url\(['"]?([^'"]+?)['"]?\)/g)},m={toSvg:d,toPng:function(e,t){return p(e,t||{}).then(function(e){return e.toDataURL()})},toJpeg:function(e,t){return p(e,t=t||{}).then(function(e){return e.toDataURL("image/jpeg",t.quality||1)})},toBlob:function(e,t){return p(e,t||{}).then(g.canvasToBlob)},toPixelData:function(t,e){return p(t,e||{}).then(function(e){return e.getContext("2d").getImageData(0,0,g.width(t),g.height(t)).data})},impl:{fontFaces:t,images:s,util:g,inliner:i,options:{}}};function d(o,i){return function(e){void 0===e.imagePlaceholder?m.impl.options.imagePlaceholder=h.imagePlaceholder:m.impl.options.imagePlaceholder=e.imagePlaceholder;void 0===e.cacheBust?m.impl.options.cacheBust=h.cacheBust:m.impl.options.cacheBust=e.cacheBust}(i=i||{}),Promise.resolve(o).then(function(e){return function i(t,n,e){if(!e&&n&&!n(t))return Promise.resolve();return Promise.resolve(t).then(r).then(function(e){return o(t,e,n)}).then(function(e){return u(t,e)});function r(e){return e instanceof HTMLCanvasElement?g.makeImage(e.toDataURL()):e.cloneNode(!1)}function o(e,t,n){var r=e.childNodes;return 0===r.length?Promise.resolve(t):o(t,g.asArray(r),n).then(function(){return t});function o(t,e,n){var r=Promise.resolve();return e.forEach(function(e){r=r.then(function(){return i(e,n)}).then(function(e){e&&t.appendChild(e)})}),r}}function u(h,m){return m instanceof Element?Promise.resolve().then(e).then(t).then(n).then(r).then(function(){return m}):m;function e(){function e(e,t){var n,r;e.cssText?t.cssText=e.cssText:(n=e,r=t,g.asArray(n).forEach(function(e){r.setProperty(e,n.getPropertyValue(e),n.getPropertyPriority(e))}))}e(window.getComputedStyle(h),m.style)}function t(){function t(e){var t,n,r,o,i,u,a,c,l=window.getComputedStyle(h,e),s=l.getPropertyValue("content");function f(e){return e+": "+r.getPropertyValue(e)+(r.getPropertyPriority(e)?" !important":"")}""!==s&&"none"!==s&&(t=g.uid(),m.className=m.className+" "+t,(n=document.createElement("style")).appendChild((a="."+t+":"+e,c=(u=l).cssText?(i=(o=o=u).getPropertyValue("content"),o.cssText+" content: "+i+";"):(r=u,g.asArray(r).map(f).join("; ")+";"),document.createTextNode(a+"{"+c+"}"))),m.appendChild(n))}[":before",":after"].forEach(function(e){t(e)})}function n(){h instanceof HTMLTextAreaElement&&(m.innerHTML=h.value),h instanceof HTMLInputElement&&m.setAttribute("value",h.value)}function r(){m instanceof SVGElement&&(m.setAttribute("xmlns","http://www.w3.org/2000/svg"),m instanceof SVGRectElement&&["width","height"].forEach(function(e){var t=m.getAttribute(e);t&&m.style.setProperty(e,t)}))}}}(e,i.filter,!0)}).then(A).then(v).then(function(t){i.bgcolor&&(t.style.backgroundColor=i.bgcolor);i.width&&(t.style.width=i.width+"px");i.height&&(t.style.height=i.height+"px");i.style&&Object.keys(i.style).forEach(function(e){t.style[e]=i.style[e]});return t}).then(function(e){return t=e,n=i.width||g.width(o),r=i.height||g.height(o),Promise.resolve(t).then(function(e){return e.setAttribute("xmlns","http://www.w3.org/1999/xhtml"),(new XMLSerializer).serializeToString(e)}).then(g.escapeXhtml).then(function(e){return'<foreignObject x="0" y="0" width="100%" height="100%">'+e+"</foreignObject>"}).then(function(e){return'<svg xmlns="http://www.w3.org/2000/svg" width="'+n+'" height="'+r+'">'+e+"</svg>"}).then(function(e){return"data:image/svg+xml;charset=utf-8,"+e});var t,n,r})}function p(n,r){return d(n,r).then(g.makeImage).then(g.delay(100)).then(function(e){var t=function(e){var t=document.createElement("canvas");{var n;t.width=r.width||g.width(e),t.height=r.height||g.height(e),r.bgcolor&&((n=t.getContext("2d")).fillStyle=r.bgcolor,n.fillRect(0,0,t.width,t.height))}return t}(n);return t.getContext("2d").drawImage(e,0,0),t})}function A(n){return t.resolveAll().then(function(e){var t=document.createElement("style");return n.appendChild(t),t.appendChild(document.createTextNode(e)),n})}function v(e){return s.inlineAll(e).then(function(){return e})}e.domtoimage=m}(window); |