Skip to content

Instantly share code, notes, and snippets.

@singleghost
Last active May 30, 2016 04:48
Show Gist options
  • Save singleghost/6a6470cf90de9f4649f1def38159defc to your computer and use it in GitHub Desktop.
Save singleghost/6a6470cf90de9f4649f1def38159defc to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name 教务网验证码识别
// @namespace dddong.org.cn
// @description 识别验证码的脚本
// @include http://10.202.78.12/*
// @version 1
// @grant none
// ==/UserScript==
//vertical 5~17
//horizental begin with 5px, every number occupies 8px, interval 1px
function autoFillCaptcha() {
var image = document.querySelector("[src='CheckCode.aspx']"); //获取到验证码图片
if (!image) return;
var canvas = document.createElement('canvas'); //新建一个canvas
var ctx = canvas.getContext("2d"); //获取2D上下文
var numbers = [
"110000111000000100011000001111000011110000111100001111000011110000111100000110001000000111000011", //0
"111100111110001111000011100100111011001111110011111100111111001111110011111100111111001111110011", //1
"110000111000000100011100001111001111110011111001111100011110001111000111100111110000000000000000", //2
"110000011000000000111100111111001110000111100001111110001111110000111100000110001000000111000011", //3
"111110011111000111110001111000011100100111001001100110010011100100000000000000001111100111111001", //4
"100000011000000110011110000111110000001100000001001110001111110000111100000110001000000111000011", //5
"110000011000000010011100001111110010001100000001000110000011110000111100100111001000000111000011", //6
"000000000000000011111001111100111111001111100111111001111110011111000111110011111100111111001111", //7
"110000111000000100111100001111000011110010000001100000010011110000111100001111001000000111000011", //8
]; //存储数字模板的数组
canvas.width = image.width; //设置canvas的宽度
canvas.height = image.height; //设置canvas的高度
document.body.appendChild(canvas); //将canvas添加进文档
ctx.drawImage(image, 0, 0); //将验证码绘制到canvas上
var captcha = "";
for (var i = 0; i < 5; i++) { //循环四次,识别四个数字
var pixels = ctx.getImageData(9 * i + 5, 5, 8, 12).data; //按照公式获取到每个数字上的像素点
var ldString = ""; //用来存储明暗值的字符串
for (var j = 0, length = pixels.length; j < length; j += 4) { //每次循环取四个值,分别是一个像素点的r,g,b,a值
ldString = ldString + (+(pixels[j] * 0.3 + pixels[j + 1] * 0.59 + pixels[j + 2] * 0.11 >= 128)); //灰度化+二值化,但我们并没有真正的处理图像
}
console.log(ldString);
var comms = numbers.map(function(value) { //为了100%识别率,这里不能直接判断是否和模板字符串相等,因为可能有个别0被计算成1,或者相反
return vec_cos(value, ldString);
});
console.log(comms);
captcha += comms.indexOf(Math.max.apply(null, comms)); //添加到识别好的验证码中
console.log("captcha:" + captcha);
}
document.querySelector("#Textbox3").value = captcha;
}
function vec_cos(value, ldString) {
if (value.length != ldString.length) {
console.log("vector length not equal!");
return undefined;
}
var sum = 0;
var modA = 0;
var modB = 0;
for (var i = value.length - 1; i >= 0; i--) {
sum += (value[i]) * ldString[i];
modA += value[i] * value[i];
modB += ldString[i] * ldString[i];
}
return Math.abs(sum / Math.sqrt(modA * modB));
}
function setup() {
document.querySelector("#Textbox3").onfocus = autoFillCaptcha;
}
window.setTimeout(setup(), 60);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment