DeathByCatpcha API Wrapper

API wrapper for using DeathByCaptcha's decaptcha service

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greatest.deepsurf.us/scripts/9125/158918/DeathByCatpcha%20API%20Wrapper.js

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        DeathByCatpcha API Wrapper
// @grant       GM_xmlhttpRequest
// ==/UserScript==


function DeathByCaptcha (dbc_user, dbc_pass) {
  this.user = dbc_user;
  this.pass = dbc_pass;

  this.balance = function balance(callback) {
    var reqUrl = 'http://api.dbcapi.me/api/user';
    var formData = new FormData();
    formData.append('username', this.user);
    formData.append('password', this.pass);

    GM_xmlhttpRequest({
      method: 'POST',
      headers: {
        'Accept': 'application/json'
      },
      data: formData,
      url: reqUrl,
      onload: function (response) {
        if (response.status === 200) {
          var balance = JSON.parse(response.responseText).balance;
          callback(null, balance);
        } else {
          callback(new Error(response.status), null);
        }
      }
    });
  };

  this.report = function report(capId, callback) {
    var reportUrl = 'http://api.dbcapi.me/api/captcha/' + capId + '/report';
    var formData = new FormData();
    formData.append('username', this.user);
    formData.append('password', this.pass);

    GM_xmlhttpRequest({
      method: 'POST',
      data: formData,
      url: reportUrl,
      onload: function (response) {
        if (response.status === 200) {
          console.log('DeathByCaptcha: captcha reported as incorrect');
          callback(null);
        } else {
          callback(new Error(response.status));
        }
      }
    });
  };

  this.solve = function solve(imgNode, callback) {

    //create base64 uri
    var canvas = document.createElement('CANVAS');
    var ctx = canvas.getContext('2d');
    canvas.height = imgNode.height;
    canvas.width = imgNode.width;
    ctx.drawImage(img, 0, 0);
    var imgB64 = canvas.toDataURL().replace(/data:image\/(png|jpg);base64,/, 'base64:');

    //create form to submit to DBC
    var formData = new FormData();
    formData.append('username', this.user);
    formData.append('password', this.pass);
    formData.append('captchafile', imgB64);

    //upload captcha to DBC
    GM_xmlhttpRequest({
      method: 'POST',
      data: formData,
      url: 'http://api.dbcapi.me/api/captcha',
      onload: function (response) {

        //return http errors
        switch (response.status) {
          case 403:
            console.error('DeathByCaptcha: 403 Forbidden - Invalid credentails or insufficient credits');
            callback(new Error(403));
            return;
          case 400:
            console.error('DeathByCaptcha: 300 Bad Request');
            callback(new Error(400));
            return;
          case 500:
            callback(new Error(500));
            console.error('DeathByCaptcha: 500 Internal Server Error');
            return;
          case 503:
            console.error('DeathByCaptcha: 503 Service Temporarily Unavailable');
            callback(new Error(503));
            return;
        }

        console.log('DeathByCaptcha: Uploaded captcha');
        console.log("DeathByCaptcha: URL - " + response.finalUrl);


        //check every 2 seconds if captcha is solved
        var pollTimer = setInterval(function () {
          GM_xmlhttpRequest({
            method: 'GET',
            headers: {
              'Accept': 'application/json'
            },
            url: capUrl,
            onload: function (response) {
              console.log('DeathByCaptcha: Checking for solved captcha.');

              if (response.status === 404) {
                clearInterval(pollTimer);
                console.log('DeathByCaptcha: Captcha ID Not Found.');
                callback(new Error(404));

              } else if (response.status === 200) {


                var solution = JSON.parse(response.responseText).text;
                var capId = JSON.parse(response.responseText).captcha;

                if (solution !== '') {
                  clearInterval(pollTimer);
                  console.log('DeathByCaptcha: Captcha solved');

                  callback(null, solution, capId);
                }
              }
            }
          });
        }, 2000);
      }
    });
  };
}