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 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)
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}}) => {
console.log({KEYWORD_CONDITION, text})
if(text.includes(keywordToClick)) {
const dom = new jsdom.JSDOM(hocr)
_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)
_coorStr = span.title
return resToFinished(true)
}
}
}
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.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(async (res, rej) => {
await _captureImage();
await _convertImageToDomString();
await _detectHTML().then(_handleClick).catch(rej);
})
}
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)
})
})
});
});
});
}
await (async function search() {
robotjs.keyTap('home', ['alt']);
robotjs.typeString(keywordSearch);
robotjs.keyTap('enter')
return new Promise(res => setTimeout(res, waitSearch))
}())
function recusiveScroll(countScroll = 0, limitStopScroll = 20) {
await (async function recusiveScroll(countScroll = 1, limitStopScroll = 20) {
if(countScroll > limitStopScroll) {
return;
}
handleToClick(KEYWORD_CONDITION)
.catch(() => {
recusiveScroll(++countScroll, limitStopScroll)
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)
}())

3
index.js Normal file
View File

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