DeathByCatpcha API Wrapper

API wrapper for using DeathByCaptcha's decaptcha service

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greatest.deepsurf.us/scripts/9125/158918/DeathByCatpcha%20API%20Wrapper.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);
      }
    });
  };
}