//识别验证码
function ocrFormApi(_data) {
var _code = "本地未开启P娃儿猫验证码识别工具的web服务。" ;
try {
console.log( "提交后台识别中..." );
$.ajax({
type: "POST" ,
url: "http://127.0.0.1:2000/" ,
timeout: 2 * 1000,
async: false ,
data: _data,
success: function (data) {
_code = data;
},
error: function (_d) {
_code = "识别错误" ;
}
});
} catch (e) {
}
return _code;
}
//滑块操作(滑块元素ID,要滑动的距离)
function mockVerify(_imgId, _distance) {
var btn = document.querySelector( "#" + _imgId);
var mousedown = document.createEvent( 'MouseEvents' );
var rect = btn.getBoundingClientRect();
var x = rect.x;
var y = rect.y;
mousedown.initMouseEvent( 'mousedown' , true , true , window, 0,
x, y, x, y, false , false , false , false , 0, null );
btn.dispatchEvent(mousedown);
var dx = 0;
var dy = 0;
var interval = setInterval( function () {
var mousemove = document.createEvent( 'MouseEvents' );
var _x = x + dx;
var _y = y + dy;
mousemove.initMouseEvent( 'mousemove' , true , true , window, 0,
_x, _y, _x, _y, false , false , false , false , 0, null );
btn.dispatchEvent(mousemove);
console.log( "MOVE" );
btn.dispatchEvent(mousemove);
if (_x - x >= _distance) {
clearInterval(interval);
var mouseup = document.createEvent( 'MouseEvents' );
mouseup.initMouseEvent( 'mouseup' , true , true , window, 0,
_x, _y, _x, _y, false , false , false , false , 0, null );
btn.dispatchEvent(mouseup);
console.log( "END" );
}
else {
dx += Math.ceil(Math.random() * 50);
}
}, 60);
}
//传入图像元素通过canvas转换
function getImgBase64(_img) {
var canvas = document.createElement( "canvas" );
canvas.width = _img.naturalWidth;
canvas.height = _img.naturalHeight;
var ctx = canvas.getContext( "2d" );
ctx.drawImage(_img, 0, 0);
return canvas.toDataURL( "image/png" ).replace( "data:image/png;base64," , "" );
}
//传入图片地址(不能跨域),通过blob转换(只能异步)
function getImgBase64ByUrl(_url, _success) {
var xhr = new XMLHttpRequest();
xhr.responseType = "blob" ;
xhr.open( "POST" , _url, true );
xhr.onload = function (data, textStatus, request) { // 请求完成处理函数
if ( this .status === 200) {
var _blob = this .response; // 获取返回值
let _f = new FileReader();
_f.onload = function (_e) {
console.log(_e.target.result);
_success(_e.target.result);
}
_f.readAsDataURL(_blob);
} else {
console.log( this .status);
}
};
xhr.send();
}
function checkCodeOut() {
if ($( "#captcha-verify-image:visible" ).length) {
//获取跨域图片数据(把 getImgBase64ByUrl 中的方法放到插件的background中,因为background没有跨域问题)
window.sendMessage({ type: "getDataFromUrl" , data: { url: $( "#captcha-verify-image:visible" ).attr( "src" ), blob: 1, type: "GET" } }, function (_base64) {
var _backgroundImg = _base64.replace( "data:image/jpeg;base64," , "" );
//点选文字
if ($( "#verify-bar-code:visible" ).length) {
window.sendMessage({ type: "getDataFromUrl" , data: { url: $( "#verify-bar-code:visible" ).attr( "src" ), blob: 1, type: "GET" } }, function (_base64) {
var _targetImg = _base64.replace( "data:image/jpeg;base64," , "" );
//识别出要求点选的文字
var _data = ocrFormApi({ type: "ocrCode" , img: _targetImg });
//_data = JSON.parse(_data);
var _txt = _data.code;
//识别背景图上的文字及对应坐标
_data = ocrFormApi({ type: "detection" , img: _backgroundImg });
console.log(_data)
_data = _data.code;
for ( var _i = 0; _i < _txt.length; _i++) {
if (_data[_txt.charAt(_i)]) {
console.log( "“" + _txt.charAt(_i) + "”字点击范围:" , _data[_txt.charAt(_i)])
} else {
console.log( "“" + _txt.charAt(_i) + "”字范围未能识别成功,请刷新。" );
$( ".secsdk_captcha_refresh--text" ).click();
}
}
});
}
//滑块
if ($( ".captcha_verify_img_slide:visible" ).length) {
window.sendMessage({ type: "getDataFromUrl" , data: { url: $( ".captcha_verify_img_slide:visible" ).attr( "src" ), blob: 1, type: "GET" } }, function (_base64) {
var _targetImg = _base64.replace( "data:image/jpeg;base64," , "" );
var _data = ocrFormApi( "slide" , { type: "slide" , targetImg: _targetImg, backgroundImg: _backgroundImg });
console.log( "滑块需要操作的数据:" , _data);
$( ".captcha_verify_img_slide:visible" ).attr( "id" , "52pj_slide" );
mockVerify( "52pj_slide" , _data.x);
});
}
});
return ;
}
setTimeout(checkCodeOut, 1000);
}
function init() {
var _l = location.href;
if (_l.indexOf( "www.douyin.com" ) != -1) {
checkCodeOut();
return ;
}
return ;
}
if ( typeof $ == 'undefined' ) {
var s = document.createElement( "script" );
s.onload = init;
s.src = "https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js" ;
document.getElementsByTagName( "HEAD" )[0].appendChild(s);
} else {
init();
}
|
所有评论(0)