lulzScript

lulz.net Captcha Refresher [2012-10-10]

Oct 10th, 2012
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           lulz.net Captcha Refresher
  3. // @namespace      http://lulz.net/
  4. // @version        1.0.0
  5. // @description    Checks if the captcha is still valid before submitting a post. If not, it will refresh the captcha and prompt the user to retype it. Also allows to refresh the captcha manually by clicking on it. This script should work with Opera, Firefox and Chrome. Install guide, updates, changelog and other scripts @ http://pastebin.com/u/lulzScript
  6. // @include http://lulz.net/*
  7. // ==/UserScript==
  8.  
  9. //Global variables
  10. var capInp;
  11. var capImg;
  12. var submBtn;
  13. var postfrm;
  14. var statusText;
  15.  
  16. function newCaptchaURL()
  17. {
  18.   //Change the dummy parameter of the captcha image source URL to bypass caching of the captcha image and return the new URL
  19.   return (capImg.getAttribute('src').replace(/dummy=\d*/, 'dummy=' + Math.floor((Math.random()*10000))));
  20. }
  21.  
  22. function onCaptchaRefresh()
  23. {
  24.   //Clear status text after captcha has been refreshed.
  25.   statusText.data = '';
  26.   //Unregister onLoad
  27.   capImg.onload = null;
  28. }
  29.  
  30. function onCaptchaClick(event)
  31. {
  32.   statusText.data = 'Refreshing captcha...';
  33.   //Get URL for new Captcha.
  34.   var isrc = newCaptchaURL();
  35.   //Calls onCaptchaRefresh after new captcha has been loaded to clear status text.
  36.   capImg.onload = onCaptchaRefresh;
  37.   //Set the new image source to trigger the captcha refresh.
  38.   capImg.setAttribute('src', isrc);
  39. }
  40.  
  41. function activateCaptchaScript()
  42. {
  43.   //Get the "input" html tag of the captcha
  44.   capInp = document.getElementsByName('captcha');
  45.   if ( capInp && (capInp.length > 0) )
  46.   {
  47.     //Get the "img" html tag of the captcha
  48.     imgs = capInp[0].parentNode.getElementsByTagName('img');
  49.     if ( imgs && (imgs.length > 0) )
  50.     {
  51.       //Save img object of Captcha
  52.       capImg = imgs[0];
  53.  
  54.       //Get postform to global variable
  55.       postfrm = document.forms['postform'];
  56.       //Find submit button
  57.       var inps = postfrm.getElementsByTagName('input');
  58.       for (var i=0; i < inps.length; i++)
  59.       {
  60.         //if the type attribute = "submit" then inps[i] is the submit button
  61.         if ( inps[i].getAttribute('type') == 'submit' )
  62.         {
  63.           //Save submit button object
  64.           submBtn = inps[i];
  65.  
  66.           //Insert elements into the document to display status messages
  67.           //Enclose everything in a div container
  68.           var statusDiv = document.createElement('div');
  69.           //Insert a line break
  70.           statusDiv.innerHTML = '<br/>';
  71.           //Insert a text node for the status text. initial text is empty ('').
  72.           //The global variable statusText will refer to this text node below the captcha
  73.           statusText = document.createTextNode('');
  74.           //Insert the text node into the div container
  75.           statusDiv.insertBefore(statusText, null);
  76.           //Insert the div container into the document (below the captcha image)
  77.           capImg.parentNode.insertBefore(statusDiv, null);
  78.  
  79.           //At this point the submit button and the captcha image have been found and the click event listeners for both are registered.
  80.           capImg.addEventListener("click", onCaptchaClick, false);
  81.           submBtn.addEventListener("click", onSubmitClick, false);
  82.  
  83.           //The script is now successfully activated. Set flag to "true"
  84.           captchaScriptActivated = true;
  85.         }
  86.       }
  87.     }
  88.   }
  89. }
  90.  
  91. //This function converts an image into a data string using a canvas element.
  92. //The data strings can be used to easily compared two images and to determine if they are different or not.
  93. //
  94. //img:          The image object to be converted to a data string
  95. //return value: The data string
  96. function imgToDataStr(img)
  97. {
  98.   //Create a canvas element
  99.   var cnv = document.createElement('canvas');
  100.  
  101.   //Set canvas width and height to the values of the image
  102.   cnv.width = img.width;
  103.   cnv.height = img.height;
  104.  
  105.   //Draw the image onto the canvas
  106.   cnv.getContext('2d').drawImage(img, 0, 0);
  107.  
  108.   //Convert the canvas data (the image) into a data string and return it
  109.   return( cnv.toDataURL() );
  110. }
  111.  
  112. //Called after the captch has successfully loaded
  113. function captchaLoadOK()
  114. {
  115.   //Captcha has successufully loaded. Set status text.
  116.   statusText.data = 'Captcha loaded.';
  117.  
  118.   //Compare the current captcha image on the page with the refreshed one
  119.   if ( imgToDataStr(this) == imgToDataStr(capImg) )
  120.   {
  121.     //If the captcha images are identical, the current captcha hasn't times out yet. So the post can be submitted.
  122.     statusText.data = 'Captcha still valid. Submitting...';
  123.     postfrm.submit();  
  124.   }
  125.   else
  126.   {
  127.     //If the captcha images are different, the current captcha has timed out. Update the captcha on the page with the refreshed one.
  128.     capImg.src = this.src;
  129.     //Clear the captcha text box
  130.     capInp.value = '';
  131.     //Ask user to retype the captcha
  132.     statusText.data = 'Captcha has changed. Please retype.';
  133.   }
  134. }
  135.  
  136. //Called if the captcha failed to load.
  137. function captchaLoadError()
  138. {
  139.   //Display error message if the captcha couldn't be loaded
  140.   statusText.data = 'Error loading captcha.';
  141. }
  142.  
  143. //Called when the submit button is clicked.
  144. function onSubmitClick(event)
  145. {
  146.   //First prevent the default submit action of the form.
  147.   event.preventDefault();
  148.  
  149.   //Create an image object
  150.   var newCaptcha = new Image();
  151.  
  152.   //Set event handlers for successful and unsuccessful captcha loading.
  153.   newCaptcha.onload = captchaLoadOK;
  154.   newCaptcha.onerror = captchaLoadError;
  155.  
  156.   //Set status text
  157.   statusText.data = 'Refreshing captcha...';
  158.   //Trigger loading of a new captcha (or a copy of the current one if it hasn't timed out yet) into the newCaptcha object
  159.   newCaptcha.src = newCaptchaURL();
  160.   //Script continues at captchaLoadOK() or captchaLoadError()
  161. }
  162.  
  163. //Flags to indicate whether the script has been activated or not. Initial value is "false".
  164. var captchaScriptActivated = false;
  165.  
  166. //Try to activate the captcha script, if the page has loaded already at this point.
  167. if ( !captchaScriptActivated )
  168.   activateCaptchaScript();
  169.  
  170. document.addEventListener("DOMContentLoaded",
  171.   function Hook_load(e)
  172.   {
  173.     //Try to activate the captcha script, if it isn't active already. At this point the DOM should be avilable.
  174.     if ( !captchaScriptActivated )
  175.       activateCaptchaScript();
  176.  
  177.   }, false
  178. );
  179.  
  180. //Additional, faster way for Opera to activate the captcha script.
  181. //Inline scripts sometimes seem to block the DOMContentLoaded event until they have executed completely.
  182. //This code will activate the captcha script as soon as (but before) an inline script is called
  183. //
  184. //Check if the opera object exists. If yes, this script runs in an opera browser.
  185. if (window.opera)
  186. {
  187.   window.opera.addEventListener ('BeforeScript',
  188.     function onDCL(e)
  189.     {
  190.       //Try to activate the captcha script.
  191.       if ( !captchaScriptActivated )
  192.         activateCaptchaScript();
  193.  
  194.       //Check if the captcha script is activated (either by this function or by the DOMContentLoaded event listener above)
  195.       if ( captchaScriptActivated )
  196.       {
  197.         //Remove this event listener again if button was inserted successfully.
  198.         window.opera.removeEventListener ('BeforeScript', onDCL, false);
  199.       }
  200.  
  201.     }, false
  202.   );
  203. }
Advertisement
Add Comment
Please, Sign In to add comment