The captcha solver made by and for japanese high school girls!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

501 lines
13 KiB

10 months ago
import * as tf from '@tensorflow/tfjs';
import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm';
import modelJSON from './model.json';
import ccl from './ccl';
2 years ago
10 months ago
const charset = [' ', '0', '2', '4', '5', '8', 'A', 'D', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'R', 'S', 'T', 'V', 'W', 'X', 'Y'];
let weightsData;
let model;
2 years ago
10 months ago
tf.enableProdMode();
const wasmToUrl = wasm => {
10 months ago
const blb = new Blob([wasm], { type: 'application/wasm' });
return URL.createObjectURL(blb);
};
const backendloaded = (async () => {
try {
// dead code elimination should occur here
// eslint-disable-next-line camelcase
if (execution_mode === 'userscript' || execution_mode === 'test') {
10 months ago
weightsData = (await import('./model.weights.bin')).default;
const tfwasmthreadedsimd = (await import('./tfjs-backend-wasm-threaded-simd.wasm')).default;
const tfwasmsimd = (await import('./tfjs-backend-wasm-simd.wasm')).default;
const tfwasm = (await import('./tfjs-backend-wasm.wasm')).default;
setWasmPaths({
'tfjs-backend-wasm.wasm': wasmToUrl(tfwasm),
'tfjs-backend-wasm-simd.wasm': wasmToUrl(tfwasmsimd),
'tfjs-backend-wasm-threaded-simd.wasm': wasmToUrl(tfwasmthreadedsimd)
10 months ago
});
} else {
10 months ago
weightsData = await (await fetch(chrome.runtime.getURL('./model.weights.bin'))).text();
const args = {
'tfjs-backend-wasm.wasm': chrome.runtime.getURL('tfjs-backend-wasm.wasm'),
'tfjs-backend-wasm-simd.wasm': chrome.runtime.getURL('tfjs-backend-wasm-simd.wasm'),
'tfjs-backend-wasm-threaded-simd.wasm': chrome.runtime.getURL('tfjs-backend-wasm-threaded-simd.wasm')
10 months ago
};
setWasmPaths(args);
}
10 months ago
const l = await tf.setBackend('wasm');
console.log('tf backend loaded', l);
} catch (err) {
10 months ago
console.log('tf err', err);
}
10 months ago
})();
2 years ago
10 months ago
function toggle(obj, v) {
if (v) obj.style.display = '';
else obj.style.display = 'none';
2 years ago
}
10 months ago
function base64ToArray(base64) {
const binaryString = window.atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
2 years ago
for (let i = 0; i < len; i++) {
10 months ago
bytes[i] = binaryString.charCodeAt(i);
2 years ago
}
10 months ago
return bytes.buffer;
2 years ago
}
const iohander = {
load: function () {
return new Promise((resolve, reject) => {
resolve({
2 years ago
modelTopology: modelJSON.modelTopology,
weightSpecs: modelJSON.weightsManifest[0].weights,
weightData: base64ToArray(weightsData),
2 years ago
format: modelJSON.format,
generatedBy: modelJSON.generatedBy,
convertedBy: modelJSON.convertedBy
10 months ago
});
});
2 years ago
}
10 months ago
};
2 years ago
10 months ago
async function load() {
const uploadJSONInput = document.getElementById('upload-json');
const uploadWeightsInput = document.getElementById('upload-weights-1');
model = await tf.loadLayersModel(iohander);
return model;
2 years ago
}
10 months ago
function black(x) {
return x < 64;
2 years ago
}
// Calculates "disorder" of the image. "Disorder" is the percentage of black pixels that have a
// non-black pixel below them. Minimizing this seems to be good enough metric for solving the slider.
10 months ago
function calculateDisorder(imgdata) {
const a = imgdata.data;
const w = imgdata.width;
const h = imgdata.height;
const pic = [];
const visited = [];
2 years ago
for (let c = 0; c < w * h; c++) {
10 months ago
if (visited[c]) continue;
if (!black(a[c * 4])) continue;
2 years ago
10 months ago
let blackCount = 0;
const items = [];
const toVisit = [c];
2 years ago
while (toVisit.length > 0) {
10 months ago
const cc = toVisit[toVisit.length - 1];
toVisit.splice(toVisit.length - 1, 1);
2 years ago
10 months ago
if (visited[cc]) continue;
visited[cc] = 1;
2 years ago
if (black(a[cc * 4])) {
10 months ago
items.push(cc);
2 years ago
10 months ago
blackCount++;
toVisit.push(cc + 1);
toVisit.push(cc - 1);
toVisit.push(cc + w);
toVisit.push(cc - w);
2 years ago
}
}
if (blackCount >= 24) {
items.forEach(function (x) {
10 months ago
pic[x] = 1;
});
2 years ago
}
}
10 months ago
let res = 0;
let total = 0;
2 years ago
for (let c = 0; c < w * h - w; c++) {
10 months ago
if (pic[c] !== pic[c + w]) res += 1;
if (pic[c]) total += 1;
2 years ago
}
10 months ago
return res / (total === 0 ? 1 : total);
2 years ago
}
// returns ImageData from captcha's background image, foreground image, and offset (ranging from 0 to -50)
10 months ago
function imageFromCanvas(img, bg, off) {
const h = img.height;
const w = img.width;
const th = 80;
const ph = 0;
const pw = 16;
const scale = th / h;
2 years ago
10 months ago
const canvas = document.createElement('canvas');
canvas.height = w * scale + pw * 2;
canvas.width = th;
2 years ago
10 months ago
const ctx = canvas.getContext('2d', { willReadFrequently: true });
2 years ago
10 months ago
ctx.fillStyle = 'rgb(238,238,238)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
2 years ago
10 months ago
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(-scale, scale);
ctx.rotate((90 * Math.PI) / 180);
2 years ago
10 months ago
const adf = 1 / 3;
10 months ago
const draw = function (off) {
2 years ago
if (bg) {
10 months ago
const border = 4;
2 years ago
ctx.drawImage(
bg,
-off + border,
0,
w - border * 2,
h,
-w / 2 + border,
-h / 2,
w - border * 2,
h
10 months ago
);
2 years ago
}
10 months ago
ctx.drawImage(img, -w / 2, -h / 2, w, h);
};
2 years ago
// if off is not specified and background image is present, try to figure out
// the best offset automatically; select the offset that has smallest value of
// calculateDisorder for the resulting image
if (bg && off == null) {
10 months ago
let bestDisorder = 999;
let bestImagedata = null;
let bestOff = -1;
2 years ago
for (let off = 0; off >= -50; off--) {
10 months ago
draw(off);
2 years ago
10 months ago
let imgdata = ctx.getImageData(0, 0, canvas.width, canvas.height);
const disorder = calculateDisorder(imgdata);
2 years ago
if (disorder < bestDisorder) {
10 months ago
bestDisorder = disorder;
draw(off);
imgdata = ctx.getImageData(0, 0, canvas.width, canvas.height);
bestImagedata = imgdata;
bestOff = off;
2 years ago
}
}
// not the best idea to do this here
setTimeout(function () {
10 months ago
const bg = document.getElementById('t-bg');
const slider = document.getElementById('t-slider');
if (!bg || !slider) return;
slider.value = -bestOff * 2;
bg.style.backgroundPositionX = bestOff + 'px';
}, 1);
draw(bestOff);
return bestImagedata;
2 years ago
} else {
10 months ago
draw(off);
return ctx.getImageData(0, 0, canvas.width, canvas.height);
2 years ago
}
}
// for debugging purposes
10 months ago
function imagedataToImage(imagedata) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = imagedata.width;
canvas.height = imagedata.height;
ctx.putImageData(imagedata, 0, 0);
const image = new Image();
image.src = canvas.toDataURL();
return image;
2 years ago
}
10 months ago
async function predict(img, bg, off) {
2 years ago
if (!model) {
10 months ago
model = await load();
2 years ago
}
10 months ago
const image = imageFromCanvas(img, bg, off);
const labels = ccl.connectedComponentLabeling(image.data.map(e => +(e > 128)), image.width, image.height);
const props = ccl.computeBounds(labels, image.width, image.height);
10 months ago
10 months ago
const sortedByArea = Object.entries(props).sort((a, b) => a[1].area - b[1].area);
const eightBiggest = sortedByArea.slice(-8);
const filtered = new Float32Array(80 * 300);
10 months ago
// TODO: maybe centering?
for (const [label, region] of eightBiggest) {
if ((region.maxRow - region.minRow) <= 20) {
10 months ago
continue;
10 months ago
}
for (let y = region.minRow; y < region.maxRow; ++y) {
for (let x = region.minCol; y < region.maxCol; ++x) {
if (labels[y * image.width + x] === label) {
10 months ago
filtered[y * 300 + x] = 1;
10 months ago
}
}
}
}
10 months ago
const tensor = tf.tensor3d(filtered, [80, 300, 1], 'float32');
const prediction = await model.predict(tensor.expandDims(0)).data();
return createSequence(prediction);
2 years ago
}
10 months ago
function createSequence(prediction) {
const csl = charset.length;
const sequence = [];
2 years ago
// for each prediction
2 years ago
for (let pos = 0; pos < prediction.length; pos += csl) {
// look at the probabilities for the 22 token characters
10 months ago
const preds = prediction.slice(pos, pos + csl);
const max = Math.max(...preds);
2 years ago
10 months ago
const seqElem = {};
2 years ago
for (let i = 0; i < csl; i++) {
10 months ago
const p = preds[i] / max; // normalize probability
const c = charset[i + 1];
2 years ago
if (p >= 0.05) { // if it's probable enough
10 months ago
seqElem[c || ''] = p; // save its probability, to give alternative solutions
2 years ago
}
}
10 months ago
sequence.push(seqElem);
2 years ago
}
10 months ago
return sequence;
2 years ago
}
10 months ago
function postprocess(sequence, overrides) {
const csl = charset.length;
let possibilities = [{ sequence: [] }];
2 years ago
sequence.forEach(function (e, i) {
10 months ago
let additions;
2 years ago
if (overrides && overrides[i] !== undefined) {
10 months ago
additions = [{ sym: overrides[i], off: i, conf: 1 }];
2 years ago
} else {
additions = Object.keys(e).map(function (sym) {
10 months ago
return { sym, off: i, conf: e[sym] };
});
2 years ago
}
10 months ago
if (additions.length === 1 && additions[0].sym === '') return;
2 years ago
10 months ago
const oldpos = possibilities;
possibilities = [];
2 years ago
oldpos.forEach(function (possibility) {
additions.forEach(function (a) {
10 months ago
const seq = [...possibility.sequence];
if (a.sym !== '') seq.push([a.sym, a.off, a.conf]);
2 years ago
const obj = {
sequence: seq
10 months ago
};
2 years ago
10 months ago
possibilities.push(obj);
});
});
});
2 years ago
10 months ago
const res = {};
2 years ago
possibilities.forEach(function (p) {
10 months ago
let line = '';
let lastSym;
let lastOff = -1;
let count = 0;
let prob = 0;
2 years ago
p.sequence.forEach(function (e) {
10 months ago
const sym = e[0];
const off = e[1];
const conf = e[2];
2 years ago
if (sym === lastSym && lastOff + 2 >= off) {
10 months ago
return;
2 years ago
}
10 months ago
line += sym;
2 years ago
10 months ago
lastSym = sym;
lastOff = off;
prob += conf;
count++;
});
2 years ago
10 months ago
if (count > 0) prob /= count;
2 years ago
if (prob > res[line] || !res[line]) {
10 months ago
res[line] = prob;
2 years ago
}
10 months ago
});
2 years ago
let keys = Object.keys(res).sort(function (a, b) {
10 months ago
return res[a] < res[b];
});
2 years ago
const keysFitting = keys.filter(function (x) {
10 months ago
return x.length === 5 || x.length === 6;
});
if (keysFitting.length > 0) keys = keysFitting;
2 years ago
return keys.map(function (x) {
10 months ago
return { seq: x, prob: res[x] };
});
2 years ago
}
10 months ago
async function imageFromUri(uri) {
2 years ago
if (uri.startsWith('url("')) {
10 months ago
uri = uri.substr(5, uri.length - 7);
2 years ago
}
// eslint-disable-next-line camelcase
if (execution_mode !== 'test' && !uri.startsWith('data:')) {
10 months ago
return null;
2 years ago
}
10 months ago
const img = new Image();
await new Promise((r) => (img.onload = r), (img.src = uri));
return img;
2 years ago
}
10 months ago
async function predictUri(uri, uribg, bgoff) {
const img = await imageFromUri(uri);
const bg = uribg ? await imageFromUri(uribg) : null;
const off = bgoff ? parseInt(bgoff) : null;
2 years ago
10 months ago
return await predict(img, bg, off);
2 years ago
}
10 months ago
const solveButton = document.createElement('input');
solveButton.id = 't-auto-solve';
solveButton.value = 'Solve';
solveButton.type = 'button';
solveButton.style.fontSize = '11px';
solveButton.style.padding = '0 2px';
solveButton.style.margin = '0px 0px 0px 6px';
solveButton.style.height = '18px';
2 years ago
solveButton.onclick = async function () {
10 months ago
solve(true);
};
2 years ago
10 months ago
const altsDiv = document.createElement('div');
altsDiv.id = 't-auto-options';
altsDiv.style.margin = '0';
altsDiv.style.padding = '0';
2 years ago
10 months ago
let storedPalceholder;
2 years ago
10 months ago
let overrides = {};
2 years ago
10 months ago
function placeAfter(elem, sibling) {
2 years ago
if (elem.parentElement !== sibling.parentElement) {
setTimeout(function () {
10 months ago
sibling.parentElement.insertBefore(elem, sibling.nextElementSibling);
}, 1);
2 years ago
}
}
10 months ago
let previousText = null;
async function solve(force) {
const resp = document.getElementById('t-resp');
if (!resp) return;
2 years ago
10 months ago
const bg = document.getElementById('t-bg');
if (!bg) return;
2 years ago
10 months ago
const fg = document.getElementById('t-fg');
if (!fg) return;
2 years ago
10 months ago
const help = document.getElementById('t-help');
if (!help) return;
2 years ago
10 months ago
await backendloaded;
10 months ago
placeAfter(solveButton, resp);
placeAfter(altsDiv, help);
2 years ago
// palememe
setTimeout(function () {
10 months ago
toggle(solveButton, bg.style.backgroundImage);
}, 1);
2 years ago
10 months ago
const text = fg.style.backgroundImage;
2 years ago
if (!text) {
10 months ago
altsDiv.innerHTML = '';
return;
2 years ago
}
10 months ago
if (text === previousText && !force) return;
previousText = text;
2 years ago
10 months ago
altsDiv.innerHTML = '';
if (!storedPalceholder) storedPalceholder = resp.placeholder;
resp.placeholder = 'solving captcha...';
2 years ago
10 months ago
overrides = {};
2 years ago
const sequence = await predictUri(
text,
bg.style.backgroundImage,
force ? bg.style.backgroundPositionX : null
10 months ago
);
const opts = postprocess(sequence);
resp.placeholder = storedPalceholder;
2 years ago
10 months ago
showOpts(opts);
2 years ago
}
10 months ago
function showOpts(opts) {
const resp = document.getElementById('t-resp');
if (!resp) return;
2 years ago
10 months ago
altsDiv.innerHTML = '';
2 years ago
if (opts.length === 0) {
10 months ago
resp.value = '';
return;
2 years ago
}
10 months ago
resp.value = opts[0].seq;
2 years ago
// for now don't display options since it seems more difficult to pick than type the whole thing
// eslint-disable-next-line no-constant-condition, no-empty
if (opts.length === 1 || true) {
}
}
const observer = new MutationObserver(async function (mutationsList, observer) {
10 months ago
solve(false);
});
window.solve = solve;
2 years ago
observer.observe(document.body, {
attributes: true,
childList: true,
subtree: true
10 months ago
});