Amraki

Captcha Solver 2018.10.15

Dec 5th, 2018
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         [KissAnime] Captcha Solver
  3. // @namespace    https://greasyfork.org/en/users/193865-westleym
  4. // @author       WestleyM
  5. // @version      2018.10.15
  6. // @icon         http://kissanime.ru/Content/images/favicon.ico
  7. // @description  Saves initial responses to KissAnime captcha and auto-selects images if it knows the answer.
  8. // @grant        none
  9. // @include      http://kissanime.ru/Special/AreYouHuman2*
  10. // @include      https://kissanime.ru/Special/AreYouHuman2*
  11. // @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  12. // ==/UserScript==
  13.  
  14. function main() {
  15.     //Variable declarations
  16.     var currentVersion = "2018.10.15";
  17.     var installText = "Thank you for installing [KissAnime] Captcha Solver!";
  18.     var updateText = "Line 106-related issues should not be possible any more.  To see how it works now you can read the few lines starting from 108\nFixed duplication of KCS-\nThe best way to reach me is on Reddit @WarriorSolution\nPlease report any bugs or issues!";
  19.     var $ = window.jQuery;
  20.     var formVerify = document.getElementById("formVerify1");
  21.     var words = [], undefinedWords = [], unknownWords = [], knownWords = [],
  22.         imageSrc = [], clickImage = [], imageData = [], imageElements = [], multiImageFlag = [];
  23.     var matchFound = 0, count = 0, impExpFlag = 0, askedForHelp = 0, PHObjFlag = 0;
  24.     var wordImagePairs = {}, wordsObj = {}, imageObj = {}, clickedImgs = {}, placeholderObjOne = {}, placeholderObjTwo = {};
  25.     var dataURL = "";
  26.     var impExpButton, inputSubmit, exportButton,
  27.         firstDiv, PElements, thirdPElement, alertBoxDiv,
  28.         alertBoxText, importExport, inputJSON, lineSeparator,
  29.         exportDirections, exportBox; //Variables used for created HTML elements
  30.  
  31.     if (formVerify === null) {
  32.         var link = document.getElementsByTagName("a");
  33.         link = link[0];
  34.         if (localStorage.getItem("KCS-lastDescriptions") != null) {
  35.             wordsObj = JSON.parse(localStorage.getItem("KCS-lastDescriptions"));
  36.             localStorage.removeItem(wordsObj.firstWord);
  37.             localStorage.removeItem(wordsObj.secondWord);
  38.             localStorage.removeItem("KCS-lastDescriptions");
  39.             console.log("Deleted the last two entries.");
  40.         }
  41.         console.log("Redirecting page. . . .");
  42.         link.click();
  43.     }
  44.  
  45.  
  46.     if (formVerify != null) { //User is on the regular captcha page
  47.         //Alerts for initial install or update of the script
  48.         if (localStorage.getItem("KCS-version") === null && localStorage.getItem("version") === null) { messagePusher("install"); }
  49.         if (localStorage.getItem("KCS-version") != currentVersion && localStorage.getItem("KCS-version") != null) { messagePusher("update"); }
  50.         if (localStorage.getItem("KCS-version") === null && localStorage.getItem("version") != null) { messagePusher("update"); }
  51.  
  52.         //Image onclick events
  53.         imageElements = $("#formVerify1").find("img").toArray();
  54.         imageElements.forEach(function(currentImage, imageIndex) { currentImage.onclick = function() { onClickEvents("image", currentImage, imageIndex); } });
  55.  
  56.         //Create custom HTML
  57.         customHTML();
  58.  
  59.         //Import/Export onclick function calls
  60.         impExpButton.onclick = function() { onClickEvents("impExpButton") };
  61.         inputSubmit.onclick = function() { onClickEvents("inputSubmit") };
  62.         exportButton.onclick = function() { onClickEvents("exportButton") };
  63.  
  64.         //Avoid conflicts, start main processes
  65.         this.$ = this.jQuery = jQuery.noConflict(true);
  66.         $(document).ready(function() {
  67.             wordGrabber();
  68.             unknownWordGrabber();
  69.             knownWordGrabber();
  70.             imageGrabber();
  71.             clickImages();
  72.  
  73.             console.log("Unknown words: " + unknownWords);
  74.             console.log("Known words: " + knownWords);
  75.             if (unknownWords[0] != undefined) { //Ask for help with the first unknown word
  76.                 askForHelp(unknownWords[0]);
  77.             }
  78.         });
  79.     }
  80.  
  81.  
  82.  
  83.     //Functions
  84.     function askForHelp(word) { //Asks you to select an answer when the script doesn't know.
  85.         alertBoxText.innerText = "Please select image: " + word;
  86.         localStorage.setItem("KCS-helpWord", word);
  87.     }
  88.  
  89.     function unknownWordGrabber() { //Finds the words that the script doesn't know the answer to
  90.         words.forEach(function(word) {
  91.             if(!localStorage.getItem("KCS-" + word)) { //If the solution isn't found in the local storage, it will be added to the "unknownWords" array
  92.                 unknownWords.push(word);
  93.             }
  94.         });
  95.     }
  96.  
  97.     function knownWordGrabber() { //Finds the words that the script knows the answer to
  98.         words.forEach(function(word) {
  99.             if(localStorage.getItem("KCS-" + word)) { //If solution is found in the local storage, it will be added to the "knownWords" array
  100.                 knownWords.push(word);
  101.             }
  102.         });
  103.     }
  104.  
  105.     function wordGrabber() { //Grabs span elements that are children of the "formVerify" form.  This will include the two sections saying what to select.  Ex: "cat, glasses, 0"
  106.         var pElements = $("#formVerify1").find("p").toArray();
  107.         var finalPElement, wordElements;
  108.  
  109.         pElements.forEach(function(pElement) { //Grabs the p element that contains 2 span elements in it.
  110.             if ($(pElement).find("span").toArray().length === 2) {
  111.                 wordElements = $(pElement).find("span").toArray();
  112.             }
  113.         });
  114.         words = [wordElements[0].innerText, wordElements[1].innerText];
  115.  
  116.         //Saves the descriptions to local Storage
  117.         var lastDescriptions = { "firstWord":wordElements[0].innerText, "secondWord":wordElements[1].innerText };
  118.         var DescJSON = JSON.stringify(lastDescriptions);
  119.         localStorage.setItem("KCS-lastDescriptions", DescJSON);
  120.     }
  121.  
  122.     function imageGrabber() {
  123.         imageElements.forEach(function(image, index) {
  124.             var objKey = "image" + index.toString();
  125.             var imageData = convertToDataUrl(image);
  126.             imageData = minimiseDataUrl(minimiseDataUrl(minimiseDataUrl(imageData, 5), 4), 3);
  127.             imageObj[objKey] = imageData;
  128.         });
  129.         console.log("original: " );
  130.         console.log(imageObj);
  131.     }
  132.  
  133.     function convertToDataUrl(img) {
  134.         var canvas = document.createElement("canvas");
  135.         canvas.width = img.width;
  136.         canvas.height = img.height;
  137.         var ctx = canvas.getContext("2d");
  138.         ctx.drawImage(img, 0, 0);
  139.         var dataURL = canvas.toDataURL("image/png");
  140.         return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
  141.     }
  142.  
  143.     function minimiseDataUrl(dataUrl,jump) {
  144.         var a = "";
  145.         for(var i = 0; i < dataUrl.length; i=i+jump) {
  146.             a += dataUrl.charAt(i);
  147.         }
  148.         return a;
  149.     }
  150.  
  151.     function clickImages() {
  152.         knownWords.forEach(function(word) {
  153.             var i = 0;
  154.             for (var key in imageObj) {
  155.                 if (localStorage.getItem("KCS-" + word) == imageObj[key]) {
  156.                     $("[indexValue='" + i + "']").click();
  157.                     break;
  158.                 } else if (i === Object.keys(imageObj).length-1) {
  159.                     var multiImageDesc = localStorage.getItem("KCS-" + word);
  160.                     var foundFlag = 0;
  161.                     try {
  162.                         JSON.parse(multiImageDesc);
  163.                         for (var j = 0; j < Object.keys(multiImageDesc).length; j++) {
  164.                             if (multiImageDesc[j] === imageObj[key]) {
  165.                                 console.log("Description with multiple images found and clicked: " + word);
  166.                                 $("[indexValue='" + i + "']").click();
  167.                                 foundFlag = 1;
  168.                             }
  169.                         }
  170.                     }
  171.                     catch(err) {}
  172.                     if (foundFlag === 0) {
  173.                         console.log("Description with multiple images found.  Solution unknown: " + word);
  174.                         multiImageFlag.push(word);
  175.                         unknownWords.push(word);
  176.                         knownWords.splice(knownWords.indexOf(word), 1);
  177.                     }
  178.                 }
  179.                 i++;
  180.             }
  181.         });
  182.     }
  183.  
  184.     function convertSolutions() {
  185.         var tempVarKey = "";
  186.         var tempVarDesc = "";
  187.         for (var i = 0; i < localStorage.length; i++) {
  188.             if (localStorage.key(i) != "KCS-helpWord" && localStorage.key(i) != "KCS-lastDescriptions" && localStorage.key(i) != "KCS-version") {
  189.                 tempVarKey = localStorage.key(i);
  190.                 tempVarKey = tempVarKey.replace(/KCS-/g, "");
  191.                 tempVarDesc = localStorage.getItem(localStorage.key(i));
  192.                 localStorage.removeItem(localStorage.key(i));
  193.                 localStorage.setItem("KCS-" + tempVarKey, tempVarDesc);
  194.             }
  195.         }
  196.     }
  197.  
  198.     function removeBrokenSolutions() {
  199.         for (var i = 0; i < localStorage.length; i++) {
  200.             if (localStorage.getItem(localStorage.key(i))[0] === "[" || localStorage.getItem(localStorage.key(i)) === undefined || localStorage.getItem(localStorage.key(i)) === "undefined") {
  201.                 localStorage.removeItem(localStorage.key(i));
  202.             }
  203.         }
  204.     }
  205.  
  206.     function messagePusher(type) {
  207.         switch(type) {
  208.             case "install":
  209.                 console.log(installText);
  210.                 localStorage.removeItem("version");
  211.                 localStorage.removeItem("lastDescriptions");
  212.                 localStorage.removeItem("helpWord");
  213.                 localStorage.setItem("KCS-version", currentVersion);
  214.                 break;
  215.             case "update":
  216.                 //alert("(You will only see this message once per update)\n\n" + updateText);
  217.                 localStorage.removeItem("version");
  218.                 localStorage.removeItem("lastDescriptions");
  219.                 localStorage.removeItem("helpWord");
  220.                 localStorage.setItem("KCS-version", currentVersion);
  221.                 convertSolutions();
  222.                 removeBrokenSolutions();
  223.                 break;
  224.         }
  225.     }
  226.  
  227.     function customHTML() {
  228.         //Message box
  229.         firstDiv = $("#formVerify1").find("div").toArray()[0];
  230.         firstDiv.style.cssText = "width:100%;"; //The box holding the information at the top was not wide enough originally
  231.  
  232.         PElements = $(firstDiv).find("p").toArray();
  233.         if (PElements.length === 2) {
  234.             PElements[0].style.cssText = "opacity:0; height:0px; width:100%; line-height:0px; font-size:0px;";
  235.         }
  236.         if (PElements.length === 3) {
  237.             PElements[0].style.cssText = "display: none;";
  238.             PElements[1].style.cssText = "opacity:0; height:0px; width:100%; line-height:0px; font-size:0px;";
  239.         }
  240.  
  241.         thirdPElement = PElements[PElements.length-1];
  242.         thirdPElement.style.cssText = "opacity:0; height:0px; width:100%; line-height:0px; font-size:0px;"; //Hides where it lists both selection choices.  This is to insure users select the images in the correct order.
  243.  
  244.         alertBoxDiv = document.createElement("div"); //Creation of div element which will contain the below text element
  245.         alertBoxDiv.style.cssText = "background:#518203; color:white; height:30px; width:100%; line-height:30px; text-align:center;";
  246.  
  247.         alertBoxText = document.createElement("h3"); //Creation of text element which will say the descriptions of images the script doesn't know the answer to
  248.         alertBoxText.innerText = "Checking data. . . .";
  249.         alertBoxText.style.cssText = "background:#518203; color:white; height:100%; width:100%; text-align:center; font-size: 20px; margin-top:0px;";
  250.  
  251.         alertBoxDiv.insertAdjacentElement("afterbegin", alertBoxText); //Inserting "alertBoxText" into "alertBoxDiv" at the top
  252.         thirdPElement.insertAdjacentElement("afterend", alertBoxDiv); //Placing "alertBoxDiv" at the end of "mainBlock"
  253.  
  254.         //Import/Export area
  255.         importExport = document.createElement("div");
  256.         importExport.style.cssText = "display:block; background: #111111; color:white; width:970px; padding:2px; text-align:center; margin-left:auto; margin-right:auto; border:1px solid #2f2f2f;";
  257.         importExport.id = "importExport";
  258.  
  259.         impExpButton = document.createElement("p");
  260.         impExpButton.style.cssText = "background:#518203; color:white; height:15px; width:960px; margin-top:5px; margin-bottom:5px; text-align:center; font-size: 15px; padding:5px; cursor:pointer;";
  261.         impExpButton.innerText = "[+] Solution List Importing/Exporting";
  262.         impExpButton.id = "impExpButton";
  263.  
  264.         inputJSON = document.createElement("input");
  265.         inputJSON.type = "text";
  266.         inputJSON.name = "JSON input";
  267.         inputJSON.id = "inputJSON";
  268.         inputJSON.placeholder = "Paste solution here";
  269.         inputJSON.style.cssText = "display:none; width:50%; margin-left:auto; margin-right:auto; margin-bottom:5px;";
  270.  
  271.         inputSubmit = document.createElement("div");
  272.         inputSubmit.style.cssText = "display:none; background:#518203; color:white; height:20px; width:50%; margin-left:auto; margin-right:auto; margin-bottom:5px; border:1px solid #5a5a5a; cursor:pointer;";
  273.         inputSubmit.innerText = "Submit";
  274.         inputSubmit.id = "inputSubmit";
  275.  
  276.         lineSeparator = document.createElement("div");
  277.         lineSeparator.style.cssText = "display:none; background:#5f5f5f; height:3px; width:100%; margin-left:auto; margin-right:auto; margin-bottom:5px;";
  278.         lineSeparator.id = "lineSeparator";
  279.  
  280.         exportButton = document.createElement("div");
  281.         exportButton.style.cssText = "display:none; background:#518203; color:white; height:20px; width:50%; margin-left:auto; margin-right:auto; margin-bottom:5px; border:1px solid #5a5a5a; cursor:pointer;";
  282.         exportButton.innerText = "Export list";
  283.         exportButton.id = "exportButton";
  284.  
  285.         exportDirections = document.createElement("div");
  286.         exportDirections.style.cssText = "display:none; background:#518203; color:white; height:20px; width:50%; margin-left:auto; margin-right:auto; margin-bottom:5px; border:1px solid #5a5a5a;";
  287.         exportDirections.innerText = "Copy the below data: (triple click to select all)";
  288.         exportDirections.id = "exportDirections";
  289.  
  290.         exportBox = document.createElement("p");
  291.         exportBox.style.cssText = "display:none; #111111; color:white; width:75%; margin-left:auto; margin-right:auto; margin-top:0px; margin-bottom:5px; text-align:center; font-size:10px; border:1px solid #2f2f2f; word-wrap: break-word; overflow:auto; max-height:500px;";
  292.         exportBox.innerText = "";
  293.         exportBox.id = "exportBox";
  294.  
  295.         importExport.insertAdjacentElement("afterbegin", impExpButton);
  296.         importExport.insertAdjacentElement("beforeend", inputSubmit);
  297.         inputSubmit.insertAdjacentElement("afterend", lineSeparator);
  298.         lineSeparator.insertAdjacentElement("afterend", exportButton);
  299.         exportButton.insertAdjacentElement("afterend", exportDirections);
  300.         exportDirections.insertAdjacentElement("afterend", exportBox);
  301.         impExpButton.insertAdjacentElement("afterend", inputJSON);
  302.         document.getElementById("containerRoot").insertAdjacentElement("afterend", importExport);
  303.     }
  304.  
  305.     function onClickEvents(clickedItem, clickedImage, imageIndexValue) {
  306.         switch(clickedItem) {
  307.             case "impExpButton":
  308.                 if (impExpFlag === 0) {
  309.                     impExpButton.innerText = "[-] Solution List Importing/Exporting";
  310.                     inputJSON.style.display = "block";
  311.                     inputSubmit.style.display = "block";
  312.                     lineSeparator.style.display = "block";
  313.                     exportButton.style.display = "block";
  314.                     impExpFlag = 1;
  315.                 } else {
  316.                     impExpButton.innerText = "[+] Solution List Importing/Exporting";
  317.                     inputJSON.style.display = "none";
  318.                     inputSubmit.style.display = "none";
  319.                     lineSeparator.style.display = "none";
  320.                     exportButton.style.display = "none";
  321.                     exportDirections.style.display = "none";
  322.                     exportBox.style.display = "none";
  323.                     impExpFlag = 0;
  324.                 }
  325.                 break;
  326.             case "exportButton":
  327.                 //Grab data from local storage and convert to JSON string
  328.                 for (var i = 0; i < localStorage.length; i++) {
  329.                     if (localStorage.key(i) != "KCS-helpWord" && localStorage.key(i) != "KCS-lastDescriptions" && localStorage.key(i) != "KCS-version") {
  330.                         wordImagePairs[localStorage.key(i)] = localStorage.getItem(localStorage.key(i));
  331.                     }
  332.                 }
  333.                 var wordImagePairsJSON = JSON.stringify(wordImagePairs);
  334.                 exportBox.innerText = wordImagePairsJSON;
  335.                 exportDirections.style.display = "block";
  336.                 exportBox.style.display = "block";
  337.                 break;
  338.             case "inputSubmit":
  339.                 var inputData = inputJSON.value;
  340.                 var currentTemp = "";
  341.                 var oldListLength = localStorage.length.toString();
  342.                 try {
  343.                     var newCaptchaData = JSON.parse(inputData);
  344.                     Object.keys(newCaptchaData).forEach(function(current) {
  345.                         currentTemp = current.replace(/KCS-/g, ""); //Allows for compatibility between old export lists and new ones.
  346.                         localStorage.setItem("KCS-" + currentTemp, newCaptchaData[current]);
  347.                     });
  348.                     inputSubmit.innerText = "Submitted successfully!  Old/new/changed solutions: " + oldListLength + "/" + localStorage.length.toString() + "/" + Object.keys(newCaptchaData).length;
  349.                     console.log("Solution list has been updated.");
  350.                 }
  351.                 catch(err) {
  352.                     inputSubmit.innerText = "There was an issue.  Check the console.";
  353.                     console.log("Issue with list upload: " + err);
  354.                 }
  355.                 removeBrokenSolutions();
  356.                 convertSolutions();
  357.                 break;
  358.             case "image":
  359.                 if ($(clickedImage).attr("class") === "imgCapSelect") {
  360.                     clickedImgs[localStorage.getItem("KCS-helpWord")] = imageIndexValue;
  361.                 } else {
  362.                     words.forEach(function(word) {
  363.                         if (imageIndexValue === clickedImgs[word]) {
  364.                             delete clickedImgs[word];
  365.                         }
  366.                     });
  367.                 }
  368.                 if (Object.keys(clickedImgs).length === words.length) {
  369.                     for (var key in clickedImgs) {
  370.                         if (key !== multiImageFlag[0] && key !== multiImageFlag[1]) {
  371.                             localStorage.setItem("KCS-" + key, imageObj["image" + clickedImgs[key].toString()]);
  372.                         } else {
  373.                             var currentSolution = localStorage.getItem(key);
  374.                             try {
  375.                                 JSON.parse(currentSolution);
  376.                                 currentSolution[Object.keys(currentSolution).length] = imageObj["image" + clickedImgs[key].toString()];
  377.                             }
  378.                             catch(err) {
  379.                                 if (PHObjFlag === 0) {
  380.                                     placeholderObjOne[0] = currentSolution;
  381.                                     placeholderObjOne[1] = imageObj["image" + clickedImgs[key].toString()];
  382.                                     currentSolution = placeholderObjOne;
  383.                                     PHObjFlag = 1;
  384.                                 } else if (PHObjFlag === 1) {
  385.                                     placeholderObjTwo[0] = currentSolution;
  386.                                     placeholderObjTwo[1] = imageObj["image" + clickedImgs[key].toString()];
  387.                                     currentSolution = placeholderObjTwo;
  388.                                     PHObjFlag = 2;
  389.                                 }
  390.                             }
  391.                             JSON.stringify(currentSolution);
  392.                             localStorage.setItem("KCS-" + key, currentSolution);
  393.                         }
  394.                     }
  395.                     alertBoxText.innerText = "Selections complete.  Loading next page. . . .";
  396.                 }
  397.                 if (Object.keys(clickedImgs).length < words.length) {
  398.                     words.forEach(function(word, index) {
  399.                         if (clickedImgs[word] === undefined && askedForHelp === 0) {
  400.                             askForHelp(word);
  401.                             askedForHelp = 1;
  402.                         }
  403.                     });
  404.                     askedForHelp = 0;
  405.                 }
  406.         }
  407.     }
  408. }
  409.  
  410. main();
Add Comment
Please, Sign In to add comment