upgrade seo tool to v2

This commit is contained in:
Kai Ton 2024-05-25 16:32:56 +00:00
parent 655241ee27
commit d52c75efc2
2 changed files with 87 additions and 61 deletions

View File

@ -8,86 +8,109 @@ const tesseract = require('tesseract.js')
const jsdom = require('jsdom') const jsdom = require('jsdom')
const Jimp = require('jimp') const Jimp = require('jimp')
const KEY_SEARCH = ' CAB-HV-25A-SG-IN1 prology.net' + ' ' module.exports = async function(keywordSearch, keywordToClick, scale = 1.5, fileScreenshot = 'screenshot.jpg', waitSearch = 2000) {
const KEYWORD_CONDITION = 'Prology.net' if(!scale) {
const SCALE = 1.5 scale = 1.5
}
async function handleToClick(keywordToClick, fileScreenshot = 'screenshot.jpg') {
if(!fileScreenshot) { if(!fileScreenshot) {
fileScreenshot = 'screenshot.jpg' fileScreenshot = 'screenshot.jpg'
} }
const _randomInteger = (min, max) => { if(!waitSearch) {
waitSearch = 2000
}
const $_randomInteger = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min; return Math.floor(Math.random() * (max - min + 1)) + min;
} }
const _asyncDetectAndToClick = async (imageFile, res, rej) => { async function handleToClick() {
return await tesseract.recognize(imageFile) 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}}) => { .then(async ({data: {hocr, text}}) => {
console.log({KEYWORD_CONDITION, text}) _horc = hocr
if(text.includes(keywordToClick)) { _text = text
const dom = new jsdom.JSDOM(hocr) })
}
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') const spanEls = dom.window.document.querySelectorAll('span')
for await(let span of spanEls) { for await(let span of spanEls) {
const textContent = span.textContent.split(' ').join('') const textContent = span.textContent.split(' ').join('')
if(textContent.includes(keywordToClick)) { if(textContent.includes(keywordToClick)) {
const [type, left, top, right, bottom] = span.title.split(' ') _coorStr = span.title
const x = parseInt((parseInt(left) + _randomInteger(20, 40)) / SCALE) return resToFinished(true)
const y = parseInt((parseInt(top) - _randomInteger(0, parseInt(top) - parseInt(bottom))) / SCALE) }
}
}
robotjs.keyTap('down')
robotjs.keyTap('down')
return rejToAgain(false)
})
}
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.moveMouse(x, y)
robotjs.mouseClick('left') robotjs.mouseClick('left')
console.log('moveMouse:', {x, y}, { console.log('moveMouse:', {x, y}, {
left, bottom, right, top left, bottom, right, top
}) })
await new Promise(res => setTimeout(() => { }
// Capture
}, 2000)) return new Promise(async (res, rej) => {
break; await _captureImage();
} await _convertImageToDomString();
} await _detectHTML().then(_handleClick).catch(rej);
return res(true)
}
robotjs.keyTap('down')
robotjs.keyTap('down')
return rej(false)
}) })
} }
return new Promise((res, rej) => { await (async function search() {
const capture = robotjs.screen.capture() robotjs.keyTap('home', ['alt']);
new Jimp( robotjs.typeString(keywordSearch);
{data: capture.image, width: capture.width, height: capture.height}, robotjs.keyTap('enter')
(err, image) => { return new Promise(res => setTimeout(res, waitSearch))
image.greyscale((err, image) => { }())
image.scale(SCALE, (err, image) => {
image.writeAsync(fileScreenshot).then(() => {
return _asyncDetectAndToClick(fileScreenshot, res, rej)
})
})
});
});
});
}
function recusiveScroll(countScroll = 0, limitStopScroll = 20) { await (async function recusiveScroll(countScroll = 1, limitStopScroll = 20) {
if(countScroll > limitStopScroll) { if(countScroll > limitStopScroll) {
return; return;
} }
handleToClick(KEYWORD_CONDITION) await handleToClick(keywordToClick, scale)
.catch(() => { .catch(async () => {
recusiveScroll(++countScroll, limitStopScroll) 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)
}())

3
index.js Normal file
View File

@ -0,0 +1,3 @@
const autoClick = require('./AutoClick')
autoClick('prology Cisco Systems', 'Prology.net')