Improvements to speed
This commit is contained in:
parent
5da02b5c28
commit
4dafb9c1b7
1 changed files with 30 additions and 38 deletions
|
@ -7,7 +7,7 @@
|
||||||
// ==UserScript==
|
// ==UserScript==
|
||||||
// @name Math Problem Solver
|
// @name Math Problem Solver
|
||||||
// @namespace https://github.com/adithyagenie/skillrack-captcha-solver
|
// @namespace https://github.com/adithyagenie/skillrack-captcha-solver
|
||||||
// @version 0.1
|
// @version 0.2
|
||||||
// @description Solves math captcha in SkillRack using Tesseract.js
|
// @description Solves math captcha in SkillRack using Tesseract.js
|
||||||
// @author adithyagenie
|
// @author adithyagenie
|
||||||
// @include https://*.skillrack.com/*
|
// @include https://*.skillrack.com/*
|
||||||
|
@ -26,61 +26,53 @@
|
||||||
console.log("Captcha not found!");
|
console.log("Captcha not found!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const time = new Date().getTime();
|
||||||
|
|
||||||
// Invert colours for better ocr
|
// Invert colours for better ocr
|
||||||
function invertImageColors(image) {
|
function invertColors(image) {
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
|
||||||
|
// Set canvas dimensions to match the image
|
||||||
canvas.width = image.width;
|
canvas.width = image.width;
|
||||||
canvas.height = image.height;
|
canvas.height = image.height;
|
||||||
const ctx = canvas.getContext("2d");
|
|
||||||
ctx.drawImage(image, 0, 0, image.width, image.height);
|
|
||||||
|
|
||||||
const imageData = ctx.getImageData(0, 0, image.width, image.height);
|
// Draw the image onto the canvas
|
||||||
const data = imageData.data;
|
ctx.drawImage(image, 0, 0);
|
||||||
|
|
||||||
for (let i = 0; i < data.length; i += 4) {
|
// Invert colors using globalCompositeOperation
|
||||||
data[i] = 255 - data[i]; // Red
|
ctx.globalCompositeOperation = "difference";
|
||||||
data[i + 1] = 255 - data[i + 1]; // Green
|
ctx.fillStyle = "white";
|
||||||
data[i + 2] = 255 - data[i + 2]; // Blue
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||||
}
|
return canvas.toDataURL();
|
||||||
|
|
||||||
ctx.putImageData(imageData, 0, 0);
|
|
||||||
image.src = canvas.toDataURL();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNums(text) {
|
function getNums(text) {
|
||||||
const a = text
|
const a = text.replace(" ", "").replace("=", "").split("+");
|
||||||
.trim()
|
return parseInt(a[0]) + parseInt(a[1]);
|
||||||
.split(" ")
|
|
||||||
.join("")
|
|
||||||
.replace("=", "")
|
|
||||||
.split("+");
|
|
||||||
return [a[0], a[1]];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
invertImageColors(image);
|
const invertedimg = invertColors(image);
|
||||||
|
console.log(`Converting image: ${new Date().getTime() - time} ms.`);
|
||||||
// Step 2: Image Processing with Tesseract.js
|
// Step 2: Image Processing with Tesseract.js
|
||||||
Tesseract.recognize(image.src, "eng", { whitelist: "1234567890+=" })
|
Tesseract.recognize(invertedimg, "eng", {
|
||||||
|
whitelist: "1234567890+=",
|
||||||
|
psm: 7,
|
||||||
|
})
|
||||||
.then(({ data: { text } }) => {
|
.then(({ data: { text } }) => {
|
||||||
// Step 3: Extract the Math Problem
|
console.log(`OCR: ${new Date().getTime() - time} ms.`);
|
||||||
const mathProblem = getNums(text);
|
// Step 3: Solve the Math Problem
|
||||||
|
try {
|
||||||
// Step 4: Solve the Math
|
const result = getNums(text);
|
||||||
if (mathProblem) {
|
|
||||||
const result =
|
|
||||||
parseInt(mathProblem[0]) + parseInt(mathProblem[1]);
|
|
||||||
console.log(
|
console.log(
|
||||||
"Found math captcha:",
|
"Found math captcha. Auto-filling answer: ",
|
||||||
mathProblem,
|
result
|
||||||
"=",
|
|
||||||
result,
|
|
||||||
"Auto-filling answer"
|
|
||||||
);
|
);
|
||||||
textbox.value = result;
|
textbox.value = result;
|
||||||
button.click();
|
button.click();
|
||||||
} else {
|
console.log(`Took ${new Date().getTime() - time} ms.`);
|
||||||
console.log("No math problem found in the image text.");
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue