const puppeteer = require('puppeteer-core');
const fs = require('fs');
const path = require('path');
const mkdir=require('./utils/mkdir');
const Step=require('./utils/Step');
const sleep=function (time) {
return new Promise(function (resolve) {
setTimeout(resolve,time)
})
};
mkdir('./dist/cookie/')
const getDefaultOsPath = () => {
if (process.platform === 'win32') {
return 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
} else {
return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
}
}
const getUserDataDir = () => {
if (process.platform === 'win32') {
return 'D:\\User Data'
} else {
return '/Users/caoke/chromeUserdata'
}
}
const config=require('./config.js');
const hello={
async init (config) {
this.config=config;
this.pngi=0;
this.name=config.name;
const the=this;
const stepArr=['_start',...config.stepArr,'end'];
this.progree=new Step(stepArr,async function (curStep,time) {
// console.log(curStep)
if(config[curStep]){
await the.runActionName(curStep,time)
}else if(the[curStep]){
await the[curStep](curStep,time)
}else{
console.log(curStep)
}
})
this.progree.run()
},
async runActionName(curStep){
const arr=this.config[curStep]
await this.runActionArr(arr)
this.progree.waitSecondAndGo(0);
},
async runActionArr(arr){
const page=this.page;
for(let i=0;i<arr.length;i++){
const action=arr[i];
const key=action[0];
console.log(key)
if(key==='click'||key==='mousedown'||key==='mousemove'||key==='mouseup'){
page.click(action[1]);
}else if(key==='keypress'){
await page.keyboard.type(action[1]);
}else if(key==='clickAndtype'){
await page.type(action[1],action[2],{delay: 100});
}else if(key==='type'){
await page.keyboard.type(action[1],{delay: 100});
}else if(key==='sleep'){
await sleep(action[1])
}else if(key==='uploadFile'){
const elementHandle=await page.$(action[1]);
await elementHandle.uploadFile(action[2])
await sleep(action[1])
}else if(key==='goBack'){
await page.goBack()
}else if(key==='userAgent'){
await page.setUserAgent(action[1])
}else if(key==='viewport'){
await page.setViewport(action[1])
}else if(key==='openUrl'){
await page.goto(action[1]);
}else if(key==='emulate'){
await page.emulate(puppeteer.devices[action[1]]);
}else if(key==='log'){
if(typeof action[1]==='string'){
await this.addLog(action[1]);
}else{
const res=await page.evaluate(action[1])
if(typeof res=='string'){
await this.addLog(res);
}else if(typeof res=='object'){
if(res.error){
throw res.error;
}
res.log&&await this.addLog(res.log);
res.action&&await this.runActionArr([res.action]);
res.actions&&await this.runActionArr(res.actions);
if(typeof res.goto=='number'){
i=i+res.goto-1;
}
if(res.go){
this.progree.waitSecondAndGo(0,res.go);
break;
}
}
}
}
}
},
//添加日志和截图
async addLog(log){
if(!log){return}
const page=this.page;
const curStep=this.progree.curStep;
const dir='./dist/'+this.name+'/'+curStep+'/';
const pngpath=dir+(++this.pngi)+'.png'
console.log(log,pngpath);
mkdir(dir);
await page.screenshot({path:pngpath})
},
async _start() {
this.browser = await puppeteer.launch({
headless: false,
ignoreDefaultArgs:true,
// args :['--user-data-dir=C:\\Users\\caoke\\AppData\\Local\\Google\\Chrome\\User Data'],
args :['--user-data-dir='+getUserDataDir()],
executablePath:getDefaultOsPath()
});
const page=this.page = await this.browser.newPage();
this.progree.waitSecondAndGo(0);
page.on('response',async function (res) {
const buffer=await res.buffer();
if(!res.fromCache()&&buffer){
const url=res.url();
let filepath='./dist/'+url.replace(':/','').replace(/:/g,'').replace(/\?.+$/,'');
mkdir(filepath);
try {
const dir=path.dirname(filepath);
if(fs.statSync(dir).isFile()){
fs.renameSync(dir,dir+'.x')
mkdir(filepath);
}
fs.writeFileSync(filepath,buffer);
}catch (e) {
fs.writeFileSync(filepath+'.x',buffer);
}
}
})
},
async end(){
// await this.browser.close();
},
async error(){
// await this.browser.close();
},
};
hello.init(config);