skillrack-captcha-solver/userscript.js

92 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-08-18 15:47:36 +00:00
//
// Copyright © 2023 adithyagenie
//
// SPDX-License-Identifier: AGPL-3.0-or-later
//
2023-08-18 15:34:25 +00:00
// ==UserScript==
// @name Math Problem Solver
// @namespace https://github.com/adithyagenie/skillrack-captcha-solver
2023-08-20 17:52:37 +00:00
// @version 0.3
2023-08-18 15:34:25 +00:00
// @description Solves math captcha in SkillRack using Tesseract.js
// @author adithyagenie
// @include https://*.skillrack.com/*
// @require https://cdn.jsdelivr.net/npm/tesseract.js@4.1.1/dist/tesseract.min.js
// ==/UserScript==
(function () {
// Wait for window to load
window.addEventListener("load", function () {
console.log("Checking for captchas");
// Step 1: Get the Image
const image = document.getElementById("j_id_6s");
const textbox = document.getElementById("capval");
const button = document.getElementById("proceedbtn");
if (image == null) {
console.log("Captcha not found!");
return;
}
2023-08-20 17:52:37 +00:00
const errors = document.getElementsByClassName("ui-growl-item");
if (errors.length > 0) {
if (errors[0].textContent.includes("Incorrect Captcha")) {
console.log("Detected failed attempt at solving captcha");
alert("I wasn't able to solve the captcha :/");
return;
}
}
2023-08-20 15:41:53 +00:00
const time = new Date().getTime();
2023-08-18 15:34:25 +00:00
// Invert colours for better ocr
2023-08-20 15:41:53 +00:00
function invertColors(image) {
2023-08-18 15:34:25 +00:00
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
2023-08-20 15:41:53 +00:00
// Set canvas dimensions to match the image
canvas.width = image.width;
canvas.height = image.height;
2023-08-18 15:34:25 +00:00
2023-08-20 15:41:53 +00:00
// Draw the image onto the canvas
ctx.drawImage(image, 0, 0);
2023-08-18 15:34:25 +00:00
2023-08-20 15:41:53 +00:00
// Invert colors using globalCompositeOperation
ctx.globalCompositeOperation = "difference";
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
return canvas.toDataURL();
2023-08-18 15:34:25 +00:00
}
function getNums(text) {
2023-08-20 15:41:53 +00:00
const a = text.replace(" ", "").replace("=", "").split("+");
return parseInt(a[0]) + parseInt(a[1]);
2023-08-18 15:34:25 +00:00
}
2023-08-20 15:41:53 +00:00
const invertedimg = invertColors(image);
console.log(`Converting image: ${new Date().getTime() - time} ms.`);
2023-08-18 15:34:25 +00:00
// Step 2: Image Processing with Tesseract.js
2023-08-20 15:41:53 +00:00
Tesseract.recognize(invertedimg, "eng", {
whitelist: "1234567890+=",
psm: 7,
})
2023-08-18 15:34:25 +00:00
.then(({ data: { text } }) => {
2023-08-20 15:41:53 +00:00
console.log(`OCR: ${new Date().getTime() - time} ms.`);
// Step 3: Solve the Math Problem
try {
const result = getNums(text);
2023-08-18 15:34:25 +00:00
console.log(
2023-08-20 15:41:53 +00:00
"Found math captcha. Auto-filling answer: ",
result
2023-08-18 15:34:25 +00:00
);
textbox.value = result;
button.click();
2023-08-20 15:41:53 +00:00
console.log(`Took ${new Date().getTime() - time} ms.`);
} catch (e) {
console.error(e);
2023-08-18 15:34:25 +00:00
}
return;
})
.catch((error) => {
console.error("Error processing captcha:", error);
});
});
})();