AB Links Solver

Solves AbLink images

  1. // ==UserScript==
  2. // @name AB Links Solver
  3. // @namespace ABLinks Solver(Solves Ablinks images)
  4. // @version 3.1
  5. // @description Solves AbLink images
  6. // @author engageub
  7. // @match *://*/*
  8. // @noframes
  9. // @connect https://unpkg.com
  10. // @require https://unpkg.com/opencv.js@1.2.1/opencv.js
  11. // @require https://unpkg.com/jimp@0.5.2/browser/lib/jimp.min.js
  12. // @require https://unpkg.com/tesseract.js@2.1.5/dist/tesseract.min.js
  13. // @grant GM_xmlhttpRequest
  14. // @antifeature referral-link
  15. // ==/UserScript==
  16.  
  17. // This script solves Ablink images with words and having 3 or 4 different options
  18. // Number identification logic for comparing words and numbers will be implemented in the next versions
  19. // Accuracy can be improved by adding more filters for different types of images and fonts
  20. // This script does not have a global matcher, you will need to add the websites in the matcher section manually, till
  21. // all the solutions are implemented
  22. // Your account will be locked for 24 hours, if 3 incorrect solutions are provided consecutively in 10 minutes. (This is the default but depends on website)
  23. // To avoid this add a rotator to change the website whenever an incorrect solution is provided.
  24.  
  25. // TODO: Refactor Code
  26. (function() {
  27. 'use strict';
  28.  
  29. var questions = [];
  30. var questionImages = [];
  31. var questionImage = "";
  32. var questionImageSource = "";
  33. var numericWordArray = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
  34.  
  35. async function waitForImage(imgElement) {
  36. return await new Promise(res => {
  37. if (imgElement.complete) {
  38. return res();
  39. }
  40. imgElement.onload = () => res();
  41. imgElement.onerror = () => res();
  42. });
  43. }
  44.  
  45. async function toDataURL(c){
  46. return await new Promise(function(resolve){
  47. const dataURI = c.toDataURL('image/png');
  48. return resolve(dataURI);
  49. })
  50.  
  51. }
  52.  
  53. async function removeNoiseUsingImageData(imgdata,width,height,threshold){
  54. return await new Promise(function(resolve){
  55. var noiseCount =0;
  56. var noiseRowStart = 0;
  57. for (let column = 0; column < width; column++) {
  58. let count = 0;
  59. for (let row = 0; row < height; row++) {
  60.  
  61. let position = row * width + column;
  62. let pixelAtPosition = imgdata[position];
  63.  
  64. //Remove noise from first row and last row
  65. if(row == 0 || row == height-1){
  66. imgdata[position] = 0xFFFFFFFF;
  67. }
  68.  
  69. if (pixelAtPosition == 0xFF000000){
  70. if(noiseCount == 0){
  71. noiseRowStart = row;
  72. }
  73. noiseCount++;
  74. }else{
  75. //Define the number of consecutive pixels to be considered as noise
  76. if(noiseCount > 0 && noiseCount <= threshold){
  77. //Start from noiseRow till current row and remove noise
  78. while(noiseRowStart < row){
  79. let noisePosition = noiseRowStart * width + column;
  80. imgdata[noisePosition] = 0xFFFFFFFF;
  81. noiseRowStart++;
  82. }
  83. }
  84. noiseCount =0;
  85. }
  86. }
  87. }
  88. return resolve(imgdata);
  89. })
  90.  
  91. }
  92.  
  93. async function imageUsingOCRAntibotQuestion(image) {
  94.  
  95. if (!image || !image.src) {
  96. console.log("No images found");
  97. return;
  98. }
  99.  
  100. var img = new Image();
  101. img.crossOrigin = 'anonymous';
  102. img.src = image.src
  103. await waitForImage(img);
  104. var c = document.createElement("canvas")
  105. c.width = img.width;
  106. c.height = img.height;
  107. var ctx = c.getContext("2d");
  108. await ctx.drawImage(img, 0, 0);
  109.  
  110. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  111. var data = await imageData.data;
  112. // console.log(data);
  113.  
  114. await ctx.putImageData(imageData, 0, 0);
  115.  
  116. let src = await cv.imread(c);
  117. let dst = new cv.Mat();
  118. let ksize = new cv.Size(3, 3);
  119. // You can try more different parameters
  120. await cv.GaussianBlur(src, dst, ksize, 0, 0, cv.BORDER_DEFAULT);
  121.  
  122. await cv.imshow(c, dst);
  123. src.delete();
  124. dst.delete();
  125.  
  126. //console.log( c.toDataURL());
  127. let imageDataURI = await toDataURL(c);
  128. return await (imageUsingOCR(imageDataURI));
  129. }
  130.  
  131. async function imageUsingOCRAntibotLowValues(image) {
  132.  
  133. if (!image || !image.src) {
  134. console.log("No images found");
  135. return;
  136. }
  137.  
  138. var img = new Image();
  139. img.crossOrigin = 'anonymous';
  140. img.src = image.src;
  141. await waitForImage(img);
  142.  
  143. var c = document.createElement("canvas")
  144. c.width = img.width;
  145. c.height = img.height;
  146. var ctx = c.getContext("2d");
  147. await ctx.drawImage(img, 0, 0);
  148. //console.log(await c.toDataURL());
  149. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  150. var data = await imageData.data;
  151.  
  152. //Make the image visible
  153. for (let i = 0; i < data.length; i += 4) {
  154.  
  155. if ((data[i] < 100 || data[i + 1] < 100 || data[i + 2] < 100) && data[i+3]>0) {
  156. data[i] = 0;
  157. data[i + 1] = 0;
  158. data[i + 2] = 0;
  159. } else {
  160. data[i] = 255;
  161. data[i + 1] = 255;
  162. data[i + 2] = 255;
  163. }
  164. data[i + 3] = 255;
  165. }
  166.  
  167. //Remove Noise from Image
  168. var imgdata = await new Uint32Array(data.buffer);
  169.  
  170. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  171.  
  172. await ctx.putImageData(imageData, 0, 0);
  173.  
  174. //console.log( c.toDataURL());
  175. let imageDataURI = await toDataURL(c);
  176. return await (imageUsingOCR(imageDataURI));
  177. }
  178.  
  179. async function imageUsingOCRAntibotHighValues(image) {
  180.  
  181. if (!image || !image.src) {
  182. console.log("No images found");
  183. return;
  184. }
  185.  
  186. var img = new Image();
  187. img.crossOrigin = 'anonymous';
  188. img.src = image.src;
  189. await waitForImage(img);
  190.  
  191. var c = document.createElement("canvas")
  192. c.width = img.width;
  193. c.height = img.height;
  194. var ctx = c.getContext("2d");
  195. await ctx.drawImage(img, 0, 0);
  196. //console.log(await c.toDataURL());
  197. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  198. var data = await imageData.data;
  199.  
  200. //Make the image visible
  201. for (let i = 0; i < data.length; i += 4) {
  202.  
  203. if ((data[i] > 100 || data[i + 1] > 100 || data[i + 2] > 100) && data[i + 3] > 0) {
  204. data[i] = 0;
  205. data[i + 1] = 0;
  206. data[i + 2] = 0;
  207.  
  208. } else {
  209.  
  210. data[i] = 255;
  211. data[i + 1] = 255;
  212. data[i + 2] = 255;
  213. }
  214. data[i + 3] = 255;
  215. }
  216.  
  217. //Remove Noise from Image
  218. var imgdata = await new Uint32Array(data.buffer);
  219.  
  220. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  221.  
  222.  
  223. await ctx.putImageData(imageData, 0, 0);
  224. //console.log( c.toDataURL());
  225. let imageDataURI = await toDataURL(c);
  226. return await (imageUsingOCR(imageDataURI));
  227. }
  228.  
  229. async function splitImageUsingOCRAntibotLowValues(questionImageSource, answerImagesLength) {
  230.  
  231. var img = new Image();
  232. img.crossOrigin = 'anonymous';
  233. img.src = questionImageSource;
  234. await waitForImage(img);
  235.  
  236. var c = document.createElement("canvas")
  237. c.width = img.width;
  238. c.height = img.height;
  239. var ctx = c.getContext("2d");
  240. await ctx.drawImage(img, 0, 0);
  241. //console.log(await c.toDataURL());
  242. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  243. var data = await imageData.data;
  244.  
  245. //Make the image visible
  246. for (let i = 0; i < data.length; i += 4) {
  247. if ((data[i] < 100 || data[i + 1] < 100 || data[i + 2] < 100) && data[i+3]>0) {
  248. data[i] = 0;
  249. data[i + 1] = 0;
  250. data[i + 2] = 0;
  251.  
  252. } else {
  253. data[i] = 255;
  254. data[i + 1] = 255;
  255. data[i + 2] = 255;
  256.  
  257. }
  258. data[i + 3] = 255;
  259. }
  260.  
  261. await ctx.putImageData(imageData, 0, 0);
  262. //console.log(c.toDataURL());
  263. let imageDataURI = await toDataURL(c);
  264.  
  265. if(answerImagesLength == 3){
  266. return await splitImageByThree(imageDataURI);
  267. }
  268.  
  269. return await (splitImage(imageDataURI));
  270.  
  271. }
  272.  
  273. async function splitImageUsingDefaultValues(questionImageSource, answerImagesLength) {
  274.  
  275. var img = new Image();
  276. img.crossOrigin = 'anonymous';
  277. img.src = questionImageSource;
  278. await waitForImage(img);
  279.  
  280. var c = document.createElement("canvas")
  281. c.width = img.width;
  282. c.height = img.height;
  283. var ctx = c.getContext("2d");
  284. await ctx.drawImage(img, 0, 0);
  285. //console.log(await c.toDataURL());
  286. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  287. var data = await imageData.data;
  288.  
  289. //Make the image visible
  290. for (let i = 0; i < data.length; i += 4) {
  291. if (data[i] > 0 && data[i + 1] > 0 && data[i + 2] > 100 && data[i+3]>0) {
  292. data[i] = 0;
  293. data[i + 1] = 0;
  294. data[i + 2] = 0;
  295.  
  296. } else {
  297. data[i] = 255;
  298. data[i + 1] = 255;
  299. data[i + 2] = 255;
  300.  
  301. }
  302. data[i + 3] = 255;
  303. }
  304.  
  305. var imgdata = await new Uint32Array(data.buffer);
  306.  
  307. //Remove Noise from Image
  308. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  309.  
  310. await ctx.putImageData(imageData, 0, 0);
  311. //console.log(c.toDataURL());
  312. let imageDataURI = await toDataURL(c);
  313. if(answerImagesLength == 3){
  314. return await splitImageByThree(imageDataURI);
  315. }
  316.  
  317. return await splitImage(imageDataURI);
  318.  
  319. }
  320.  
  321.  
  322. async function splitImageUsingOCRAntibotHighValues(questionImageSource, answerImagesLength) {
  323.  
  324. var img = new Image();
  325. img.crossOrigin = 'anonymous';
  326. img.src = questionImageSource;
  327. await waitForImage(img);
  328.  
  329. var c = document.createElement("canvas")
  330. c.width = img.width;
  331. c.height = img.height;
  332. var ctx = c.getContext("2d");
  333. await ctx.drawImage(img, 0, 0);
  334.  
  335. //console.log(await c.toDataURL());
  336.  
  337.  
  338. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  339. var data = await imageData.data;
  340.  
  341. //Make the image visible
  342.  
  343. for (let i = 0; i < data.length; i += 4) {
  344.  
  345. if ((data[i] > 100 || data[i + 1] > 100 || data[i + 2] > 100) && data[i + 3] > 0) {
  346. data[i] = 0;
  347. data[i + 1] = 0;
  348. data[i + 2] = 0;
  349.  
  350. } else {
  351.  
  352. data[i] = 255;
  353. data[i + 1] = 255;
  354. data[i + 2] = 255;
  355. }
  356. data[i + 3] = 255;
  357. }
  358.  
  359. var imgdata = await new Uint32Array(data.buffer);
  360.  
  361. //Remove Noise from Image
  362. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  363.  
  364.  
  365. await ctx.putImageData(imageData, 0, 0);
  366.  
  367. let imageDataURI = await toDataURL(c);
  368.  
  369. if(answerImagesLength == 3){
  370. return await splitImageByThree(imageDataURI);
  371. }
  372.  
  373. return await splitImage(imageDataURI);
  374.  
  375. }
  376.  
  377. async function splitImage(imgSource) {
  378.  
  379. var img = new Image();
  380. img.crossOrigin = 'anonymous';
  381. img.src = imgSource
  382. await waitForImage(img);
  383. var c = document.createElement("canvas")
  384. c.width = img.width;
  385. c.height = img.height;
  386. var ctx = c.getContext("2d");
  387. await ctx.drawImage(img, 0, 0);
  388.  
  389. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  390. var data = await imageData.data;
  391. var imgdata = await new Uint32Array(data.buffer);
  392.  
  393. //Scan from left to right
  394. //Get the weight of white spaces
  395. //Ignore first white space and last white space
  396. var sequenceLength = 0;
  397. var prevColumn = 0;
  398. var hashMap = new Map();
  399. var first = 0;
  400. var second = 0;
  401. var third = 0;
  402. var firstMaxColumn = 0;
  403. var secondMaxColumn = 0;
  404. var thirdMaxColumn = 0;
  405.  
  406. //Remove Noise from Image
  407. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  408.  
  409. //await ctx.putImageData(imageData, 0, 0);
  410.  
  411. //console.log(await c.toDataURL());
  412.  
  413.  
  414. for (let column = Math.floor(0.1 * c.width); column < c.width; column++) {
  415. var count = 0;
  416. for (let row = 0; row < c.height; row++) {
  417.  
  418. var position = row * c.width + column;
  419. var pixelAtPosition = imgdata[position];
  420. if (pixelAtPosition == 0xFFFFFFFF) {
  421. count++;
  422. }
  423.  
  424. }
  425.  
  426. //Get the blank spaces based on weight of the column
  427. if (count > Math.floor(0.88 * c.height) && column != 0) {
  428. if (column - prevColumn == 1) {
  429. sequenceLength = sequenceLength + 1;
  430. }
  431. } else {
  432.  
  433. if ((column - sequenceLength != 1) && (column != 0 || sequenceLength != 0 || column != c.width - 1)) {
  434. // If current element is
  435. // greater than first
  436. if (sequenceLength > first) {
  437. third = second;
  438. thirdMaxColumn = secondMaxColumn;
  439. second = first;
  440. secondMaxColumn = firstMaxColumn;
  441. first = sequenceLength;
  442. firstMaxColumn = column - 1;
  443. } else if (sequenceLength > second) {
  444. third = second;
  445. thirdMaxColumn = secondMaxColumn;
  446. second = sequenceLength;
  447. secondMaxColumn = column - 1;
  448. } else if (sequenceLength > third) {
  449. third = sequenceLength;
  450. thirdMaxColumn = column - 1;
  451. }
  452. }
  453.  
  454. sequenceLength = 0;
  455. }
  456.  
  457. prevColumn = column;
  458.  
  459. }
  460.  
  461. firstMaxColumn = firstMaxColumn - Math.floor(first / 2)
  462. secondMaxColumn = secondMaxColumn - Math.floor(second / 2)
  463. thirdMaxColumn = thirdMaxColumn - Math.floor(third / 2)
  464.  
  465. var columnArray = [firstMaxColumn, secondMaxColumn, thirdMaxColumn];
  466. columnArray = await columnArray.sort(function(a, b) {
  467. return a - b;
  468. });
  469.  
  470.  
  471. await ctx.putImageData(imageData, 0, 0);
  472.  
  473.  
  474. let url = await questionImage.src.replace(/^data:image\/\w+;base64,/, "");
  475. let buffer = await new Buffer(url, 'base64');
  476. //Check if overlaps are detected and split the images
  477. var len = [];
  478. len[0] = columnArray[0] - 0;
  479. len[1] = columnArray[1] - columnArray[0];
  480. len[2] = columnArray[2] - columnArray[1];
  481. len[3] = c.width - columnArray[2];
  482.  
  483. for (let i = 0; i < len.length; i++) {
  484. if (len[i] < Math.floor(0.1 * c.width)) {
  485. console.log("Overlap detected");
  486. return;
  487. break;
  488. }
  489. }
  490.  
  491. await new Promise((resolve, reject) => {
  492.  
  493. Jimp.read(buffer).then(async function(data) {
  494. await data.crop(0, 0, columnArray[0], questionImage.height)
  495. .getBase64(Jimp.AUTO, async function(err, src) {
  496. let img = new Image();
  497. img.crossOrigin = 'anonymous';
  498. img.src = src
  499. await waitForImage(img);
  500. questionImages[0] = img;
  501. resolve();
  502. })
  503. });
  504. });
  505.  
  506. await new Promise((resolve, reject) => {
  507. Jimp.read(buffer).then(async function(data) {
  508. await data.crop(columnArray[0], 0, columnArray[1] - columnArray[0], questionImage.height)
  509. .getBase64(Jimp.AUTO, async function(err, src) {
  510. var img = new Image();
  511. img.crossOrigin = 'anonymous';
  512. img.src = src
  513. await waitForImage(img);
  514. questionImages[1] = img;
  515. resolve();
  516.  
  517. })
  518. });
  519. });
  520.  
  521. await new Promise((resolve, reject) => {
  522. Jimp.read(buffer).then(async function(data) {
  523. await data.crop(columnArray[1], 0, columnArray[2] - columnArray[1], questionImage.height)
  524. .getBase64(Jimp.AUTO, async function(err, src) {
  525. var img = new Image();
  526. img.crossOrigin = 'anonymous';
  527. img.src = src
  528. await waitForImage(img);
  529. questionImages[2] = img;
  530. resolve();
  531.  
  532. })
  533. });
  534. });
  535.  
  536. await new Promise((resolve, reject) => {
  537. Jimp.read(buffer).then(async function(data) {
  538. await data.crop(columnArray[2], 0, c.width - columnArray[2], questionImage.height)
  539. .getBase64(Jimp.AUTO, async function(err, src) {
  540. var img = new Image();
  541. img.crossOrigin = 'anonymous';
  542. img.src = src
  543. await waitForImage(img);
  544. questionImages[3] = img;
  545. resolve();
  546. })
  547. });
  548. });
  549. }
  550.  
  551.  
  552. async function splitImageByThree(imgSource) {
  553.  
  554. var img = new Image();
  555. img.crossOrigin = 'anonymous';
  556. img.src = imgSource
  557. await waitForImage(img);
  558. var c = document.createElement("canvas")
  559. c.width = img.width;
  560. c.height = img.height;
  561. var ctx = c.getContext("2d");
  562. await ctx.drawImage(img, 0, 0);
  563.  
  564. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  565. var data = await imageData.data;
  566. var imgdata = await new Uint32Array(data.buffer);
  567.  
  568. //Scan from left to right
  569. //Get the weight of white spaces
  570. //Ignore first white space and last white space
  571. var sequenceLength = 0;
  572. var prevColumn = 0;
  573. var hashMap = new Map();
  574. var first = 0;
  575. var second = 0;
  576. var third = 0;
  577. var firstMaxColumn = 0;
  578. var secondMaxColumn = 0;
  579. var thirdMaxColumn = 0;
  580.  
  581. //Remove Noise from Image
  582. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  583.  
  584. //await ctx.putImageData(imageData, 0, 0);
  585.  
  586. //console.log(await c.toDataURL());
  587.  
  588.  
  589. for (let column = Math.floor(0.1 * c.width); column < c.width; column++) {
  590. var count = 0;
  591. for (let row = 0; row < c.height; row++) {
  592.  
  593. var position = row * c.width + column;
  594. var pixelAtPosition = imgdata[position];
  595. if (pixelAtPosition == 0xFFFFFFFF) {
  596. count++;
  597. }
  598.  
  599. }
  600.  
  601. //Get the blank spaces based on weight of the column
  602. if (count > Math.floor(0.88 * c.height) && column != 0) {
  603. if (column - prevColumn == 1) {
  604. sequenceLength = sequenceLength + 1;
  605. }
  606. } else {
  607.  
  608. if ((column - sequenceLength != 1) && (column != 0 || sequenceLength != 0 || column != c.width - 1)) {
  609. // If current element is
  610. // greater than first
  611. if (sequenceLength > first) {
  612. second = first;
  613. secondMaxColumn = firstMaxColumn;
  614. first = sequenceLength;
  615. firstMaxColumn = column - 1;
  616. } else if (sequenceLength > second) {
  617. second = sequenceLength;
  618. secondMaxColumn = column - 1;
  619. }
  620. }
  621.  
  622. sequenceLength = 0;
  623. }
  624.  
  625. prevColumn = column;
  626.  
  627. }
  628.  
  629. firstMaxColumn = firstMaxColumn - Math.floor(first / 2)
  630. secondMaxColumn = secondMaxColumn - Math.floor(second / 2)
  631.  
  632. var columnArray = [firstMaxColumn, secondMaxColumn];
  633. columnArray = await columnArray.sort(function(a, b) {
  634. return a - b;
  635. });
  636.  
  637.  
  638. await ctx.putImageData(imageData, 0, 0);
  639.  
  640.  
  641. let url = await questionImage.src.replace(/^data:image\/\w+;base64,/, "");
  642. let buffer = await new Buffer(url, 'base64');
  643. //Check if overlaps are detected and split the images
  644. var len = [];
  645. len[0] = columnArray[0] - 0;
  646. len[1] = columnArray[1] - columnArray[0];
  647. len[2] = c.width - columnArray[1];
  648.  
  649. for (let i = 0; i < len.length; i++) {
  650. if (len[i] < Math.floor(0.1 * c.width)) {
  651. console.log("Overlap detected");
  652. return;
  653. break;
  654. }
  655. }
  656.  
  657. await new Promise((resolve, reject) => {
  658.  
  659. Jimp.read(buffer).then(async function(data) {
  660. await data.crop(0, 0, columnArray[0], questionImage.height)
  661. .getBase64(Jimp.AUTO, async function(err, src) {
  662. let img = new Image();
  663. img.crossOrigin = 'anonymous';
  664. img.src = src
  665. await waitForImage(img);
  666. questionImages[0] = img;
  667. resolve();
  668. })
  669. });
  670. });
  671.  
  672. await new Promise((resolve, reject) => {
  673. Jimp.read(buffer).then(async function(data) {
  674. await data.crop(columnArray[0], 0, columnArray[1] - columnArray[0], questionImage.height)
  675. .getBase64(Jimp.AUTO, async function(err, src) {
  676. var img = new Image();
  677. img.crossOrigin = 'anonymous';
  678. img.src = src
  679. await waitForImage(img);
  680. questionImages[1] = img;
  681. resolve();
  682.  
  683. })
  684. });
  685. });
  686.  
  687. await new Promise((resolve, reject) => {
  688. Jimp.read(buffer).then(async function(data) {
  689. await data.crop(columnArray[1], 0, c.width - columnArray[1], questionImage.height)
  690. .getBase64(Jimp.AUTO, async function(err, src) {
  691. var img = new Image();
  692. img.crossOrigin = 'anonymous';
  693. img.src = src
  694. await waitForImage(img);
  695. questionImages[2] = img;
  696. resolve();
  697. })
  698. });
  699. });
  700. }
  701.  
  702.  
  703. async function imageUsingOCRAntibotQuestion1(image) {
  704.  
  705. if (!image || !image.src) {
  706. console.log("No images found");
  707. return;
  708. }
  709.  
  710. var img = new Image();
  711. img.crossOrigin = 'anonymous';
  712. img.src = image.src
  713. await waitForImage(img);
  714. var c = document.createElement("canvas")
  715. c.width = image.width;
  716. c.height = image.height;
  717. var ctx = c.getContext("2d");
  718. // ctx.filter = 'grayscale(1)';
  719. await ctx.drawImage(img, 0, 0);
  720.  
  721. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  722. var data = await imageData.data;
  723. // console.log(data);
  724.  
  725. await ctx.putImageData(imageData, 0, 0);
  726.  
  727.  
  728. let src = await cv.imread(c);
  729.  
  730. let dst = new cv.Mat();
  731. await cv.medianBlur(src, dst, 3)
  732.  
  733. await cv.imshow(c, dst);
  734.  
  735. src.delete();
  736. dst.delete();
  737.  
  738. //console.log( c.toDataURL());
  739. let imageDataURI = await toDataURL(c);
  740.  
  741. return await (imageUsingOCR(imageDataURI));
  742. }
  743.  
  744.  
  745.  
  746. async function imageUsingOCRAntibot1(image) {
  747. var img1 = image;
  748.  
  749. var img = new Image();
  750. img.crossOrigin = 'anonymous';
  751. img.src = img1.src
  752. await waitForImage(img);
  753.  
  754. var c = document.createElement("canvas")
  755. c.width = img1.width;
  756. c.height = img1.height;
  757. var ctx = c.getContext("2d");
  758.  
  759. await ctx.drawImage(img, 0, 0);
  760.  
  761.  
  762. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  763. var data = await imageData.data;
  764.  
  765.  
  766. var hashMap = new Map();
  767.  
  768. for (let i = 0; i < data.length; i += 4) {
  769.  
  770. var rgba = data[i] + ',' + data[i + 1] + ',' + data[i + 2] + ',' + data[i + 3];
  771.  
  772. if (hashMap.has(rgba)) {
  773. hashMap.set(rgba, hashMap.get(rgba) + 1)
  774. } else {
  775. hashMap.set(rgba, 1)
  776. }
  777.  
  778. }
  779.  
  780. var data_tmp = [];
  781. var data_tmp_edges = [];
  782.  
  783. for (let i = 0; i < data.length; i += 4) {
  784.  
  785. if (data[i + 3] > 130 && data[i] < 100 && data[i + 1] < 100 && data[i + 2] < 100) {
  786. data[i] = 0;
  787. data[i + 1] = 0;
  788. data[i + 2] = 0;
  789. data[i + 3] = 255;
  790. data_tmp_edges[i] = 1;
  791. data_tmp_edges[i + 1] = 1;
  792. data_tmp_edges[i + 2] = 1;
  793.  
  794. } else {
  795. data[i] = 255;
  796. data[i + 1] = 255;
  797. data[i + 2] = 255;
  798. data[i + 3] = 255;
  799.  
  800. }
  801. }
  802.  
  803. await ctx.putImageData(imageData, 0, 0);
  804.  
  805. let imageDataURI = await toDataURL(c);
  806.  
  807. return await (imageUsingOCR(imageDataURI));
  808.  
  809. }
  810.  
  811.  
  812. async function imageUsingOCRAntibotFiltered(image) {
  813.  
  814. var img = new Image();
  815. img.crossOrigin = 'anonymous';
  816. img.src = image.src
  817. await waitForImage(img);
  818.  
  819. let mat = cv.imread(img);
  820.  
  821. var c = document.createElement("canvas")
  822. c.width = image.width;
  823. c.height = image.height;
  824. var ctx = c.getContext("2d");
  825. await ctx.drawImage(img, 0, 0);
  826. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  827. var data = await imageData.data;
  828. // console.log(data);
  829.  
  830. for (let i = 0; i < data.length; i += 4) {
  831. if (data[i + 3] > 130 && data[i] < 100) {
  832. data[i] = 255;
  833. data[i + 1] = 255;
  834. data[i + 2] = 255;
  835. data[i + 3] = 255;
  836. } else {
  837. data[i] = 0;
  838. data[i + 1] = 0;
  839. data[i + 2] = 0;
  840. data[i + 3] = 255;
  841. }
  842.  
  843. }
  844.  
  845.  
  846. await ctx.putImageData(imageData, 0, 0);
  847.  
  848.  
  849. let src = await cv.imread(c);
  850.  
  851. let dst = new cv.Mat();
  852.  
  853. let M = cv.Mat.ones(2, 1, cv.CV_8U);
  854. let anchor = new cv.Point(-1, -1);
  855.  
  856. // Opening , remove small particles from image
  857. await cv.morphologyEx(src, dst, cv.MORPH_OPEN, M, anchor, 1,
  858. cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
  859. await cv.imshow(c, dst);
  860.  
  861. //Image erode, thinning the text
  862.  
  863. src = await cv.imread(c);
  864. M = cv.Mat.ones(2, 1, cv.CV_8U);
  865. await cv.erode(src, dst, M, anchor, 1, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
  866. await cv.imshow(c, dst);
  867.  
  868. src.delete();
  869. dst.delete();
  870. M.delete();
  871.  
  872. // console.log( c.toDataURL());
  873.  
  874. let imageDataURI = await toDataURL(c);
  875. return await (imageUsingOCR(imageDataURI));
  876.  
  877. }
  878.  
  879. async function imageUsingOCRAntibotFiltered1(image) {
  880.  
  881. var img = new Image();
  882. img.crossOrigin = 'anonymous';
  883. img.src = image.src
  884. await waitForImage(img);
  885.  
  886. let mat = cv.imread(img);
  887.  
  888. var c = document.createElement("canvas")
  889. c.width = image.width;
  890. c.height = image.height;
  891. var ctx = c.getContext("2d");
  892. await ctx.drawImage(img, 0, 0);
  893. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  894. var data = await imageData.data;
  895. // console.log(data);
  896.  
  897. for (let i = 0; i < data.length; i += 4) {
  898. if (data[i + 3] > 130 && data[i] > 70) {
  899. data[i] = 255;
  900. data[i + 1] = 255;
  901. data[i + 2] = 255;
  902. data[i + 3] = 255;
  903. } else {
  904. data[i] = 0;
  905. data[i + 1] = 0;
  906. data[i + 2] = 0;
  907. data[i + 3] = 255;
  908. }
  909.  
  910. }
  911.  
  912. await ctx.putImageData(imageData, 0, 0);
  913.  
  914. let src = await cv.imread(c);
  915. let dst = new cv.Mat();
  916. let M = cv.Mat.ones(2, 1, cv.CV_8U);
  917. let anchor = new cv.Point(-1, -1);
  918.  
  919. // Opening morphology, remove noise from image
  920. await cv.morphologyEx(src, dst, cv.MORPH_OPEN, M, anchor, 1,
  921. cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
  922. await cv.imshow(c, dst);
  923. //console.log( c.toDataURL());
  924.  
  925. //Image erode
  926. src = await cv.imread(c);
  927. M = cv.Mat.ones(2, 1, cv.CV_8U);
  928. await cv.erode(src, dst, M, anchor, 1, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
  929. await cv.imshow(c, dst);
  930. src.delete();
  931. dst.delete();
  932. M.delete();
  933.  
  934. // console.log( c.toDataURL());
  935. let imageDataURI = await toDataURL(c);
  936.  
  937. return await (imageUsingOCR(imageDataURI));
  938.  
  939. }
  940.  
  941. async function imageUsingOCRAntibot(image) {
  942.  
  943. var img = new Image();
  944. img.crossOrigin = 'anonymous';
  945. img.src = image.src
  946. await waitForImage(img);
  947. var c = document.createElement("canvas")
  948. c.width = image.width;
  949. c.height = image.height;
  950. var ctx = c.getContext("2d");
  951. // ctx.filter = 'grayscale(1)';
  952. await ctx.drawImage(img, 0, 0);
  953.  
  954. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  955. var data = await imageData.data;
  956.  
  957. var hashMap = new Map();
  958.  
  959. for (let i = 0; i < data.length; i += 4) {
  960.  
  961. var rgba = data[i] + ',' + data[i + 1] + ',' + data[i + 2] + ',' + data[i + 3];
  962.  
  963. if (hashMap.has(rgba)) {
  964. hashMap.set(rgba, hashMap.get(rgba) + 1)
  965. } else {
  966. hashMap.set(rgba, 1)
  967. }
  968.  
  969. }
  970.  
  971. var maxCount = 0;
  972. var objectKey = "0,0,0,0";
  973. await hashMap.forEach((value, key) => {
  974. if (maxCount < value && key != "0,0,0,0") {
  975. objectKey = key;
  976. maxCount = value;
  977. }
  978.  
  979. });
  980.  
  981. var alphaValues = objectKey.split(",");
  982. var alpha = Number(alphaValues[alphaValues.length - 1]);
  983.  
  984. var data_tmp = [];
  985. var data_tmp_edges = [];
  986.  
  987. for (let i = 0; i < data.length; i += 4) {
  988.  
  989. if (data[i + 3] == alpha) {
  990. data[i] = 255;
  991. data[i + 1] = 255;
  992. data[i + 2] = 255;
  993. data[i + 3] = 255;
  994. //Giving some value for representation
  995. data_tmp[i] = 1;
  996. data_tmp[i + 1] = 1;
  997. data_tmp[i + 2] = 1;
  998.  
  999.  
  1000. } else if (data[i + 3] > 0) {
  1001. data[i] = 0;
  1002. data[i + 1] = 0;
  1003. data[i + 2] = 0;
  1004. data[i + 3] = 255;
  1005. data_tmp_edges[i] = 1;
  1006. data_tmp_edges[i + 1] = 1;
  1007. data_tmp_edges[i + 2] = 1;
  1008.  
  1009. } else {
  1010. data[i] = 255;
  1011. data[i + 1] = 255;
  1012. data[i + 2] = 255;
  1013. data[i + 3] = 255;
  1014.  
  1015. }
  1016. }
  1017.  
  1018.  
  1019. //Fill if the adjacent value was present earlier
  1020. for (let k = 0; k < 20; k++) {
  1021. for (let i = 4; i < data.length; i += 4) {
  1022.  
  1023. if (data[i] == 0 && data_tmp[i - 4] == 1) {
  1024. data[i - 4] = 0;
  1025. data[i - 3] = 0;
  1026. data[i - 2] = 0;
  1027. data[i - 1] = 255;
  1028. }
  1029. }
  1030. }
  1031.  
  1032. //console.log(imageData.data);
  1033.  
  1034. await ctx.putImageData(imageData, 0, 0);
  1035.  
  1036. // console.log( c.toDataURL());
  1037. let imageDataURI = await toDataURL(c);
  1038.  
  1039. return await (imageUsingOCR(imageDataURI));
  1040.  
  1041.  
  1042. }
  1043.  
  1044. var worker = "";
  1045.  
  1046. async function imageUsingOCR(img) {
  1047. var answer = "";
  1048.  
  1049. if (!worker) {
  1050. worker = await new Tesseract.createWorker();
  1051. }
  1052.  
  1053. if(!img || img.width ==0 || img.height == 0){
  1054. console.log("OCR cannot be performed on this image");
  1055. return "";
  1056. }
  1057.  
  1058. try {
  1059.  
  1060. await worker.load();
  1061. await worker.loadLanguage('eng');
  1062. await worker.initialize('eng');
  1063. await worker.setParameters({
  1064. tessedit_pageseg_mode: '6',
  1065. preserve_interword_spaces: '1',
  1066. tessedit_char_whitelist: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,@!*+',
  1067. //tessedit_ocr_engine_mode:'1'
  1068. });
  1069.  
  1070. await worker.recognize(img, "eng").then(async function(result) {
  1071. answer = result.data.text.trim();
  1072. console.log("Captcha Answer::" + answer);
  1073. });
  1074.  
  1075. // await worker.terminate();
  1076. } catch (err) {
  1077. console.log(err.message);
  1078. await worker.terminate();
  1079.  
  1080. }
  1081.  
  1082. return answer;
  1083.  
  1084. }
  1085.  
  1086.  
  1087. // Compare similar strings
  1088. var LevenshteinDistance = function(a, b) {
  1089. if (a.length == 0) return b.length;
  1090. if (b.length == 0) return a.length;
  1091.  
  1092. var matrix = [];
  1093.  
  1094. // increment along the first column of each row
  1095. var i;
  1096. for (i = 0; i <= b.length; i++) {
  1097. matrix[i] = [i];
  1098. }
  1099.  
  1100. // increment each column in the first row
  1101. var j;
  1102. for (j = 0; j <= a.length; j++) {
  1103. matrix[0][j] = j;
  1104. }
  1105.  
  1106. // Fill in the rest of the matrix
  1107. for (i = 1; i <= b.length; i++) {
  1108. for (j = 1; j <= a.length; j++) {
  1109. if (b.charAt(i - 1) == a.charAt(j - 1)) {
  1110. matrix[i][j] = matrix[i - 1][j - 1];
  1111. } else {
  1112. matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
  1113. Math.min(matrix[i][j - 1] + 1, // insertion
  1114. matrix[i - 1][j] + 1)); // deletion
  1115. }
  1116. }
  1117. }
  1118.  
  1119. return matrix[b.length][a.length];
  1120. };
  1121.  
  1122.  
  1123. function countPairs(s1, s2) {
  1124. var n1 = s1.length;
  1125. var n2 = s2.length;
  1126.  
  1127. // To store the frequencies of
  1128. // characters of string s1 and s2
  1129. let freq1 = new Array(26);
  1130. let freq2 = new Array(26);
  1131. freq1.fill(0);
  1132. freq2.fill(0);
  1133.  
  1134. // To store the count of valid pairs
  1135. let i, count = 0;
  1136.  
  1137. // Update the frequencies of
  1138. // the characters of string s1
  1139. for (i = 0; i < n1; i++)
  1140. freq1[s1[i].charCodeAt() - 'a'.charCodeAt()]++;
  1141.  
  1142. // Update the frequencies of
  1143. // the characters of string s2
  1144. for (i = 0; i < n2; i++)
  1145. freq2[s2[i].charCodeAt() - 'a'.charCodeAt()]++;
  1146.  
  1147. // Find the count of valid pairs
  1148. for (i = 0; i < 26; i++)
  1149. count += (Math.min(freq1[i], freq2[i]));
  1150.  
  1151. return count;
  1152. }
  1153.  
  1154. async function getFinalOCRResultFromImage(image,leastLength){
  1155. var ocrResult = "";
  1156. var tempResult = "";
  1157. ocrResult = await imageUsingOCRAntibotLowValues(image);
  1158.  
  1159. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  1160. tempResult = ocrResult.trim();
  1161. } else {
  1162. ocrResult = await imageUsingOCRAntibotHighValues(image);
  1163. }
  1164.  
  1165. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  1166. tempResult = ocrResult.trim();
  1167. } else {
  1168. ocrResult = await imageUsingOCR(image);
  1169. }
  1170.  
  1171.  
  1172. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  1173. tempResult = ocrResult.trim();
  1174. } else {
  1175. ocrResult = await imageUsingOCRAntibotQuestion(image);
  1176. }
  1177.  
  1178. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  1179. tempResult = ocrResult.trim();
  1180. } else {
  1181. ocrResult = await imageUsingOCRAntibotQuestion1(image);
  1182. }
  1183.  
  1184.  
  1185. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  1186. tempResult = ocrResult.trim()
  1187. } else {
  1188. ocrResult = await imageUsingOCRAntibot(image)
  1189. }
  1190.  
  1191.  
  1192. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  1193. tempResult = ocrResult.trim()
  1194. } else {
  1195. ocrResult = await imageUsingOCRAntibot1(image);
  1196. }
  1197.  
  1198. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  1199. tempResult = ocrResult.trim()
  1200. } else {
  1201. ocrResult = await imageUsingOCRAntibotFiltered(image)
  1202. }
  1203.  
  1204. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  1205. tempResult = ocrResult.trim()
  1206. } else {
  1207. ocrResult = await imageUsingOCRAntibotFiltered1(image)
  1208. }
  1209.  
  1210. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  1211. tempResult = ocrResult.trim()
  1212. }
  1213.  
  1214. ocrResult = tempResult;
  1215.  
  1216. return ocrResult;
  1217.  
  1218.  
  1219. }
  1220.  
  1221. //Adding referral links to faucetpay list
  1222. if (window.location.href.includes("faucetpay.io/page/faucet-list") && document.querySelectorAll(".btn.btn-primary.btn-sm").length > 0) {
  1223. for (let i = 0; i < document.querySelectorAll(".btn.btn-primary.btn-sm").length; i++) {
  1224. document.querySelectorAll(".btn.btn-primary.btn-sm")[i].href =
  1225. document.querySelectorAll(".btn.btn-primary.btn-sm")[i].href.replace(/\/$/, "") + "/?r=1HeD2a11n8d9zBTaznNWfVxtw1dKuW2vT5";
  1226. }
  1227. }
  1228.  
  1229.  
  1230. if(window.location.href.includes("gr8.cc")){
  1231. var oldFunction = unsafeWindow.open;
  1232. unsafeWindow.open= function(url){url = url.split("?r=")[0] + "?r=1HeD2a11n8d9zBTaznNWfVxtw1dKuW2vT5"; return oldFunction(url)}
  1233. for(let i=0; i< document.querySelectorAll("a").length;i++){
  1234. document.querySelectorAll("a")[i].removeAttribute("onmousedown");
  1235. document.querySelectorAll("a")[i].href= document.querySelectorAll("a")[i].href.split("?r=")[0] + "?r=1HeD2a11n8d9zBTaznNWfVxtw1dKuW2vT5";
  1236. }
  1237. }
  1238.  
  1239.  
  1240.  
  1241. setTimeout(async function() {
  1242.  
  1243. var answerSelector = "";
  1244. var questionSelector = "";
  1245. var addCount = 0;
  1246. var leastLength = 0;
  1247. var maxImages = 0;
  1248.  
  1249. if (document.querySelectorAll(".modal-content [href='/'] img").length == 4 && document.querySelectorAll(".modal-content img").length >= 5) {
  1250. questionSelector = ".modal-content img";
  1251. answerSelector = ".modal-content [href='/'] img";
  1252. } else if (document.querySelector(".modal-header img") && document.querySelectorAll(".modal-body [href='/'] img").length == 4) {
  1253. questionSelector = ".modal-header img";
  1254. answerSelector = ".modal-body [href='/'] img";
  1255. } else if (document.querySelector(".alert.alert-info img") && document.querySelectorAll(".antibotlinks [href='/'] img").length == 4) {
  1256. questionSelector = ".alert.alert-info img";
  1257. answerSelector = ".antibotlinks [href='/'] img";
  1258. } else if (document.querySelector(".alert.alert-warning img") && document.querySelectorAll(".antibotlinks [href='/'] img").length == 3) {
  1259. questionSelector = ".alert.alert-warning img";
  1260. answerSelector = ".antibotlinks [href='/'] img";
  1261. } else if (document.querySelector(".alert.alert-warning img") && document.querySelectorAll(".antibotlinks [href='#'] img").length == 3) {
  1262. questionSelector = ".alert.alert-warning img";
  1263. answerSelector = ".antibotlinks [href='#'] img";
  1264. } else if ( document.querySelector(".sm\\:flex.items-center img") && document.querySelectorAll("[href='javascript:void(0)'] img").length == 3) {
  1265. questionSelector = ".sm\\:flex.items-center img";
  1266. answerSelector = "[href='javascript:void(0)'] img";
  1267. } else if (document.querySelectorAll(".modal-content [href='/'] img").length == 3 && document.querySelectorAll(".modal-content img").length >= 4) {
  1268. questionSelector = ".modal-content img";
  1269. answerSelector = ".modal-content [href='/'] img";
  1270. } else if (document.querySelector(".modal-header img") && document.querySelectorAll(".modal-body [href='/'] img").length == 3) {
  1271. questionSelector = ".modal-header img";
  1272. answerSelector = ".modal-body [href='/'] img";
  1273. } else if (document.querySelector(".alert.alert-info img") && document.querySelectorAll(".antibotlinks [href='/'] img").length == 3) {
  1274. questionSelector = ".alert.alert-info img";
  1275. answerSelector = ".antibotlinks [href='/'] img";
  1276. } else {
  1277. console.log("Ab links not detected");
  1278. return;
  1279. }
  1280.  
  1281. var answerImagesLength = document.querySelectorAll(answerSelector).length;
  1282.  
  1283. for (let i = 0; i < answerImagesLength; i++) {
  1284. if (document.querySelector(answerSelector).width <= document.querySelector(answerSelector).height) {
  1285. document.querySelector(answerSelector).value = "####"; //Using this as reference to move to next url
  1286. console.log("Numeric/Roman captcha Detected , captcha cannot be solved at the moment");
  1287. console.log("Reload the page to see if the captcha changes");
  1288. // solveNumberCaptchaByAnswer()
  1289. return;
  1290. }
  1291. }
  1292.  
  1293. if (document.querySelector(questionSelector).width < (answerImagesLength+1) * document.querySelector(questionSelector).height) {
  1294. document.querySelector(answerSelector).value = "####"; //Using this as reference to move to next url
  1295. console.log("Numeric/Roman captcha Detected , captcha cannot be solved at the moment");
  1296. console.log("Reload the page to see if the captcha changes");
  1297. // solveNumberCaptchaByQuestion()
  1298. return;
  1299. }
  1300.  
  1301. if (document.querySelector(questionSelector).width < 10 * document.querySelector(questionSelector).height) {
  1302. leastLength = 2;
  1303. } else {
  1304. leastLength = 3;
  1305. }
  1306.  
  1307. console.log("Solving Ab Links....");
  1308.  
  1309. if (!document.querySelector(questionSelector) || !document.querySelector(questionSelector).src) {
  1310. document.querySelector(answerSelector).value = "####"; //Using this as reference to move to next url
  1311. console.log("No image source found for question");
  1312. return
  1313. }
  1314.  
  1315. questionImage = document.querySelector(questionSelector);
  1316. questionImageSource = document.querySelector(questionSelector).src;
  1317. await waitForImage(questionImage);
  1318. var optionImages = [];
  1319.  
  1320. for (let i = 0; i < answerImagesLength; i++) {
  1321. optionImages[i] = document.querySelectorAll(answerSelector)[i + addCount];
  1322. }
  1323.  
  1324. var questionSolution = await imageUsingOCRAntibotLowValues(questionImage);
  1325. questionSolution = questionSolution.replace(/,$/, "");
  1326.  
  1327. if (!questionSolution || !questionSolution.includes(",") || questionSolution.split(",").length != answerImagesLength) {
  1328. questionSolution = await imageUsingOCRAntibotHighValues(questionImage);
  1329. questionSolution = questionSolution.replace(/,$/, "");
  1330. }
  1331.  
  1332. if (!questionSolution || !questionSolution.includes(",") || questionSolution.split(",").length != answerImagesLength) {
  1333. questionSolution = await imageUsingOCR(questionImage);
  1334. questionSolution = questionSolution.replace(/,$/, "");
  1335. }
  1336.  
  1337. if (!questionSolution || !questionSolution.includes(",") || questionSolution.split(",").length != answerImagesLength) {
  1338. questionSolution = await imageUsingOCRAntibotQuestion(questionImage);
  1339. questionSolution = questionSolution.replace(/,$/, "");
  1340. }
  1341.  
  1342.  
  1343. if (!questionSolution || !questionSolution.includes(",") || questionSolution.split(",").length != answerImagesLength) {
  1344.  
  1345. await splitImageUsingDefaultValues(questionImageSource, answerImagesLength);
  1346.  
  1347. if(questionImages.length < answerImagesLength){
  1348. questionImages = [];
  1349. await splitImageUsingOCRAntibotLowValues(questionImageSource, answerImagesLength);
  1350. }
  1351.  
  1352. if(questionImages.length < answerImagesLength){
  1353. questionImages = [];
  1354. await splitImageUsingOCRAntibotHighValues(questionImageSource, answerImagesLength);
  1355. }
  1356.  
  1357. if(questionImages.length < answerImagesLength){
  1358. document.querySelector(answerSelector).value = "####"; //Using this as reference to move to next url
  1359. console.log("Captcha cannot be solved");
  1360. return;
  1361. }
  1362.  
  1363. for (let i = 0; i < answerImagesLength; i++) {
  1364.  
  1365. questions[i] = await getFinalOCRResultFromImage(questionImages[i],leastLength);
  1366. questions[i] = questions[i].replaceAll("5", "s").replaceAll("3", "e").replaceAll(",", "")
  1367. .replaceAll("8", "b").replaceAll("1", "l").replaceAll("@", "a").replaceAll("*", "").replaceAll("9", "g")
  1368. .replaceAll("!", "i").replaceAll("0", "o").replaceAll("4", "a").replaceAll("2", "z").toLowerCase();
  1369.  
  1370. }
  1371. } else {
  1372. questionSolution = questionSolution.toLowerCase();
  1373. questionSolution = questionSolution.replaceAll("5", "s").replaceAll("3", "e")
  1374. .replaceAll("8", "b").replaceAll("1", "l").replaceAll("@", "a").replaceAll("*", "").replaceAll("9", "g")
  1375. .replaceAll("!", "i").replaceAll("0", "o").replaceAll("4", "a").replaceAll("2", "z").toLowerCase();
  1376. questions = questionSolution.split(',');
  1377. }
  1378.  
  1379. leastLength = 1000;
  1380. for (let i = 0; i < answerImagesLength; i++) {
  1381. if (questions[i].length < leastLength) {
  1382. leastLength = questions[i].length;
  1383. }
  1384. }
  1385.  
  1386. leastLength = leastLength - 1;
  1387.  
  1388. var answers = [];
  1389.  
  1390. for (let i = 0; i < answerImagesLength; i++) {
  1391. var answer = "";
  1392. answers[i] = await getFinalOCRResultFromImage(optionImages[i],leastLength);
  1393. answers[i] = answers[i].replaceAll("5", "s").replaceAll("3", "e")
  1394. .replaceAll("8", "b").replaceAll("1", "l").replaceAll("@", "a").replaceAll("9", "g")
  1395. .replaceAll("!", "i").replaceAll("0", "o").replaceAll("4", "a").replaceAll("2", "z").toLowerCase();
  1396.  
  1397. }
  1398.  
  1399. await worker.terminate();
  1400.  
  1401. if (questions.length == answerImagesLength) {
  1402.  
  1403. var map = new Map();
  1404. for (let i = 0; i < answerImagesLength; i++) {
  1405. questions[i] = questions[i].replaceAll(",", "").replaceAll(" ", "").trim();
  1406. for (let j = 0; j < answerImagesLength; j++) {
  1407. let score = "";
  1408. answers[j] = answers[j].replaceAll(",", "").replaceAll(" ", "").trim();
  1409. score = await LevenshteinDistance(questions[i], answers[j]);
  1410. map.set(questions[i] + "::" + answers[j], score);
  1411. }
  1412. }
  1413.  
  1414. map[Symbol.iterator] = function*() {
  1415. yield*[...this.entries()].sort((a, b) => a[1] - b[1]);
  1416. }
  1417.  
  1418. var tempMap = new Map();
  1419. var finalMap = new Map();
  1420. var preValue = "";
  1421. var count = 0;
  1422. for (let [key, value] of map) {
  1423. count = count + 1;
  1424. //Sort by same score
  1425. if (!preValue) {
  1426. preValue = value;
  1427. tempMap.set(key, value)
  1428. continue;
  1429. }
  1430.  
  1431. if (preValue == value) {
  1432. tempMap.set(key, value);
  1433. } else {
  1434. //The new score is different, sort all the temp values
  1435. tempMap[Symbol.iterator] = function*() {
  1436. yield*[...this.entries()].sort((a, b) => a[0] - b[0]);
  1437. }
  1438.  
  1439. finalMap = new Map([...finalMap, ...tempMap]);
  1440. tempMap = new Map();
  1441. tempMap.set(key, value)
  1442. preValue = value;
  1443. }
  1444.  
  1445. if (count == map.size) {
  1446. tempMap.set(key, value);
  1447. tempMap[Symbol.iterator] = function*() {
  1448. yield*[...this.entries()].sort((a, b) => a[0] - b[0]);
  1449. }
  1450.  
  1451. finalMap = new Map([...finalMap, ...tempMap]);
  1452. }
  1453.  
  1454. }
  1455.  
  1456. var questionAnswerMap = new Map();
  1457. var answerSet = new Set();
  1458. var prevKey = "";
  1459. map = finalMap;
  1460. for (let [key, value] of map) {
  1461. if (!prevKey) {
  1462. prevKey = key
  1463. continue;
  1464. }
  1465. //Check if scores are equal and assign the value
  1466. if (map.get(prevKey) == map.get(key) && prevKey.split("::")[0] == key.split("::")[0] && !answerSet.has(prevKey.split("::")[1]) &&
  1467. !answerSet.has(key.split("::")[1]) && !questionAnswerMap.has(prevKey.split("::")[0]) && !questionAnswerMap.has(key.split("::")[0])) {
  1468. var prevCount = countPairs(prevKey.split("::")[1], prevKey.split("::")[0]);
  1469. var currCount = countPairs(key.split("::")[1], key.split("::")[0]);
  1470.  
  1471. if (prevCount > currCount) {
  1472. key = prevKey;
  1473. } else {
  1474. prevKey = key;
  1475. }
  1476. } else {
  1477. if (!questionAnswerMap.has(prevKey.split("::")[0]) && !answerSet.has(prevKey.split("::")[1])) {
  1478. questionAnswerMap.set(prevKey.split("::")[0], prevKey.split("::")[1]);
  1479. answerSet.add(prevKey.split("::")[1]);
  1480. }
  1481. prevKey = key;
  1482. }
  1483. }
  1484.  
  1485. if (questionAnswerMap.size == answerImagesLength-1 && !questionAnswerMap.has(prevKey.split("::")[0]) && !answerSet.has(prevKey.split("::")[1])) {
  1486. questionAnswerMap.set(prevKey.split("::")[0], prevKey.split("::")[1]);
  1487. answerSet.add(prevKey.split("::")[1]);
  1488. }
  1489.  
  1490. var answersMap = new Map();
  1491.  
  1492. for (let i = 0; i < answerImagesLength; i++) {
  1493. answersMap.set(answers[i], i);
  1494. }
  1495.  
  1496. //Selecting the Answers
  1497. for (let i = 0; i < answerImagesLength; i++) {
  1498. var ans = questionAnswerMap.get(questions[i]);
  1499. let j = answersMap.get(ans);
  1500. console.log("Answer for " + questions[i] + "::" + answers[j]);
  1501. if (document.querySelectorAll(answerSelector)[j + addCount]) {
  1502. document.querySelectorAll(answerSelector)[j + addCount].click();
  1503. } else {
  1504. console.log("Answer Selector could not be identified");
  1505. }
  1506. }
  1507.  
  1508. }
  1509.  
  1510. }, 10000)
  1511.  
  1512. })();