Greasy Fork

[KissAnime] Captcha Solver

Saves initial responses to KissAnime captcha and auto-selects images if it knows the answer. Compatible with any number of images in the captcha.

目前为 2018-07-01 提交的版本。查看 最新版本

// ==UserScript==
// @name         [KissAnime] Captcha Solver
// @namespace    https://greasyfork.org/en/users/193865-westleym
// @author       WestleyM
// @version      2018.07.01
// @icon         http://kissanime.ru/Content/images/favicon.ico
// @description  Saves initial responses to KissAnime captcha and auto-selects images if it knows the answer. Compatible with any number of images in the captcha.
// @grant        none
// @include      http://kissanime.ru/Special/AreYouHuman2*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==

/*
Author Notes:
This is a fork of a script made by user ankit16-19.  The original script can be found here: https://greasyfork.org/en/scripts/368595-kissanime-anime-captcha-solver
It should be noted that the initial solving of the captcha will take quite a long time due to the addition of numbers in the images.  My estimate is about 100 episodes.  In the future I may also include the captcha solutions in the code, I still haven't decided since it will take up a large chunk of the file.

If ankit16-19 would like this removed I will delete it.
*/


//Global variables
var words = [];
var undefinedWords = [];
var imageSrc = [];
var clickImage = [];
var count = 0;
var imageElements = $("#formVerify").find("img").toArray();  //All images under the "formVerify" form
var imageNumber = imageElements.length;  //How many images are in the captcha


//onclick events
imageElements.forEach(function(currentImage, imageIndex) {
  currentImage.onclick = function() {
    if (!localStorage.getItem("helpword")){ //Saves the solution to local storage
	  	localStorage.setItem(localStorage.getItem("helpWord"),ProcessImages()[imageIndex])
	    localStorage.removeItem("helpword");
	  }
    
    if (count === 1) {  //Changes text in the alert box
      alertBoxText.innerText = "Selections complete.  Loading next page. . . .";
    }
    
	  if (unknownwords[1] !== undefined && count < 1){ //If there is a word that is not known it will ask for help.  This specific if statement will only activate after you've made your first selection and if the second is unknown.
	    askForHelp(unknownwords[1]);
	    count++;
	  }
  }
});

//Message box creation
var firstDiv = $("#formVerify").find("div").toArray()[0];
firstDiv.style.cssText = "width:100%;";  //The box holding the information at the top was not wide enough originally
var thirdPElement = $(firstDiv).find("p").toArray()[2];
thirdPElement.style.cssText = "opacity:0; height:0px; width:100%;"  //Hides where it lists both selection choices.  This is to insure users select the images in the correct order.

var alertBoxDiv = document.createElement("div");  //Creation of div element which will contain the below text element
alertBoxDiv.style.cssText = "background:#518203; color:white; height:30px; width:100%; line-height:30px; textAlign:center;"

var alertBoxText = document.createElement("h3");  //Creation of text element which will say the descriptions of images the script doesn't know the answer to
alertBoxText.innerText = "Checking data. . . .";
alertBoxText.style.cssText = "background:#518203; color:white; height:100%; width:100%; textAlign:center; font-size: 20px;"

alertBoxDiv.insertAdjacentElement("afterbegin", alertBoxText);  //Inserting "alertBoxText" into "alertBoxDiv" at the top
thirdPElement.insertAdjacentElement("afterend", alertBoxDiv);  //Placing "alertBoxDiv" at the end of "mainBlock"


//Avoid conflicts
this.$ = this.jQuery = jQuery.noConflict(true);
$(document).ready(function()
{
 	unknownwords = UnknownWords();
  knownwords = KnownWords();
  console.log("Unknown words: " + unknownwords);
  console.log("Known words: " + knownwords);
  if(unknownwords[0] !== undefined){  //Ask for help with the first unknown word
  	askForHelp(unknownwords[0]);
  }
  knownwords.forEach(function(word){
    matchfound = 0;
    console.log("processing known word:" + word)
    ProcessImages().forEach(function(image,counter){  //Grabs known value from local storage and clicks the corresponding image
      console.log("counter: " + counter);
      if(localStorage.getItem(word) == image){
        console.log("found match for word " + word);
        matchfound = 1;
        $("[indexValue='"+counter+"']").click();
      }else if(counter === imageNumber-1 && matchfound === 0){
        location.reload();
      }
    })
    
  })  

});

//Functions
function askForHelp(word){  //Asks you to select an answer when the script doesn't know.
  alertBoxText.innerText = "Please select image: " + word;
  localStorage.setItem("helpWord",word);
}

function UnknownWords(){  //Finds the words that the script doesn't know the answer to
  var unknownwords = [];
  Words().forEach(function(word){
     if(!localStorage.getItem(word)){  //If the solution isn't found in the local storage, it will be added to the "unknownwords" array
       	unknownwords.push(word);
     }
  });
  return unknownwords;
}

function KnownWords(){  //Finds the words that the script knows the answer to
  var knownWords = [];
  Words().forEach(function(word){
     if(localStorage.getItem(word)){  //If solution is found in the local storage, it will be added to the "Knownwords" array
       	knownWords.push(word);
     }
  });
  return knownWords;
}

function SaveWord(word, dataurl){
  if(!localStorage.getItem(word)){
   		localStorage.removeItem(word);
    	localStorage.setItem(word, dataurl);
  }else {
   		localStorage.setItem(word,dataurl);
  }
}

function Words(){ //Grabs span elements that are children of the "formVerify" form.  This will include the "SKIP THIS (RapidVideo)" button and the two sections saying what to select.  Ex: "cat, glasses, 0"
    var words = $("#formVerify").find("span").toArray();
    var firstDesc = words[1].innerText; //Start at index 1 to skip the RapidVideo span element
    var secondDesc = words[2].innerText;
    return [firstDesc, secondDesc];
}

function Images(){
  return $("[indexValue]").toArray();
}

function ConvertToDataUrl(img) {
  var canvas = document.createElement("canvas");
  canvas.width = img.width;
  canvas.height = img.height;
  var ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0);
  var dataURL = canvas.toDataURL("image/png");
  return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}

function MinimiseDataUrl(dataUrl,jump)
{
    var a = "";
    for(var i = 0; i < dataUrl.length; i=i+jump)
        a += dataUrl.charAt(i);
    return a;
}

function ProcessImages()
{
  var imagedata = [];
  Images().forEach(function(image, counter)
  {
    dataurl = ConvertToDataUrl(image)
    imagedata.push(MinimiseDataUrl(MinimiseDataUrl(MinimiseDataUrl(dataurl, 5), 4),3));
  })
  return imagedata
}