From d52c75efc276369d9ffa6b6eae53344544d49aef Mon Sep 17 00:00:00 2001 From: "kai.t" Date: Sat, 25 May 2024 16:32:56 +0000 Subject: [PATCH] upgrade seo tool to v2 --- AutoClick.js | 145 +++++++++++++++++++++++++++++---------------------- index.js | 3 ++ 2 files changed, 87 insertions(+), 61 deletions(-) create mode 100644 index.js diff --git a/AutoClick.js b/AutoClick.js index 2cb658c..5765f51 100644 --- a/AutoClick.js +++ b/AutoClick.js @@ -8,86 +8,109 @@ 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 +module.exports = async function(keywordSearch, keywordToClick, scale = 1.5, fileScreenshot = 'screenshot.jpg', waitSearch = 2000) { + if(!scale) { + scale = 1.5 + } -async function handleToClick(keywordToClick, fileScreenshot = 'screenshot.jpg') { if(!fileScreenshot) { fileScreenshot = 'screenshot.jpg' } - const _randomInteger = (min, max) => { + if(!waitSearch) { + waitSearch = 2000 + } + + 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) + async function handleToClick() { + let _horc = ''; + let _text = ''; + let _coorStr = ''; + + + const _captureImage = async () => { + const capture = robotjs.screen.capture() + return new Promise(res => { + 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(() => { + res(true) + }) + }) + }); + }); + }) + } + + const _convertImageToDomString = async () => { + await tesseract + .recognize(fileScreenshot) + .then(async ({data: {hocr, text}}) => { + _horc = hocr + _text = text + }) + } + + const _detectHTML = async () => { + console.log({keywordToClick, _text}) + return new Promise(async (resToFinished, rejToAgain) => { + if(_text.includes(keywordToClick)) { + const dom = new jsdom.JSDOM(_horc) 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; + _coorStr = span.title + return resToFinished(true) } } - return res(true) } + robotjs.keyTap('down') robotjs.keyTap('down') - return rej(false) + return rejToAgain(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) - }) - }) - }); - }); - }); -} + async function _handleClick() { + const [type, left, top, right, bottom] = _coorStr.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 + }) + } -function recusiveScroll(countScroll = 0, limitStopScroll = 20) { - if(countScroll > limitStopScroll) { - return; - } - - handleToClick(KEYWORD_CONDITION) - .catch(() => { - recusiveScroll(++countScroll, limitStopScroll) + return new Promise(async (res, rej) => { + await _captureImage(); + await _convertImageToDomString(); + await _detectHTML().then(_handleClick).catch(rej); }) + } + + await (async function search() { + robotjs.keyTap('home', ['alt']); + robotjs.typeString(keywordSearch); + robotjs.keyTap('enter') + return new Promise(res => setTimeout(res, waitSearch)) + }()) + + await (async function recusiveScroll(countScroll = 1, limitStopScroll = 20) { + if(countScroll > limitStopScroll) { + return; + } + + await handleToClick(keywordToClick, scale) + .catch(async () => { + await 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) -}()) diff --git a/index.js b/index.js new file mode 100644 index 0000000..5463cf4 --- /dev/null +++ b/index.js @@ -0,0 +1,3 @@ +const autoClick = require('./AutoClick') + +autoClick('prology Cisco Systems', 'Prology.net')