mhtml2html
Version:
Converts .mhtml to a single .html page.
1 lines • 6.73 kB
JavaScript
;var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};!function(e){function t(e,t){if(!e)throw new Error(t)}function n(e){var t=/[\\\"\x00-\x1f\x7f-\uffff]/g,n={"\b":"\b","\t":"\t","\n":"\n","\f":"\f","\r":"\r",'"':'"',"\\":"\\"};return t.lastIndex=0,t.test(e)?e.replace(t,function(e){var t=n[e];return"string"==typeof t?t:"\\"+("0000"+e.charCodeAt(0).toString(16)).slice(-4)}):e}var o=void 0;"undefined"!=typeof require&&(o=require);var r="url(",i=void 0,d=void 0;void 0!=e&&(i=e.mhtml2html),d=function(){var e=String.fromCharCode,t=function(t){return t.replace(/[\t\x20]$/gm,"").replace(/=(?:\r\n?|\n|$)/g,"").replace(/=([a-fA-F0-9]{2})/g,function(t,n){var o=parseInt(n,16);return e(o)})},n=function(e){return e.replace(/\x20$/,"=20").replace(/\t$/,"=09")},o=/[\0-\x08\n-\x1F=\x7F-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]/g,r=function(e){for(var t=e.replace(o,function(e){if(e>"ÿ")throw RangeError("`quotedPrintable.encode()` expects extended ASCII input only. Don’t forget to encode the input first using a character encoding like UTF-8.");var t=e.charCodeAt(0),n=t.toString(16).toUpperCase();return"="+("0"+n).slice(-2)}),r=t.split(/\r\n?|\n/g),i=-1,d=r.length,a=[];++i<d;)for(var f=r[i],u=75,s=0,p=f.length;s<p;){var c=t.slice(s,s+u);/=$/.test(c)?(c=c.slice(0,u-1),s+=u-1):/=[A-F0-9]$/.test(c)?(c=c.slice(0,u-2),s+=u-2):s+=u,a.push(c)}var l=c.length;return/[\t\x20]$/.test(c)&&(a.pop(),l+2<=u+1?a.push(n(c)):a.push(c.slice(0,l-1),n(c.slice(l-1,l)))),a.join("=\r\n")};return{encode:r,decode:t,version:"1.0.0"}}();var a={noConflict:function(){return e.mhtml2html=i,a},parse:function(e){function n(){for(;t(x<e.length-1,"Unexpected EOF"),/\s/.test(e[x]);)x++,"\n"==e[x]&&E++}function r(n){var o;for(T=x;;){if("\n"==e[x]){x++,E++;break}t(x++<e.length-1,"Unexpected EOF")}switch(o=e.substring(T,x),n){case"quoted-printable":return d.decode(o);case"base64":return o.trim();default:return o}}function i(e,n){var o=e.split(/[:=](.+)?/);t(o.length>=2,"Invalid header; Line "+E),n[o[0].trim()]=o[1].trim()}var a=arguments.length>1&&void 0!==arguments[1]&&arguments[1],f={MHTML_HEADERS:0,MTHML_CONTENT:1,MHTML_DATA:2,MHTML_END:3},u=void 0,s=void 0,p=void 0,c=void 0,l=void 0,v=void 0,y=void 0,h=void 0,m=void 0,g=void 0,b=void 0,M=void 0,x=void 0,T=void 0,E=void 0,L=void 0,_=void 0;for(s={},p={},c={},l={},g=f.MHTML_HEADERS,x=E=0;g!=f.MHTML_END;)switch(g){case f.MHTML_HEADERS:b=r(),0!=b&&"\n"!=b?i(b,s):(L=s.boundary,t(void 0!==L,"Missing boundary from document headers; Line "+E),L=L.replace(/\"/g,""),n(),b=r(),t(b.includes(L),"Expected boundary; Line "+E),p={},g=f.MTHML_CONTENT);break;case f.MTHML_CONTENT:b=r(),0!=b&&"\n"!=b?i(b,p):(y=p["Content-Transfer-Encoding"],h=p["Content-Type"],m=p["Content-ID"],v=p["Content-Location"],void 0===M&&(M=v,t(void 0!==M&&"text/html"===h,"Index not found; Line "+E)),t(void 0!==m||void 0!==v,"ID or location header not provided; Line "+E),t(void 0!==y,"Content-Transfer-Encoding not provided; Line "+E),t(void 0!==h,"Content-Type not provided; Line "+E),u={encoding:y,type:h,data:"",id:m},void 0!==m&&(l[m]=u),void 0!==v&&void 0===c[v]&&(c[v]=u),n(),p={},g=f.MHTML_DATA);break;case f.MHTML_DATA:for(b=r(y);!b.includes(L);)u.data+=b,b=r(y);try{u.data=decodeURIComponent(escape(u.data))}catch(e){}if(a===!0&&void 0!==M)return"undefined"==typeof DOMParser?(t("undefined"!=typeof o,"Require is not defined."),(_=o("jsdom").jsdom)(u.data,{})):(_=new DOMParser,_.parseFromString(u.data,"text/html"));g=x>=e.length-1?f.MHTML_END:f.MTHML_CONTENT}return{frames:l,media:c,index:M}},convert:function(e){function i(e,t){function n(n){r=e.split("/"),i=t.split("/"),n&&r.pop();for(var o=0;o<i.length;o++)".."==i[o]?r.pop():"."!=i[o]&&r.push(i[o]);return r.join("/")}var o,r,i,d;return/^((http|https|ftp):\/\/)/.test(t)?t:"/"==t[0]?(o=e.match(new RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?")),o[2]+"://"+o[4]+t):(d=n(),null==p[d]?n(!0):d)}function f(e,t){var o,r;return v=t.substring(y,t.indexOf(")",y)),y+=v.length,o=i(e,v.replace(/(\"|\')/g,"")),null==p[o]?null:(v="url('data:"+p[o].type+";base64,"+("base64"===p[o].encoding?p[o].data:m(unescape(encodeURIComponent(n(p[o].data)))))+"')",r=y,y=h+v.length,""+t.substring(0,h)+v+t.substring(r+1))}function u(e){var t=void 0,o=void 0,i=void 0,a=void 0,u=void 0,c=void 0,l=void 0;for(i=[e];i.length;){for(t=i.shift(),o=new Array(Object.keys(t.childNodes).length),y=0;y<o.length;y++)o[y]=t.childNodes[y];o.forEach(function(o){switch(o.getAttribute&&(u=o.getAttribute("href"),c=o.getAttribute("src")),o.tagName){case"HEAD":a=e.createElement("base"),a.setAttribute("target","_parent"),o.insertBefore(a,o.firstChild);break;case"LINK":if("undefined"!=typeof p[u]&&"text/css"===p[u].type){for(l=e.createElement("style"),l.type="text/css",y=0;(y=p[u].data.indexOf(r,y))>0;)h=y,y+=r.length,v=f(u,p[u].data),null!=v&&(p[u].data=v);l.appendChild(e.createTextNode(n(p[u].data))),t.replaceChild(l,o)}break;case"IMG":if("undefined"!=typeof p[c]&&p[c].type.includes("image")){switch(p[c].encoding){case"quoted-printable":v="data:"+p[c].type+";utf8,"+d.decode(p[c].data);break;case"base64":v="data:"+p[c].type+";base64,"+p[c].data;break;default:v="data:"+p[c].type+";base64,"+m(unescape(encodeURIComponent(n(p[c].data))))}o.setAttribute("src",v)}default:for(l in o.style)if("string"==typeof o.style[l])for(;(y=o.style[l].indexOf(r,y))>0;)h=y,y+=r.length,v=f(s,o.style[l]),null!=v&&(o.style[l]=v)}o.removeAttribute&&o.removeAttribute("integrity"),i.push(o)})}return e}var s=void 0,p=void 0,c=void 0,l=void 0,v=void 0,y=void 0,h=void 0;("undefined"==typeof e?"undefined":_typeof(e))===_typeof("")?e=a.parse(e):t(("undefined"==typeof e?"undefined":_typeof(e))===_typeof({}),"Expected argument of type string or object"),c=e.frames,p=e.media,s=e.index,t(("undefined"==typeof c?"undefined":_typeof(c))===_typeof({}),"MHTML error: invalid frames"),t(("undefined"==typeof p?"undefined":_typeof(p))===_typeof({}),"MHTML error: invalid media"),t(("undefined"==typeof s?"undefined":_typeof(s))===_typeof(" "),"MHTML error: invalid index"),t(p[s]&&"text/html"===p[s].type,"MHTML error: invalid index");var m=void 0;return"undefined"==typeof btoa?(t("undefined"!==o,"Require is not defined."),m=o("btoa")):m=btoa,"undefined"==typeof DOMParser?(t("undefined"!=typeof o,"Require is not defined."),l=o("jsdom").jsdom,u(l(p[s].data,{}))):(l=new DOMParser,u(l.parseFromString(p[s].data,"text/html")))}};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=a),exports.mhtml2html=a):void 0!=e&&(e.mhtml2html=a)}("undefined"!=typeof window?window:null);