AB Links Solver


SUBMITTED BY: ccont2246

DATE: Dec. 22, 2022, 12:43 p.m.

FORMAT: Text only

SIZE: 46.0 kB

HITS: 39264

  1. // ==UserScript==
  2. // @name AB Links Solver
  3. // @namespace ABLinks Solver(Solves Ablinks images)
  4. // @version 3.0
  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. // This script solves Ablink images with words and having 4 different options
  17. // Number identification logic for comparing words and numbers will be implemented in the next versions
  18. // Accuracy can be improved by adding more filters for different types of images and fonts
  19. // This script does not have a global matcher, you will need to add the websites in the matcher section manually, till
  20. // all the solutions are implemented
  21. // 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)
  22. // To avoid this add a rotator to change the website whenever an incorrect solution is provided.
  23. // TODO: Refactor Code
  24. (function() {
  25. 'use strict';
  26. var questions = [];
  27. var questionImages = [];
  28. var questionImage = "";
  29. var questionImageSource = "";
  30. var numericWordArray = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
  31. async function waitForImage(imgElement) {
  32. return await new Promise(res => {
  33. if (imgElement.complete) {
  34. return res();
  35. }
  36. imgElement.onload = () => res();
  37. imgElement.onerror = () => res();
  38. });
  39. }
  40. async function toDataURL(c){
  41. return await new Promise(function(resolve){
  42. const dataURI = c.toDataURL('image/png');
  43. return resolve(dataURI);
  44. })
  45. }
  46. async function removeNoiseUsingImageData(imgdata,width,height,threshold){
  47. return await new Promise(function(resolve){
  48. var noiseCount =0;
  49. var noiseRowStart = 0;
  50. for (let column = 0; column < width; column++) {
  51. let count = 0;
  52. for (let row = 0; row < height; row++) {
  53. let position = row * width + column;
  54. let pixelAtPosition = imgdata[position];
  55. //Remove noise from first row and last row
  56. if(row == 0 || row == height-1){
  57. imgdata[position] = 0xFFFFFFFF;
  58. }
  59. if (pixelAtPosition == 0xFF000000){
  60. if(noiseCount == 0){
  61. noiseRowStart = row;
  62. }
  63. noiseCount++;
  64. }else{
  65. //Define the number of consecutive pixels to be considered as noise
  66. if(noiseCount > 0 && noiseCount <= threshold){
  67. //Start from noiseRow till current row and remove noise
  68. while(noiseRowStart < row){
  69. let noisePosition = noiseRowStart * width + column;
  70. imgdata[noisePosition] = 0xFFFFFFFF;
  71. noiseRowStart++;
  72. }
  73. }
  74. noiseCount =0;
  75. }
  76. }
  77. }
  78. return resolve(imgdata);
  79. })
  80. }
  81. async function imageUsingOCRAntibotQuestion(image) {
  82. if (!image || !image.src) {
  83. console.log("No images found");
  84. return;
  85. }
  86. var img = new Image();
  87. img.crossOrigin = 'anonymous';
  88. img.src = image.src
  89. await waitForImage(img);
  90. var c = document.createElement("canvas")
  91. c.width = img.width;
  92. c.height = img.height;
  93. var ctx = c.getContext("2d");
  94. await ctx.drawImage(img, 0, 0);
  95. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  96. var data = await imageData.data;
  97. // console.log(data);
  98. await ctx.putImageData(imageData, 0, 0);
  99. let src = await cv.imread(c);
  100. let dst = new cv.Mat();
  101. let ksize = new cv.Size(3, 3);
  102. // You can try more different parameters
  103. await cv.GaussianBlur(src, dst, ksize, 0, 0, cv.BORDER_DEFAULT);
  104. await cv.imshow(c, dst);
  105. src.delete();
  106. dst.delete();
  107. //console.log( c.toDataURL());
  108. let imageDataURI = await toDataURL(c);
  109. return await (imageUsingOCR(imageDataURI));
  110. }
  111. async function imageUsingOCRAntibotLowValues(image) {
  112. if (!image || !image.src) {
  113. console.log("No images found");
  114. return;
  115. }
  116. var img = new Image();
  117. img.crossOrigin = 'anonymous';
  118. img.src = image.src;
  119. await waitForImage(img);
  120. var c = document.createElement("canvas")
  121. c.width = img.width;
  122. c.height = img.height;
  123. var ctx = c.getContext("2d");
  124. await ctx.drawImage(img, 0, 0);
  125. //console.log(await c.toDataURL());
  126. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  127. var data = await imageData.data;
  128. //Make the image visible
  129. for (let i = 0; i < data.length; i += 4) {
  130. if ((data[i] < 100 || data[i + 1] < 100 || data[i + 2] < 100) && data[i+3]>0) {
  131. data[i] = 0;
  132. data[i + 1] = 0;
  133. data[i + 2] = 0;
  134. } else {
  135. data[i] = 255;
  136. data[i + 1] = 255;
  137. data[i + 2] = 255;
  138. }
  139. data[i + 3] = 255;
  140. }
  141. //Remove Noise from Image
  142. var imgdata = await new Uint32Array(data.buffer);
  143. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  144. await ctx.putImageData(imageData, 0, 0);
  145. //console.log( c.toDataURL());
  146. let imageDataURI = await toDataURL(c);
  147. return await (imageUsingOCR(imageDataURI));
  148. }
  149. async function imageUsingOCRAntibotHighValues(image) {
  150. if (!image || !image.src) {
  151. console.log("No images found");
  152. return;
  153. }
  154. var img = new Image();
  155. img.crossOrigin = 'anonymous';
  156. img.src = image.src;
  157. await waitForImage(img);
  158. var c = document.createElement("canvas")
  159. c.width = img.width;
  160. c.height = img.height;
  161. var ctx = c.getContext("2d");
  162. await ctx.drawImage(img, 0, 0);
  163. //console.log(await c.toDataURL());
  164. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  165. var data = await imageData.data;
  166. //Make the image visible
  167. for (let i = 0; i < data.length; i += 4) {
  168. if ((data[i] > 100 || data[i + 1] > 100 || data[i + 2] > 100) && data[i + 3] > 0) {
  169. data[i] = 0;
  170. data[i + 1] = 0;
  171. data[i + 2] = 0;
  172. } else {
  173. data[i] = 255;
  174. data[i + 1] = 255;
  175. data[i + 2] = 255;
  176. }
  177. data[i + 3] = 255;
  178. }
  179. //Remove Noise from Image
  180. var imgdata = await new Uint32Array(data.buffer);
  181. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  182. await ctx.putImageData(imageData, 0, 0);
  183. //console.log( c.toDataURL());
  184. let imageDataURI = await toDataURL(c);
  185. return await (imageUsingOCR(imageDataURI));
  186. }
  187. async function splitImageUsingOCRAntibotLowValues(questionImageSource) {
  188. var img = new Image();
  189. img.crossOrigin = 'anonymous';
  190. img.src = questionImageSource;
  191. await waitForImage(img);
  192. var c = document.createElement("canvas")
  193. c.width = img.width;
  194. c.height = img.height;
  195. var ctx = c.getContext("2d");
  196. await ctx.drawImage(img, 0, 0);
  197. //console.log(await c.toDataURL());
  198. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  199. var data = await imageData.data;
  200. //Make the image visible
  201. for (let i = 0; i < data.length; i += 4) {
  202. if ((data[i] < 100 || data[i + 1] < 100 || data[i + 2] < 100) && data[i+3]>0) {
  203. data[i] = 0;
  204. data[i + 1] = 0;
  205. data[i + 2] = 0;
  206. } else {
  207. data[i] = 255;
  208. data[i + 1] = 255;
  209. data[i + 2] = 255;
  210. }
  211. data[i + 3] = 255;
  212. }
  213. await ctx.putImageData(imageData, 0, 0);
  214. //console.log(c.toDataURL());
  215. let imageDataURI = await toDataURL(c);
  216. return await (splitImage(imageDataURI));
  217. }
  218. async function splitImageUsingDefaultValues(questionImageSource) {
  219. var img = new Image();
  220. img.crossOrigin = 'anonymous';
  221. img.src = questionImageSource;
  222. await waitForImage(img);
  223. var c = document.createElement("canvas")
  224. c.width = img.width;
  225. c.height = img.height;
  226. var ctx = c.getContext("2d");
  227. await ctx.drawImage(img, 0, 0);
  228. //console.log(await c.toDataURL());
  229. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  230. var data = await imageData.data;
  231. //Make the image visible
  232. for (let i = 0; i < data.length; i += 4) {
  233. if (data[i] > 0 && data[i + 1] > 0 && data[i + 2] > 100 && data[i+3]>0) {
  234. data[i] = 0;
  235. data[i + 1] = 0;
  236. data[i + 2] = 0;
  237. } else {
  238. data[i] = 255;
  239. data[i + 1] = 255;
  240. data[i + 2] = 255;
  241. }
  242. data[i + 3] = 255;
  243. }
  244. var imgdata = await new Uint32Array(data.buffer);
  245. //Remove Noise from Image
  246. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  247. await ctx.putImageData(imageData, 0, 0);
  248. //console.log(c.toDataURL());
  249. let imageDataURI = await toDataURL(c);
  250. return await splitImage(imageDataURI);
  251. }
  252. async function splitImageUsingOCRAntibotHighValues(questionImageSource) {
  253. var img = new Image();
  254. img.crossOrigin = 'anonymous';
  255. img.src = questionImageSource;
  256. await waitForImage(img);
  257. var c = document.createElement("canvas")
  258. c.width = img.width;
  259. c.height = img.height;
  260. var ctx = c.getContext("2d");
  261. await ctx.drawImage(img, 0, 0);
  262. //console.log(await c.toDataURL());
  263. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  264. var data = await imageData.data;
  265. //Make the image visible
  266. for (let i = 0; i < data.length; i += 4) {
  267. if ((data[i] > 100 || data[i + 1] > 100 || data[i + 2] > 100) && data[i + 3] > 0) {
  268. data[i] = 0;
  269. data[i + 1] = 0;
  270. data[i + 2] = 0;
  271. } else {
  272. data[i] = 255;
  273. data[i + 1] = 255;
  274. data[i + 2] = 255;
  275. }
  276. data[i + 3] = 255;
  277. }
  278. var imgdata = await new Uint32Array(data.buffer);
  279. //Remove Noise from Image
  280. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  281. await ctx.putImageData(imageData, 0, 0);
  282. let imageDataURI = await toDataURL(c);
  283. return await splitImage(imageDataURI);
  284. }
  285. async function splitImage(imgSource) {
  286. var img = new Image();
  287. img.crossOrigin = 'anonymous';
  288. img.src = imgSource
  289. await waitForImage(img);
  290. var c = document.createElement("canvas")
  291. c.width = img.width;
  292. c.height = img.height;
  293. var ctx = c.getContext("2d");
  294. await ctx.drawImage(img, 0, 0);
  295. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  296. var data = await imageData.data;
  297. var imgdata = await new Uint32Array(data.buffer);
  298. //Scan from left to right
  299. //Get the weight of white spaces
  300. //Ignore first white space and last white space
  301. var sequenceLength = 0;
  302. var prevColumn = 0;
  303. var hashMap = new Map();
  304. var first = 0;
  305. var second = 0;
  306. var third = 0;
  307. var firstMaxColumn = 0;
  308. var secondMaxColumn = 0;
  309. var thirdMaxColumn = 0;
  310. //Remove Noise from Image
  311. imgdata = await removeNoiseUsingImageData(imgdata,c.width,c.height,1);
  312. //await ctx.putImageData(imageData, 0, 0);
  313. //console.log(await c.toDataURL());
  314. for (let column = Math.floor(0.1 * c.width); column < c.width; column++) {
  315. var count = 0;
  316. for (let row = 0; row < c.height; row++) {
  317. var position = row * c.width + column;
  318. var pixelAtPosition = imgdata[position];
  319. if (pixelAtPosition == 0xFFFFFFFF) {
  320. count++;
  321. }
  322. }
  323. //Get the blank spaces based on weight of the column
  324. if (count > Math.floor(0.88 * c.height) && column != 0) {
  325. if (column - prevColumn == 1) {
  326. sequenceLength = sequenceLength + 1;
  327. }
  328. } else {
  329. if ((column - sequenceLength != 1) && (column != 0 || sequenceLength != 0 || column != c.width - 1)) {
  330. // If current element is
  331. // greater than first
  332. if (sequenceLength > first) {
  333. third = second;
  334. thirdMaxColumn = secondMaxColumn;
  335. second = first;
  336. secondMaxColumn = firstMaxColumn;
  337. first = sequenceLength;
  338. firstMaxColumn = column - 1;
  339. } else if (sequenceLength > second) {
  340. third = second;
  341. thirdMaxColumn = secondMaxColumn;
  342. second = sequenceLength;
  343. secondMaxColumn = column - 1;
  344. } else if (sequenceLength > third) {
  345. third = sequenceLength;
  346. thirdMaxColumn = column - 1;
  347. }
  348. }
  349. sequenceLength = 0;
  350. }
  351. prevColumn = column;
  352. }
  353. firstMaxColumn = firstMaxColumn - Math.floor(first / 2)
  354. secondMaxColumn = secondMaxColumn - Math.floor(second / 2)
  355. thirdMaxColumn = thirdMaxColumn - Math.floor(third / 2)
  356. var columnArray = [firstMaxColumn, secondMaxColumn, thirdMaxColumn];
  357. columnArray = await columnArray.sort(function(a, b) {
  358. return a - b;
  359. });
  360. await ctx.putImageData(imageData, 0, 0);
  361. let url = await questionImage.src.replace(/^data:image\/\w+;base64,/, "");
  362. let buffer = await new Buffer(url, 'base64');
  363. //Check if overlaps are detected and split the images
  364. var len = [];
  365. len[0] = columnArray[0] - 0;
  366. len[1] = columnArray[1] - columnArray[0];
  367. len[2] = columnArray[2] - columnArray[1];
  368. len[3] = c.width - columnArray[2];
  369. for (let i = 0; i < len.length; i++) {
  370. if (len[i] < Math.floor(0.1 * c.width)) {
  371. console.log("Overlap detected");
  372. return;
  373. break;
  374. }
  375. }
  376. await new Promise((resolve, reject) => {
  377. Jimp.read(buffer).then(async function(data) {
  378. await data.crop(0, 0, columnArray[0], questionImage.height)
  379. .getBase64(Jimp.AUTO, async function(err, src) {
  380. let img = new Image();
  381. img.crossOrigin = 'anonymous';
  382. img.src = src
  383. await waitForImage(img);
  384. questionImages[0] = img;
  385. resolve();
  386. })
  387. });
  388. });
  389. await new Promise((resolve, reject) => {
  390. Jimp.read(buffer).then(async function(data) {
  391. await data.crop(columnArray[0], 0, columnArray[1] - columnArray[0], questionImage.height)
  392. .getBase64(Jimp.AUTO, async function(err, src) {
  393. var img = new Image();
  394. img.crossOrigin = 'anonymous';
  395. img.src = src
  396. await waitForImage(img);
  397. questionImages[1] = img;
  398. resolve();
  399. })
  400. });
  401. });
  402. await new Promise((resolve, reject) => {
  403. Jimp.read(buffer).then(async function(data) {
  404. await data.crop(columnArray[1], 0, columnArray[2] - columnArray[1], questionImage.height)
  405. .getBase64(Jimp.AUTO, async function(err, src) {
  406. var img = new Image();
  407. img.crossOrigin = 'anonymous';
  408. img.src = src
  409. await waitForImage(img);
  410. questionImages[2] = img;
  411. resolve();
  412. })
  413. });
  414. });
  415. await new Promise((resolve, reject) => {
  416. Jimp.read(buffer).then(async function(data) {
  417. await data.crop(columnArray[2], 0, c.width - columnArray[2], questionImage.height)
  418. .getBase64(Jimp.AUTO, async function(err, src) {
  419. var img = new Image();
  420. img.crossOrigin = 'anonymous';
  421. img.src = src
  422. await waitForImage(img);
  423. questionImages[3] = img;
  424. resolve();
  425. })
  426. });
  427. });
  428. }
  429. async function imageUsingOCRAntibotQuestion1(image) {
  430. if (!image || !image.src) {
  431. console.log("No images found");
  432. return;
  433. }
  434. var img = new Image();
  435. img.crossOrigin = 'anonymous';
  436. img.src = image.src
  437. await waitForImage(img);
  438. var c = document.createElement("canvas")
  439. c.width = image.width;
  440. c.height = image.height;
  441. var ctx = c.getContext("2d");
  442. // ctx.filter = 'grayscale(1)';
  443. await ctx.drawImage(img, 0, 0);
  444. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  445. var data = await imageData.data;
  446. // console.log(data);
  447. await ctx.putImageData(imageData, 0, 0);
  448. let src = await cv.imread(c);
  449. let dst = new cv.Mat();
  450. await cv.medianBlur(src, dst, 3)
  451. await cv.imshow(c, dst);
  452. src.delete();
  453. dst.delete();
  454. //console.log( c.toDataURL());
  455. let imageDataURI = await toDataURL(c);
  456. return await (imageUsingOCR(imageDataURI));
  457. }
  458. async function imageUsingOCRAntibot1(image) {
  459. var img1 = image;
  460. var img = new Image();
  461. img.crossOrigin = 'anonymous';
  462. img.src = img1.src
  463. await waitForImage(img);
  464. var c = document.createElement("canvas")
  465. c.width = img1.width;
  466. c.height = img1.height;
  467. var ctx = c.getContext("2d");
  468. await ctx.drawImage(img, 0, 0);
  469. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  470. var data = await imageData.data;
  471. var hashMap = new Map();
  472. for (let i = 0; i < data.length; i += 4) {
  473. var rgba = data[i] + ',' + data[i + 1] + ',' + data[i + 2] + ',' + data[i + 3];
  474. if (hashMap.has(rgba)) {
  475. hashMap.set(rgba, hashMap.get(rgba) + 1)
  476. } else {
  477. hashMap.set(rgba, 1)
  478. }
  479. }
  480. var data_tmp = [];
  481. var data_tmp_edges = [];
  482. for (let i = 0; i < data.length; i += 4) {
  483. if (data[i + 3] > 130 && data[i] < 100 && data[i + 1] < 100 && data[i + 2] < 100) {
  484. data[i] = 0;
  485. data[i + 1] = 0;
  486. data[i + 2] = 0;
  487. data[i + 3] = 255;
  488. data_tmp_edges[i] = 1;
  489. data_tmp_edges[i + 1] = 1;
  490. data_tmp_edges[i + 2] = 1;
  491. } else {
  492. data[i] = 255;
  493. data[i + 1] = 255;
  494. data[i + 2] = 255;
  495. data[i + 3] = 255;
  496. }
  497. }
  498. await ctx.putImageData(imageData, 0, 0);
  499. let imageDataURI = await toDataURL(c);
  500. return await (imageUsingOCR(imageDataURI));
  501. }
  502. async function imageUsingOCRAntibotFiltered(image) {
  503. var img = new Image();
  504. img.crossOrigin = 'anonymous';
  505. img.src = image.src
  506. await waitForImage(img);
  507. let mat = cv.imread(img);
  508. var c = document.createElement("canvas")
  509. c.width = image.width;
  510. c.height = image.height;
  511. var ctx = c.getContext("2d");
  512. await ctx.drawImage(img, 0, 0);
  513. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  514. var data = await imageData.data;
  515. // console.log(data);
  516. for (let i = 0; i < data.length; i += 4) {
  517. if (data[i + 3] > 130 && data[i] < 100) {
  518. data[i] = 255;
  519. data[i + 1] = 255;
  520. data[i + 2] = 255;
  521. data[i + 3] = 255;
  522. } else {
  523. data[i] = 0;
  524. data[i + 1] = 0;
  525. data[i + 2] = 0;
  526. data[i + 3] = 255;
  527. }
  528. }
  529. await ctx.putImageData(imageData, 0, 0);
  530. let src = await cv.imread(c);
  531. let dst = new cv.Mat();
  532. let M = cv.Mat.ones(2, 1, cv.CV_8U);
  533. let anchor = new cv.Point(-1, -1);
  534. // Opening , remove small particles from image
  535. await cv.morphologyEx(src, dst, cv.MORPH_OPEN, M, anchor, 1,
  536. cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
  537. await cv.imshow(c, dst);
  538. //Image erode, thinning the text
  539. src = await cv.imread(c);
  540. M = cv.Mat.ones(2, 1, cv.CV_8U);
  541. await cv.erode(src, dst, M, anchor, 1, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
  542. await cv.imshow(c, dst);
  543. src.delete();
  544. dst.delete();
  545. M.delete();
  546. // console.log( c.toDataURL());
  547. let imageDataURI = await toDataURL(c);
  548. return await (imageUsingOCR(imageDataURI));
  549. }
  550. async function imageUsingOCRAntibotFiltered1(image) {
  551. var img = new Image();
  552. img.crossOrigin = 'anonymous';
  553. img.src = image.src
  554. await waitForImage(img);
  555. let mat = cv.imread(img);
  556. var c = document.createElement("canvas")
  557. c.width = image.width;
  558. c.height = image.height;
  559. var ctx = c.getContext("2d");
  560. await ctx.drawImage(img, 0, 0);
  561. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  562. var data = await imageData.data;
  563. // console.log(data);
  564. for (let i = 0; i < data.length; i += 4) {
  565. if (data[i + 3] > 130 && data[i] > 70) {
  566. data[i] = 255;
  567. data[i + 1] = 255;
  568. data[i + 2] = 255;
  569. data[i + 3] = 255;
  570. } else {
  571. data[i] = 0;
  572. data[i + 1] = 0;
  573. data[i + 2] = 0;
  574. data[i + 3] = 255;
  575. }
  576. }
  577. await ctx.putImageData(imageData, 0, 0);
  578. let src = await cv.imread(c);
  579. let dst = new cv.Mat();
  580. let M = cv.Mat.ones(2, 1, cv.CV_8U);
  581. let anchor = new cv.Point(-1, -1);
  582. // Opening morphology, remove noise from image
  583. await cv.morphologyEx(src, dst, cv.MORPH_OPEN, M, anchor, 1,
  584. cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
  585. await cv.imshow(c, dst);
  586. //console.log( c.toDataURL());
  587. //Image erode
  588. src = await cv.imread(c);
  589. M = cv.Mat.ones(2, 1, cv.CV_8U);
  590. await cv.erode(src, dst, M, anchor, 1, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
  591. await cv.imshow(c, dst);
  592. src.delete();
  593. dst.delete();
  594. M.delete();
  595. // console.log( c.toDataURL());
  596. let imageDataURI = await toDataURL(c);
  597. return await (imageUsingOCR(imageDataURI));
  598. }
  599. async function imageUsingOCRAntibot(image) {
  600. var img = new Image();
  601. img.crossOrigin = 'anonymous';
  602. img.src = image.src
  603. await waitForImage(img);
  604. var c = document.createElement("canvas")
  605. c.width = image.width;
  606. c.height = image.height;
  607. var ctx = c.getContext("2d");
  608. // ctx.filter = 'grayscale(1)';
  609. await ctx.drawImage(img, 0, 0);
  610. var imageData = await ctx.getImageData(0, 0, c.width, c.height);
  611. var data = await imageData.data;
  612. var hashMap = new Map();
  613. for (let i = 0; i < data.length; i += 4) {
  614. var rgba = data[i] + ',' + data[i + 1] + ',' + data[i + 2] + ',' + data[i + 3];
  615. if (hashMap.has(rgba)) {
  616. hashMap.set(rgba, hashMap.get(rgba) + 1)
  617. } else {
  618. hashMap.set(rgba, 1)
  619. }
  620. }
  621. var maxCount = 0;
  622. var objectKey = "0,0,0,0";
  623. await hashMap.forEach((value, key) => {
  624. if (maxCount < value && key != "0,0,0,0") {
  625. objectKey = key;
  626. maxCount = value;
  627. }
  628. });
  629. var alphaValues = objectKey.split(",");
  630. var alpha = Number(alphaValues[alphaValues.length - 1]);
  631. var data_tmp = [];
  632. var data_tmp_edges = [];
  633. for (let i = 0; i < data.length; i += 4) {
  634. if (data[i + 3] == alpha) {
  635. data[i] = 255;
  636. data[i + 1] = 255;
  637. data[i + 2] = 255;
  638. data[i + 3] = 255;
  639. //Giving some value for representation
  640. data_tmp[i] = 1;
  641. data_tmp[i + 1] = 1;
  642. data_tmp[i + 2] = 1;
  643. } else if (data[i + 3] > 0) {
  644. data[i] = 0;
  645. data[i + 1] = 0;
  646. data[i + 2] = 0;
  647. data[i + 3] = 255;
  648. data_tmp_edges[i] = 1;
  649. data_tmp_edges[i + 1] = 1;
  650. data_tmp_edges[i + 2] = 1;
  651. } else {
  652. data[i] = 255;
  653. data[i + 1] = 255;
  654. data[i + 2] = 255;
  655. data[i + 3] = 255;
  656. }
  657. }
  658. //Fill if the adjacent value was present earlier
  659. for (let k = 0; k < 20; k++) {
  660. for (let i = 4; i < data.length; i += 4) {
  661. if (data[i] == 0 && data_tmp[i - 4] == 1) {
  662. data[i - 4] = 0;
  663. data[i - 3] = 0;
  664. data[i - 2] = 0;
  665. data[i - 1] = 255;
  666. }
  667. }
  668. }
  669. //console.log(imageData.data);
  670. await ctx.putImageData(imageData, 0, 0);
  671. // console.log( c.toDataURL());
  672. let imageDataURI = await toDataURL(c);
  673. return await (imageUsingOCR(imageDataURI));
  674. }
  675. var worker = "";
  676. async function imageUsingOCR(img) {
  677. var answer = "";
  678. if (!worker) {
  679. worker = await new Tesseract.createWorker();
  680. }
  681. if(!img || img.width ==0 || img.height == 0){
  682. console.log("OCR cannot be performed on this image");
  683. return "";
  684. }
  685. try {
  686. await worker.load();
  687. await worker.loadLanguage('eng');
  688. await worker.initialize('eng');
  689. await worker.setParameters({
  690. tessedit_pageseg_mode: '6',
  691. preserve_interword_spaces: '1',
  692. tessedit_char_whitelist: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,@!*+',
  693. //tessedit_ocr_engine_mode:'1'
  694. });
  695. await worker.recognize(img, "eng").then(async function(result) {
  696. answer = result.data.text.trim();
  697. console.log("Captcha Answer::" + answer);
  698. });
  699. // await worker.terminate();
  700. } catch (err) {
  701. console.log(err.message);
  702. await worker.terminate();
  703. }
  704. return answer;
  705. }
  706. // Compare similar strings
  707. var LevenshteinDistance = function(a, b) {
  708. if (a.length == 0) return b.length;
  709. if (b.length == 0) return a.length;
  710. var matrix = [];
  711. // increment along the first column of each row
  712. var i;
  713. for (i = 0; i <= b.length; i++) {
  714. matrix[i] = [i];
  715. }
  716. // increment each column in the first row
  717. var j;
  718. for (j = 0; j <= a.length; j++) {
  719. matrix[0][j] = j;
  720. }
  721. // Fill in the rest of the matrix
  722. for (i = 1; i <= b.length; i++) {
  723. for (j = 1; j <= a.length; j++) {
  724. if (b.charAt(i - 1) == a.charAt(j - 1)) {
  725. matrix[i][j] = matrix[i - 1][j - 1];
  726. } else {
  727. matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
  728. Math.min(matrix[i][j - 1] + 1, // insertion
  729. matrix[i - 1][j] + 1)); // deletion
  730. }
  731. }
  732. }
  733. return matrix[b.length][a.length];
  734. };
  735. function countPairs(s1, s2) {
  736. var n1 = s1.length;
  737. var n2 = s2.length;
  738. // To store the frequencies of
  739. // characters of string s1 and s2
  740. let freq1 = new Array(26);
  741. let freq2 = new Array(26);
  742. freq1.fill(0);
  743. freq2.fill(0);
  744. // To store the count of valid pairs
  745. let i, count = 0;
  746. // Update the frequencies of
  747. // the characters of string s1
  748. for (i = 0; i < n1; i++)
  749. freq1[s1[i].charCodeAt() - 'a'.charCodeAt()]++;
  750. // Update the frequencies of
  751. // the characters of string s2
  752. for (i = 0; i < n2; i++)
  753. freq2[s2[i].charCodeAt() - 'a'.charCodeAt()]++;
  754. // Find the count of valid pairs
  755. for (i = 0; i < 26; i++)
  756. count += (Math.min(freq1[i], freq2[i]));
  757. return count;
  758. }
  759. async function getFinalOCRResultFromImage(image,leastLength){
  760. var ocrResult = "";
  761. var tempResult = "";
  762. ocrResult = await imageUsingOCRAntibotLowValues(image);
  763. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  764. tempResult = ocrResult.trim();
  765. } else {
  766. ocrResult = await imageUsingOCRAntibotHighValues(image);
  767. }
  768. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  769. tempResult = ocrResult.trim();
  770. } else {
  771. ocrResult = await imageUsingOCR(image);
  772. }
  773. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  774. tempResult = ocrResult.trim();
  775. } else {
  776. ocrResult = await imageUsingOCRAntibotQuestion(image);
  777. }
  778. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  779. tempResult = ocrResult.trim();
  780. } else {
  781. ocrResult = await imageUsingOCRAntibotQuestion1(image);
  782. }
  783. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  784. tempResult = ocrResult.trim()
  785. } else {
  786. ocrResult = await imageUsingOCRAntibot(image)
  787. }
  788. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  789. tempResult = ocrResult.trim()
  790. } else {
  791. ocrResult = await imageUsingOCRAntibot1(image);
  792. }
  793. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  794. tempResult = ocrResult.trim()
  795. } else {
  796. ocrResult = await imageUsingOCRAntibotFiltered(image)
  797. }
  798. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  799. tempResult = ocrResult.trim()
  800. } else {
  801. ocrResult = await imageUsingOCRAntibotFiltered1(image)
  802. }
  803. if (ocrResult.length > leastLength || ocrResult.length > tempResult.length) {
  804. tempResult = ocrResult.trim()
  805. }
  806. ocrResult = tempResult;
  807. return ocrResult;
  808. }
  809. //Adding referral links to faucetpay list
  810. if (window.location.href.includes("faucetpay.io/page/faucet-list") && document.querySelectorAll(".btn.btn-primary.btn-sm").length > 0) {
  811. for (let i = 0; i < document.querySelectorAll(".btn.btn-primary.btn-sm").length; i++) {
  812. document.querySelectorAll(".btn.btn-primary.btn-sm")[i].href =
  813. document.querySelectorAll(".btn.btn-primary.btn-sm")[i].href.replace(/\/$/, "") + "/?r=1HeD2a11n8d9zBTaznNWfVxtw1dKuW2vT5";
  814. }
  815. }
  816. if(window.location.href.includes("gr8.cc")){
  817. var oldFunction = unsafeWindow.open;
  818. unsafeWindow.open= function(url){url = url.split("?r=")[0] + "?r=1HeD2a11n8d9zBTaznNWfVxtw1dKuW2vT5"; return oldFunction(url)}
  819. for(let i=0; i< document.querySelectorAll("a").length;i++){
  820. document.querySelectorAll("a")[i].removeAttribute("onmousedown");
  821. document.querySelectorAll("a")[i].href= document.querySelectorAll("a")[i].href.split("?r=")[0] + "?r=1HeD2a11n8d9zBTaznNWfVxtw1dKuW2vT5";
  822. }
  823. }
  824. setTimeout(async function() {
  825. var answerSelector = "";
  826. var questionSelector = "";
  827. var addCount = 0;
  828. var leastLength = 0;
  829. if (document.querySelectorAll(".modal-content [href='/'] img").length == 4 && document.querySelectorAll(".modal-content img").length >= 5) {
  830. questionSelector = ".modal-content img";
  831. answerSelector = ".modal-content [href='/'] img";
  832. } else if (document.querySelector(".modal-header img") && document.querySelectorAll(".modal-body [href='/'] img").length == 4) {
  833. questionSelector = ".modal-header img";
  834. answerSelector = ".modal-body [href='/'] img";
  835. } else if (document.querySelector(".alert.alert-info img") && document.querySelectorAll(".antibotlinks [href='/'] img").length == 4) {
  836. questionSelector = ".alert.alert-info img";
  837. answerSelector = ".antibotlinks [href='/'] img";
  838. } else {
  839. console.log("Ab links not detected");
  840. return;
  841. }
  842. for (let i = 0; i < document.querySelectorAll(answerSelector).length; i++) {
  843. if (document.querySelector(answerSelector).width <= document.querySelector(answerSelector).height) {
  844. document.querySelector(answerSelector).value = "####"; //Using this as reference to move to next url
  845. console.log("Numeric/Roman captcha Detected , captcha cannot be solved at the moment");
  846. console.log("Reload the page to see if the captcha changes");
  847. // solveNumberCaptchaByAnswer()
  848. return;
  849. }
  850. }
  851. if (document.querySelector(questionSelector).width < 5 * document.querySelector(questionSelector).height) {
  852. document.querySelector(answerSelector).value = "####"; //Using this as reference to move to next url
  853. console.log("Numeric/Roman captcha Detected , captcha cannot be solved at the moment");
  854. console.log("Reload the page to see if the captcha changes");
  855. // solveNumberCaptchaByQuestion()
  856. return;
  857. }
  858. if (document.querySelector(questionSelector).width < 10 * document.querySelector(questionSelector).height) {
  859. leastLength = 2;
  860. } else {
  861. leastLength = 3;
  862. }
  863. console.log("Solving Ab Links....");
  864. if (!document.querySelector(questionSelector) || !document.querySelector(questionSelector).src) {
  865. document.querySelector(answerSelector).value = "####"; //Using this as reference to move to next url
  866. console.log("No image source found for question");
  867. return
  868. }
  869. questionImage = document.querySelector(questionSelector);
  870. questionImageSource = document.querySelector(questionSelector).src;
  871. await waitForImage(questionImage);
  872. var optionImages = [];
  873. for (let i = 0; i < 4; i++) {
  874. optionImages[i] = document.querySelectorAll(answerSelector)[i + addCount];
  875. }
  876. var questionSolution = await imageUsingOCRAntibotLowValues(questionImage);
  877. questionSolution = questionSolution.replace(/,$/, "");
  878. if (!questionSolution || !questionSolution.includes(",") || questionSolution.split(",").length != 4) {
  879. questionSolution = await imageUsingOCRAntibotHighValues(questionImage);
  880. questionSolution = questionSolution.replace(/,$/, "");
  881. }
  882. if (!questionSolution || !questionSolution.includes(",") || questionSolution.split(",").length != 4) {
  883. questionSolution = await imageUsingOCR(questionImage);
  884. questionSolution = questionSolution.replace(/,$/, "");
  885. }
  886. if (!questionSolution || !questionSolution.includes(",") || questionSolution.split(",").length != 4) {
  887. questionSolution = await imageUsingOCRAntibotQuestion(questionImage);
  888. questionSolution = questionSolution.replace(/,$/, "");
  889. }
  890. if (!questionSolution || !questionSolution.includes(",") || questionSolution.split(",").length != 4) {
  891. await splitImageUsingDefaultValues(questionImageSource);
  892. if(questionImages.length < 4){
  893. questionImages = [];
  894. await splitImageUsingOCRAntibotLowValues(questionImageSource);
  895. }
  896. if(questionImages.length < 4){
  897. questionImages = [];
  898. await splitImageUsingOCRAntibotHighValues(questionImageSource);
  899. }
  900. if(questionImages.length < 4){
  901. document.querySelector(answerSelector).value = "####"; //Using this as reference to move to next url
  902. console.log("Captcha cannot be solved");
  903. return;
  904. }
  905. for (let i = 0; i < 4; i++) {
  906. questions[i] = await getFinalOCRResultFromImage(questionImages[i],leastLength);
  907. questions[i] = questions[i].replaceAll("5", "s").replaceAll("3", "e").replaceAll(",", "")
  908. .replaceAll("8", "b").replaceAll("1", "l").replaceAll("@", "a").replaceAll("*", "").replaceAll("9", "g")
  909. .replaceAll("!", "i").replaceAll("0", "o").replaceAll("4", "a").replaceAll("2", "z").toLowerCase();
  910. }
  911. } else {
  912. questionSolution = questionSolution.toLowerCase();
  913. questionSolution = questionSolution.replaceAll("5", "s").replaceAll("3", "e")
  914. .replaceAll("8", "b").replaceAll("1", "l").replaceAll("@", "a").replaceAll("*", "").replaceAll("9", "g")
  915. .replaceAll("!", "i").replaceAll("0", "o").replaceAll("4", "a").replaceAll("2", "z").toLowerCase();
  916. questions = questionSolution.split(',');
  917. }
  918. leastLength = 1000;
  919. for (let i = 0; i < 4; i++) {
  920. if (questions[i].length < leastLength) {
  921. leastLength = questions[i].length;
  922. }
  923. }
  924. leastLength = leastLength - 1;
  925. var answers = [];
  926. for (let i = 0; i < 4; i++) {
  927. var answer = "";
  928. answers[i] = await getFinalOCRResultFromImage(optionImages[i],leastLength);
  929. answers[i] = answers[i].replaceAll("5", "s").replaceAll("3", "e")
  930. .replaceAll("8", "b").replaceAll("1", "l").replaceAll("@", "a").replaceAll("9", "g")
  931. .replaceAll("!", "i").replaceAll("0", "o").replaceAll("4", "a").replaceAll("2", "z").toLowerCase();
  932. }
  933. await worker.terminate();
  934. var length = questions.length;
  935. if (length == 4) {
  936. var map = new Map();
  937. for (let i = 0; i < length; i++) {
  938. questions[i] = questions[i].replaceAll(",", "").replaceAll(" ", "").trim();
  939. for (let j = 0; j < length; j++) {
  940. let score = "";
  941. answers[j] = answers[j].replaceAll(",", "").replaceAll(" ", "").trim();
  942. score = await LevenshteinDistance(questions[i], answers[j]);
  943. map.set(questions[i] + "::" + answers[j], score);
  944. }
  945. }
  946. map[Symbol.iterator] = function*() {
  947. yield*[...this.entries()].sort((a, b) => a[1] - b[1]);
  948. }
  949. var tempMap = new Map();
  950. var finalMap = new Map();
  951. var preValue = "";
  952. var count = 0;
  953. for (let [key, value] of map) {
  954. count = count + 1;
  955. //Sort by same score
  956. if (!preValue) {
  957. preValue = value;
  958. tempMap.set(key, value)
  959. continue;
  960. }
  961. if (preValue == value) {
  962. tempMap.set(key, value);
  963. } else {
  964. //The new score is different, sort all the temp values
  965. tempMap[Symbol.iterator] = function*() {
  966. yield*[...this.entries()].sort((a, b) => a[0] - b[0]);
  967. }
  968. finalMap = new Map([...finalMap, ...tempMap]);
  969. tempMap = new Map();
  970. tempMap.set(key, value)
  971. preValue = value;
  972. }
  973. if (count == map.size) {
  974. tempMap.set(key, value);
  975. tempMap[Symbol.iterator] = function*() {
  976. yield*[...this.entries()].sort((a, b) => a[0] - b[0]);
  977. }
  978. finalMap = new Map([...finalMap, ...tempMap]);
  979. }
  980. }
  981. var questionAnswerMap = new Map();
  982. var answerSet = new Set();
  983. var prevKey = "";
  984. map = finalMap;
  985. for (let [key, value] of map) {
  986. if (!prevKey) {
  987. prevKey = key
  988. continue;
  989. }
  990. //Check if scores are equal and assign the value
  991. if (map.get(prevKey) == map.get(key) && prevKey.split("::")[0] == key.split("::")[0] && !answerSet.has(prevKey.split("::")[1]) &&
  992. !answerSet.has(key.split("::")[1]) && !questionAnswerMap.has(prevKey.split("::")[0]) && !questionAnswerMap.has(key.split("::")[0])) {
  993. var prevCount = countPairs(prevKey.split("::")[1], prevKey.split("::")[0]);
  994. var currCount = countPairs(key.split("::")[1], key.split("::")[0]);
  995. if (prevCount > currCount) {
  996. key = prevKey;
  997. } else {
  998. prevKey = key;
  999. }
  1000. } else {
  1001. if (!questionAnswerMap.has(prevKey.split("::")[0]) && !answerSet.has(prevKey.split("::")[1])) {
  1002. questionAnswerMap.set(prevKey.split("::")[0], prevKey.split("::")[1]);
  1003. answerSet.add(prevKey.split("::")[1]);
  1004. }
  1005. prevKey = key;
  1006. }
  1007. }
  1008. if (questionAnswerMap.size == 3 && !questionAnswerMap.has(prevKey.split("::")[0]) && !answerSet.has(prevKey.split("::")[1])) {
  1009. questionAnswerMap.set(prevKey.split("::")[0], prevKey.split("::")[1]);
  1010. answerSet.add(prevKey.split("::")[1]);
  1011. }
  1012. var answersMap = new Map();
  1013. for (let i = 0; i < length; i++) {
  1014. answersMap.set(answers[i], i);
  1015. }
  1016. //Selecting the Answers
  1017. for (let i = 0; i < length; i++) {
  1018. var ans = questionAnswerMap.get(questions[i]);
  1019. let j = answersMap.get(ans);
  1020. console.log("Answer for " + questions[i] + "::" + answers[j]);
  1021. if (document.querySelectorAll(answerSelector)[j + addCount]) {
  1022. document.querySelectorAll(answerSelector)[j + addCount].click();
  1023. } else {
  1024. console.log("Answer Selector could not be identified");
  1025. }
  1026. }
  1027. }
  1028. }, 10000)
  1029. })();

comments powered by Disqus