94 lines
2.8 KiB
JavaScript
94 lines
2.8 KiB
JavaScript
/**
|
|
* NOTE: install "dark mode" on browser because tesseract need contract to detect text
|
|
* NOTE: zoom 120% on browser because below
|
|
*/
|
|
|
|
const robotjs = require('robotjs')
|
|
const tesseract = require('tesseract.js')
|
|
const jsdom = require('jsdom')
|
|
const Jimp = require('jimp')
|
|
|
|
const KEY_SEARCH = ' CAB-HV-25A-SG-IN1 prology.net' + ' '
|
|
const KEYWORD_CONDITION = 'Prology.net'
|
|
const SCALE = 1.5
|
|
|
|
async function handleToClick(keywordToClick, fileScreenshot = 'screenshot.jpg') {
|
|
if(!fileScreenshot) {
|
|
fileScreenshot = 'screenshot.jpg'
|
|
}
|
|
|
|
const _randomInteger = (min, max) => {
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
}
|
|
|
|
const _asyncDetectAndToClick = async (imageFile, res, rej) => {
|
|
return await tesseract.recognize(imageFile)
|
|
.then(async ({data: {hocr, text}}) => {
|
|
console.log({KEYWORD_CONDITION, text})
|
|
if(text.includes(keywordToClick)) {
|
|
const dom = new jsdom.JSDOM(hocr)
|
|
const spanEls = dom.window.document.querySelectorAll('span')
|
|
for await(let span of spanEls) {
|
|
const textContent = span.textContent.split(' ').join('')
|
|
if(textContent.includes(keywordToClick)) {
|
|
const [type, left, top, right, bottom] = span.title.split(' ')
|
|
const x = parseInt((parseInt(left) + _randomInteger(20, 40)) / SCALE)
|
|
const y = parseInt((parseInt(top) - _randomInteger(0, parseInt(top) - parseInt(bottom))) / SCALE)
|
|
robotjs.moveMouse(x, y)
|
|
robotjs.mouseClick('left')
|
|
console.log('moveMouse:', {x, y}, {
|
|
left, bottom, right, top
|
|
})
|
|
await new Promise(res => setTimeout(() => {
|
|
// Capture
|
|
|
|
}, 2000))
|
|
break;
|
|
}
|
|
}
|
|
return res(true)
|
|
}
|
|
robotjs.keyTap('down')
|
|
robotjs.keyTap('down')
|
|
return rej(false)
|
|
})
|
|
}
|
|
|
|
return new Promise((res, rej) => {
|
|
const capture = robotjs.screen.capture()
|
|
new Jimp(
|
|
{data: capture.image, width: capture.width, height: capture.height},
|
|
(err, image) => {
|
|
image.greyscale((err, image) => {
|
|
image.scale(SCALE, (err, image) => {
|
|
image.writeAsync(fileScreenshot).then(() => {
|
|
return _asyncDetectAndToClick(fileScreenshot, res, rej)
|
|
})
|
|
})
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function recusiveScroll(countScroll = 0, limitStopScroll = 20) {
|
|
if(countScroll > limitStopScroll) {
|
|
return;
|
|
}
|
|
|
|
handleToClick(KEYWORD_CONDITION)
|
|
.catch(() => {
|
|
recusiveScroll(++countScroll, limitStopScroll)
|
|
})
|
|
}
|
|
|
|
(async function start() {
|
|
robotjs.keyTap('home', ['alt']);
|
|
robotjs.typeString(KEY_SEARCH);
|
|
robotjs.keyTap('enter')
|
|
|
|
// wait search
|
|
setTimeout(async () => {
|
|
await recusiveScroll(0, 30)
|
|
}, 2000)
|
|
}())
|