/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/doc88-download@1.0.0/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
var ppapiFlashPath,mainWin,path=require("path"),fs=require("fs"),app=require("electron").app,ipc=require("electron").ipcMain,session=require("electron").session,BrowserWindow=require("electron").BrowserWindow,request=require("request"),mkdirp=require("mkdirp"),async=require("async"),jimp=require("jimp"),debug=require("debug")("doc88-download"),program=require("commander"),doc88util=require("./doc88util");function myParseInt(e,r){var a=parseInt(e,10);return"number"==typeof a?a:r}function main(){debug("evt: ready"),session.defaultSession.webRequest.onBeforeSendHeaders((function(e,r){var a="mainFrame"!=e.resourceType;"script"==e.resourceType&&-1!=["/jquery","/view-mini.js"].map((function(r,a){return-1!=e.url.indexOf(r)})).indexOf(!0)&&(a=!1),a||debug("req: "+e.method+" - "+e.url+" ["+e.resourceType+"]"),r({cancel:a})})),mainWin=new BrowserWindow({show:!1}),console.log("Fetching "+program.args[0]),mainWin.loadURL(program.args[0]),mainWin.webContents.once("did-finish-load",(function(){debug("evt: did-finish-load"),mainWin.webContents.executeJavaScript("\n            require('electron').ipcRenderer.sendSync('document:retrieved', {\n                'html': document.documentElement.innerHTML,\n                'mpib': window.mpib,\n                'mtp': window.mtp,\n                'mhost': window.mhost,\n                'mhi': window.mhi,\n                'mpebt': window.mpebt,\n                'madif': window.madif,\n                'p_s': window.p_s,\n                'product_code': window.product_code\n            });\n        ")}))}program.version(require("./package.json").version).usage("[options] <url>").option("-o, --out-dir <dir>",'output directory, default "./output"',"output").option("-f, --plugin-flash-path <path>","flash Player plugin path").option("-d, --force-download","force download if PNG already exists").option("-w, --wait <ms>","milliseconds to wait before capture, default 500",myParseInt,500).option("-c, --concurrent-worker <max>","max concurrent worker for capture, default 1",myParseInt,1).option("-t, --scale-up-factor <t>","scale up factor, default 5",myParseInt,5).option("-s, --skip-check","skip captured image checking").parse(process.argv),program.args&&0!=program.args.length||program.help(),debug("args: out-dir: %s",program.outDir),debug("args: plugin-flash-path: %s",program.pluginFlashPath),debug("args: force-download: %s",!!program.forceDownload),debug("args: wait: %s",program.wait),debug("args: concurrent-worker: %s",program.concurrentWorker),debug("args: scale-up-factor: %s",program.scaleUpFactor),debug("args: url: %s",program.args),program.pluginFlashPath?ppapiFlashPath=program.pluginFlashPath:"win32"==process.platform?ppapiFlashPath=path.join(__dirname,"plugins","pepflashplayer.dll"):"linux"==process.platform?ppapiFlashPath=path.join(__dirname,"plugins","libpepflashplayer.so"):"darwin"==process.platform&&(ppapiFlashPath=path.join(__dirname,"plugins","PepperFlashPlayer.plugin")),app.commandLine.appendSwitch("ppapi-flash-path",ppapiFlashPath),debug('append switch ppapi-flash-path="%s"',ppapiFlashPath),app.on("ready",main),app.on("window-all-closed",(function(){})),mkdirp(program.outDir),ipc.on("document:retrieved",(function(e,r){if(debug("evt: document:retrieved"),console.log("Page loaded"),!r.mpib)return console.log("Unable to get page context, please check network connection and URL"),app.exit(1);var a=doc88util.decodePageContext(r.mpib).split(","),o=path.resolve(path.join(program.outDir,r.product_code)),n=path.join(o,"html"),t=path.join(o,"png");if(mkdirp.sync(o),mkdirp.sync(n),mkdirp.sync(t),r&&r.product_code&&r.html){var i=path.join(o,"raw.html");fs.writeFileSync(i,r.html),console.log(`Wrote raw html file '${i}'`)}var p=String(r.mtp).length;console.log("Total pages: "+r.mtp);for(var s=[],l=[],c=0;c<r.mtp;c++){var d=doc88util.constructFlashParams(c+1,a,r.mtp,r.mhost,r.mhi,r.mpebt,r.madif,r.p_s),u=a[c].split("-"),m=parseInt(u[1])*program.scaleUpFactor,g=parseInt(u[2])*program.scaleUpFactor,h=`<!DOCTYPE html>\n<html><body style="width:${m}px;height:${g}px;overflow:hidden;margin:0"><object type="application/x-shockwave-flash" data="http://assets.doc88.com/assets/swf/pv.swf?v=1.7" width="100%" height="100%" style="visibility: visible;"><param name="hasPriority" value="true"><param name="wmode" value="transparent"><param name="swliveconnect" value="true"><param name="FlashVars" value="`+d+'"><param name="allowScriptAccess" value="always"></object></body></html>',f=path.join(n,`${w(c+1,p)}.html`);fs.writeFileSync(f,h),l.push(f),s[c]={width:m,height:g}}function w(e,r,a){return Array(r-String(e).length+1).join(a||"0")+e}console.log(`Wrote page html to directory '${n}'`),session.defaultSession.webRequest.onBeforeSendHeaders(null),async.eachOfLimit(l,program.concurrentWorker,(function(e,r,a){var o=l[r],n=path.join(t,`${w(r+1,p)}.png`);if(!program.forceDownload&&fs.existsSync(n))return console.log(`Image for page ${r+1} already exists, skip processing '${o}'`),a();var i=new BrowserWindow({useContentSize:!0,frame:!1,show:!1,autoHideMenuBar:!0,enableLargerThanScreen:!0,backgroundColor:"#FF0000",webPreferences:{plugins:!0}});i.webContents.once("did-finish-load",(function(){var e,t,p;debug("evt: did-finish-load: `%s`",o),i.setSize(s[r].width,s[r].height);var l=0;async.doWhilst((function(r){setTimeout((function(){debug("capturing image of '%s'",o),i.capturePage((function(a){e=a,jimp.read(a.resize({height:480,quality:"good"}).toPNG(),(function(e,a){if(e)return r(new Error("Unable to convert NativeImage to Jimp image"));new jimp((t=a).bitmap.width,t.bitmap.height,4278190335,(function(e,a){if(e)return r(new Error("Unable to create red image for comparison"));p=a,r()}))}))}))}),program.wait)}),(function(){if(program.skipCheck)return!1;debug("checking image captured from '%s'",o);var e=jimp.diff(t,p);return debug("image pixel diff: %s",e.percent),l++<10&&e.percent<.005}),(function(r){r?(console.log("Unable to capture image of '%s': %s",o,r),i.destroy(),a()):fs.writeFile(n,e.toPNG(),(function(e){e?console.log("Unable to write file '%s': %s",n,e):console.log(`Processed '${o}', image saved at '${n}'`),i.destroy(),a()}))}))})),debug("load "+o),i.loadURL("file://"+o)}),(function(e){e&&console.err(e),console.log("Processed all pages"),app.exit(0)})),e.returnValue=!0,mainWin.destroy()}));
//# sourceMappingURL=/sm/721d64409f9db91d2d5601d024413c931bf7763b6595042e6fa6acb6bb12c77e.map