-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
157 lines (135 loc) · 4.16 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// const { ocrSpace } = require('ocr-space-api-wrapper');
//common.js import ocrBot.js
// const ocrBot = require("./ocrBot.js");
async function solveCaptcha() {
// document.body.style.backgroundColor = "red";
let input = document.querySelector(".submit-container input");
console.log(input);
let base64code = await getCaptchaBase64(input);
console.log(base64code);
let codeJson = await getCode(base64code);
// await fillCapthaInput(code);
let codeIsValid = /^\d{4}$/.test(codeJson.ParsedResults[0].ParsedText.match(/\d+/g))
if (!codeIsValid) {
//regex only 4 number
//code.ParsedResults[0].ParsedText most be only number
console.log('try again')
// console.log(codeJson)
reNewCaptcha()
solveCaptcha()
return false
};
// console.log(codeJson);
await fillCapthaInput(input, codeJson.ParsedResults[0].ParsedText);
return true;
}
function reNewCaptcha() {
document.querySelector(".captcha-img").click()
}
async function fillCapthaInput(input, captchaCode) {
input.focus();
//simulate typing in the input
for (let i = 0; i < captchaCode.length; i++) {
input.value += captchaCode.charAt(i);
// simulate typing delay
setTimeout(function () {
input.dispatchEvent(new Event("input"));
}, 100);
}
}
async function getCaptchaBase64() {
let img = document.querySelector(".submit-container img");
return new Promise((resolve) => {
img.onload = () => {
resolve(img.src);
};
});
}
async function getCode(captchaBase64) {
return await requestCaptcha(captchaBase64);
}
async function requestCaptcha(captchaBase64) {
var myHeaders = new Headers();
myHeaders.append("apikey", "400b06478488957");
var formdata = new FormData();
formdata.append("language", "eng");
formdata.append("isOverlayRequired", "false");
formdata.append("base64Image", captchaBase64);
formdata.append("iscreatesearchablepdf", "false");
formdata.append("issearchablepdfhidetextlayer", "false");
var requestOptions = {
method: "POST",
headers: myHeaders,
body: formdata,
redirect: "follow",
};
return await fetch("https://api.ocr.space/parse/image", requestOptions)
.then((response) => response.json())
.catch((error) => false);
}
let isCaptchaFrame = () => {
let img = document.querySelector(".submit-container img");
if (img) {
return true;
}
return false;
};
let isTermsCheckBox = () => {
let checkbox = document.querySelector("#agreement");
if (checkbox) {
return true;
}
return false;
};
var currentURL = location.href;
setInterval(function () {
if (location.href != currentURL) {
console.log("url changed");
if (isCaptchaFrame()) {
// clearInterval(captchaInterval);
// solveCaptcha();
document.querySelector('.captcha-img').addEventListener('click', () => {
solveCaptcha();
});
document.querySelector('.captcha-text').addEventListener('click', () => {
solveCaptcha();
});
let captchaImage = document.querySelector(".submit-container img");
observer = new MutationObserver((changes) => {
changes.forEach(change => {
if (change.attributeName.includes('src')) {
// console.dir(captchaImage.src);
solveCaptcha();
}
});
});
observer.observe(captchaImage, { attributes: true });
}
if (isTermsCheckBox()) {
// clearInterval(captchaInterval);
document.querySelector('#agreement').click();
}
currentURL = location.href
}
}, 100);
// let runInterval = () => {
// let captchaInterval = setInterval(() => {
// console.log("checking");
// if (isCaptchaFrame()) {
// clearInterval(captchaInterval);
// solveCaptcha();
// document.querySelector('.captcha-img').addEventListener('click', () => {
// solveCaptcha();
// });
// document.querySelector('.captcha-text').addEventListener('click', () => {
// solveCaptcha();
// });
// }
// if (isTermsCheckBox()) {
// clearInterval(captchaInterval);
// document.querySelector('#agreement').click();
// }
// }, 500);
// };
// runInterval();
// event listener for change src .submit-container img src