Guest User

Untitled

a guest
Jun 9th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. // ==UserScript==
  2. // @name chaptcha
  3. // @namespace https://2chk.hk/chaptcha
  4. // @description Automatically enter captcha at 2ch.hk using chaptcha.py backend
  5. // @downloadURL https://raw.githubusercontent.com/Kagami/chaptcha/master/chaptcha.user.js
  6. // @updateURL https://raw.githubusercontent.com/Kagami/chaptcha/master/chaptcha.user.js
  7. // @include https://2ch.hk/*
  8. // @version 0.0.2
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. // TODO: Some better way to store that setting?
  13. var OCR_BACKEND_URL = localStorage.getItem("OCR_BACKEND_URL") ||
  14. "https://ch.genshiken.org";
  15.  
  16. function getImageData(img) {
  17. return new Promise(function(resolve, reject) {
  18. img.onload = function() {
  19. var canvas = document.createElement("canvas");
  20. canvas.width = img.width;
  21. canvas.height = img.height;
  22. var context = canvas.getContext("2d");
  23. context.drawImage(img, 0, 0);
  24. canvas.toBlob(resolve);
  25. };
  26. img.onerror = reject;
  27. });
  28. }
  29.  
  30. function ocr(data) {
  31. return new Promise(function(resolve, reject) {
  32. var xhr = new XMLHttpRequest();
  33. xhr.open("POST", OCR_BACKEND_URL + "/ocr", true);
  34. xhr.onload = function() {
  35. if (this.status >= 200 && this.status < 400) {
  36. resolve(this.responseText);
  37. } else {
  38. reject(new Error(this.responseText));
  39. }
  40. };
  41. xhr.onerror = reject;
  42. var form = new FormData();
  43. form.append("file", data);
  44. xhr.send(form);
  45. });
  46. }
  47.  
  48. function setAnswer(answer) {
  49. document.getElementById("captcha-value").value = answer;
  50. document.getElementById("qr-captcha-value").value = answer;
  51. }
  52.  
  53. function initChaptcha() {
  54. var container = document.getElementById("captcha-widget-main");
  55. if (!container) return;
  56. var observer = new MutationObserver(function(mutations) {
  57. mutations.forEach(function(mutation) {
  58. Array.prototype.filter.call(mutation.addedNodes, function(node) {
  59. return node.tagName === "IMG";
  60. }).forEach(function(img) {
  61. setAnswer("");
  62. getImageData(img).then(ocr).then(function(answer) {
  63. setAnswer(answer);
  64. }, function() {
  65. // Indicate error.
  66. setAnswer("000000");
  67. });
  68. });
  69. });
  70. });
  71. // Captcha updates synchronously in all places so it's enough to
  72. // observe only single one.
  73. observer.observe(container, {childList: true});
  74. }
  75.  
  76. initChaptcha();
Add Comment
Please, Sign In to add comment