Last active
May 27, 2018 09:30
-
-
Save mittorn/b1e2d6b74f61546adaec078ec9724ed4 to your computer and use it in GitHub Desktop.
2captcha solver for GM (based on https://addons.mozilla.org/en-US/firefox/addon/recaptcha-solver/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name recaptcha | |
// @namespace * | |
// @include * | |
// @version 1 | |
// @grant GM_xmlhttpRequest | |
// @grant GM.xmlHttpRequest | |
// ==/UserScript== | |
// https://addons.mozilla.org/en-US/firefox/addon/recaptcha-solver/ | |
var pageurl = window.location.href; | |
var messageError = "ERROR_WRONG_USER_KEY"; | |
var messageNotReady = "CAPCHA_NOT_READY"; | |
var id = ""; | |
var message = document.createElement('span'); | |
var button = document.createElement('button'); | |
var afterElement = null; | |
function checkCompletion(code, key, repeat) { | |
setupTextarea(); | |
//var http = new XMLHttpRequest(); | |
var url = 'https://2captcha.com/res.php?key='+key+'&action=get&id='+code+'&json=0'; | |
/* http.onreadystatechange = function() { | |
if(http.readyState == 4 && http.status == 200){ | |
var response = http.responseText; | |
if(response == messageNotReady){ | |
setTimeout(function() { | |
checkCompletion(code, key, repeat); | |
}, repeat) | |
}else{ | |
setCaptchaCode(response.substring(3)); | |
} | |
} | |
} | |
http.open("GET", url, true); | |
http.send(null); | |
*/ | |
var options = { | |
method: "GET", | |
url: url, | |
onload: function(res) | |
{ | |
var response = res.responseText; | |
if(response == messageNotReady) | |
{ | |
sendMessage("waiting"); | |
setTimeout(function() { | |
checkCompletion(code, key, repeat); | |
}, repeat); | |
} | |
else | |
{ | |
sendMessage("setting captha code"); | |
setCaptchaCode(response.substring(3)); | |
} | |
} | |
}; | |
try{ | |
GM.xmlHttpRequest(options); | |
} | |
catch(e) | |
{ | |
GM_xmlhttpRequest(options); | |
} | |
} | |
function getParameterByName(name, url) { | |
if (!url) url = window.location.href; | |
name = name.replace(/[\[\]]/g, "\\$&"); | |
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), | |
results = regex.exec(url); | |
if (!results) return null; | |
if (!results[2]) return ''; | |
return decodeURIComponent(results[2].replace(/\+/g, " ")); | |
} | |
function insertAfter(el, referenceNode) { | |
referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling); | |
} | |
function setCaptchaCode(code) { | |
var ele = document.getElementById("g-recaptcha-response"); | |
sendMessage("<b>SOLVED</b>"); | |
// setChecked(); | |
if(ele != null){ | |
// uncomment this to show the textarea | |
ele.style.display = "block"; | |
ele.innerHTML = code; | |
} | |
try{ | |
if(unsafeWindow.___grecaptcha_cfg.clients[0].ba.l.callback) | |
unsafeWindow.___grecaptcha_cfg.clients[0].ba.l.callback(code); | |
} | |
catch(e) | |
{ | |
sendMessage(e); | |
} | |
} | |
function setupTextarea() { | |
var ele = document.getElementById("g-recaptcha-response"); | |
if(ele != null){ | |
ele.style.display = "inline-block"; | |
ele.style.zIndex = "-1"; | |
} | |
} | |
function setupMessageBox() { | |
message.innerHTML = "<b>prepared</b>"; | |
var container = document.createElement('div'); | |
container.className = '2captcha_solver'; | |
container.innerHTML = ''; | |
container.appendChild(message); | |
button.innerHTML = 'solve'; | |
button.onclick = makeRequest; | |
container.appendChild(button); | |
container.style.backgroundColor = "#F9F9F9"; | |
container.style.border = "1px solid #D3D3D3"; | |
container.style.borderTop = "none"; | |
container.style.borderRadius = "0 0 3px 3px"; | |
container.style.padding = "5px"; | |
container.style.boxSizing = "border-box"; | |
container.style.width = "302px"; | |
container.style.margin = "-4px 2px 0 0"; | |
insertAfter(container, afterElement); | |
// afterElement.appendChild(message); | |
} | |
function sendMessage(str){ | |
if( console ) | |
console.log(str); | |
message.innerHTML = str; | |
} | |
function startWatching(code, key) { | |
var initial = 15000, repeat = 5000; | |
setTimeout(function() { | |
try | |
{ | |
checkCompletion(code, key, repeat); | |
} | |
catch(e) | |
{ | |
alert(e); | |
} | |
}, initial); | |
} | |
function makeRequest(result){ | |
setupTextarea(); | |
sendMessage("<b>Solving captcha...</b>"); | |
var key = '<YOUR API KEY>'; | |
var url = 'https://2captcha.com/in.php?key='+key+'&googlekey='+id+'&method=userrecaptcha&soft_id=2151&pageurl='+pageurl; | |
//var http = new XMLHttpRequest(); | |
/*http.onreadystatechange = function() { | |
if(http.readyState == 4 && http.status == 200){ | |
var response = http.responseText; | |
if(response == messageError){ | |
sendMessage("Error incorrect API key provided") | |
}else{ | |
startWatching(response.substring(3), key); | |
} | |
}else if(http.readyState == 4){ | |
sendMessage("Error from 2captcha.com "+ http.status); | |
} | |
} | |
http.open("GET", url, true); | |
http.send(null);*/ | |
var options = { | |
method: "GET", | |
url: url, | |
onload: function(res) | |
{ | |
var response = res.responseText; | |
if(response == messageError){ | |
sendMessage("Error incorrect API key provided"); | |
}else{ | |
startWatching(response.substring(3), key); | |
} | |
} | |
}; | |
try{ | |
GM.xmlHttpRequest(options); | |
} | |
catch(e) | |
{ | |
GM_xmlhttpRequest(options); | |
} | |
return false; | |
} | |
function postProcessPage2() | |
{ | |
// wait till id is visible | |
var checkId = setInterval(function() { | |
var ele = document.getElementsByClassName('g-recaptcha'); | |
if(ele != null && ele[0] != undefined){ | |
id = ele[0].getAttribute('data-sitekey'); | |
afterElement = ele[0].firstElementChild; | |
}else{ | |
var frames = document.getElementsByTagName("iframe"); | |
for(var i = 0; i < frames.length; i++){ | |
var src = frames[i].getAttribute('src'); | |
if(src != null && src.startsWith("https://www.google.com/recaptcha")){ | |
id = getParameterByName("k", src); | |
if(id != "" && id != null){ | |
afterElement = frames[i]; | |
break; | |
} | |
} | |
} | |
} | |
if(id != "" && id != null){ | |
setupMessageBox(); | |
clearInterval(checkId); | |
} | |
}, 1000); | |
}; | |
document.addEventListener('load', postProcessPage2, false); | |
postProcessPage2(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment