1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| const puppeteer = require('puppeteer'); const devices = require('puppeteer/DeviceDescriptors'); const iPhone = devices['iPhone 6 Plus']; let timeout = function (delay) { return new Promise((resolve, reject) => { setTimeout(() => { try { resolve(1) } catch (e) { reject(0) } }, delay); }) }
let page = null let btn_position = null let times = 0 const distanceError = [-10,2,3,5]
async function run() { const browser = await puppeteer.launch({ headless:false }); page = await browser.newPage();
await page.emulate(iPhone); await page.goto('https://www.qdfuns.com/'); await timeout(1000);
page.click('a[data-type=login]') await timeout(1000);
page.type('input[data-type=email]','你的账号') await timeout(500); page.type('input[placeholder=密码]','你的密码') await timeout(1000);
page.click('.geetest_radar_tip') await timeout(1000);
btn_position = await getBtnPosition();
drag(null) }
async function calculateDistance() { const distance = await page.evaluate(() => {
function compare(document) { const ctx1 = document.querySelector('.geetest_canvas_fullbg'); const ctx2 = document.querySelector('.geetest_canvas_bg'); const pixelDifference = 30; let res = [];
for(let i=57;i<260;i++){ for(let j=1;j<160;j++) { const imgData1 = ctx1.getContext("2d").getImageData(1*i,1*j,1,1) const imgData2 = ctx2.getContext("2d").getImageData(1*i,1*j,1,1) const data1 = imgData1.data; const data2 = imgData2.data; const res1=Math.abs(data1[0]-data2[0]); const res2=Math.abs(data1[1]-data2[1]); const res3=Math.abs(data1[2]-data2[2]); if(!(res1 < pixelDifference && res2 < pixelDifference && res3 < pixelDifference)) { if(!res.includes(i)) { res.push(i); } } } } return {min:res[0]-7,max:res[res.length-1]-54} } return compare(document) }) return distance; }
async function getBtnPosition() { const btn_position = await page.evaluate(() => { const {clientWidth,clientHeight} = document.querySelector('.geetest_popup_ghost') return {btn_left:clientWidth/2-104,btn_top:clientHeight/2+59} }) return btn_position; }
async function tryValidation(distance) { const distance1 = distance - 10 const distance2 = 10
page.mouse.click(btn_position.btn_left,btn_position.btn_top,{delay:2000}) page.mouse.down(btn_position.btn_left,btn_position.btn_top) page.mouse.move(btn_position.btn_left+distance1,btn_position.btn_top,{steps:30}) await timeout(800); page.mouse.move(btn_position.btn_left+distance1+distance2,btn_position.btn_top,{steps:20}) await timeout(800); page.mouse.up() await timeout(4000);
const isSuccess = await page.evaluate(() => { return document.querySelector('.geetest_success_radar_tip_content') && document.querySelector('.geetest_success_radar_tip_content').innerHTML }) await timeout(1000); const reDistance = await page.evaluate(() => { return document.querySelector('.geetest_result_content') && document.querySelector('.geetest_result_content').innerHTML }) await timeout(1000); return {isSuccess:isSuccess==='验证成功',reDistance:reDistance.includes('怪物吃了拼图')} }
async function drag(distance) { distance = distance || await calculateDistance(); const result = await tryValidation(distance.min) if(result.isSuccess) { await timeout(1000); console.log('验证成功') page.click('#modal-member-login button') }else if(result.reDistance) { console.log('重新计算滑距离录,重新滑动') times = 0 await drag(null) } else { if(distanceError[times]){ times ++ console.log('重新滑动') await drag({min:distance.max,max:distance.max+distanceError[times]}) } else { console.log('滑动失败') times = 0 run() } } }
run()
|