Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name lulz.net Captcha Refresher
- // @namespace http://lulz.net/
- // @version 1.0.0
- // @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
- // @include http://lulz.net/*
- // ==/UserScript==
- //Global variables
- var capInp;
- var capImg;
- var submBtn;
- var postfrm;
- var statusText;
- function newCaptchaURL()
- {
- //Change the dummy parameter of the captcha image source URL to bypass caching of the captcha image and return the new URL
- return (capImg.getAttribute('src').replace(/dummy=\d*/, 'dummy=' + Math.floor((Math.random()*10000))));
- }
- function onCaptchaRefresh()
- {
- //Clear status text after captcha has been refreshed.
- statusText.data = '';
- //Unregister onLoad
- capImg.onload = null;
- }
- function onCaptchaClick(event)
- {
- statusText.data = 'Refreshing captcha...';
- //Get URL for new Captcha.
- var isrc = newCaptchaURL();
- //Calls onCaptchaRefresh after new captcha has been loaded to clear status text.
- capImg.onload = onCaptchaRefresh;
- //Set the new image source to trigger the captcha refresh.
- capImg.setAttribute('src', isrc);
- }
- function activateCaptchaScript()
- {
- //Get the "input" html tag of the captcha
- capInp = document.getElementsByName('captcha');
- if ( capInp && (capInp.length > 0) )
- {
- //Get the "img" html tag of the captcha
- imgs = capInp[0].parentNode.getElementsByTagName('img');
- if ( imgs && (imgs.length > 0) )
- {
- //Save img object of Captcha
- capImg = imgs[0];
- //Get postform to global variable
- postfrm = document.forms['postform'];
- //Find submit button
- var inps = postfrm.getElementsByTagName('input');
- for (var i=0; i < inps.length; i++)
- {
- //if the type attribute = "submit" then inps[i] is the submit button
- if ( inps[i].getAttribute('type') == 'submit' )
- {
- //Save submit button object
- submBtn = inps[i];
- //Insert elements into the document to display status messages
- //Enclose everything in a div container
- var statusDiv = document.createElement('div');
- //Insert a line break
- statusDiv.innerHTML = '<br/>';
- //Insert a text node for the status text. initial text is empty ('').
- //The global variable statusText will refer to this text node below the captcha
- statusText = document.createTextNode('');
- //Insert the text node into the div container
- statusDiv.insertBefore(statusText, null);
- //Insert the div container into the document (below the captcha image)
- capImg.parentNode.insertBefore(statusDiv, null);
- //At this point the submit button and the captcha image have been found and the click event listeners for both are registered.
- capImg.addEventListener("click", onCaptchaClick, false);
- submBtn.addEventListener("click", onSubmitClick, false);
- //The script is now successfully activated. Set flag to "true"
- captchaScriptActivated = true;
- }
- }
- }
- }
- }
- //This function converts an image into a data string using a canvas element.
- //The data strings can be used to easily compared two images and to determine if they are different or not.
- //
- //img: The image object to be converted to a data string
- //return value: The data string
- function imgToDataStr(img)
- {
- //Create a canvas element
- var cnv = document.createElement('canvas');
- //Set canvas width and height to the values of the image
- cnv.width = img.width;
- cnv.height = img.height;
- //Draw the image onto the canvas
- cnv.getContext('2d').drawImage(img, 0, 0);
- //Convert the canvas data (the image) into a data string and return it
- return( cnv.toDataURL() );
- }
- //Called after the captch has successfully loaded
- function captchaLoadOK()
- {
- //Captcha has successufully loaded. Set status text.
- statusText.data = 'Captcha loaded.';
- //Compare the current captcha image on the page with the refreshed one
- if ( imgToDataStr(this) == imgToDataStr(capImg) )
- {
- //If the captcha images are identical, the current captcha hasn't times out yet. So the post can be submitted.
- statusText.data = 'Captcha still valid. Submitting...';
- postfrm.submit();
- }
- else
- {
- //If the captcha images are different, the current captcha has timed out. Update the captcha on the page with the refreshed one.
- capImg.src = this.src;
- //Clear the captcha text box
- capInp.value = '';
- //Ask user to retype the captcha
- statusText.data = 'Captcha has changed. Please retype.';
- }
- }
- //Called if the captcha failed to load.
- function captchaLoadError()
- {
- //Display error message if the captcha couldn't be loaded
- statusText.data = 'Error loading captcha.';
- }
- //Called when the submit button is clicked.
- function onSubmitClick(event)
- {
- //First prevent the default submit action of the form.
- event.preventDefault();
- //Create an image object
- var newCaptcha = new Image();
- //Set event handlers for successful and unsuccessful captcha loading.
- newCaptcha.onload = captchaLoadOK;
- newCaptcha.onerror = captchaLoadError;
- //Set status text
- statusText.data = 'Refreshing captcha...';
- //Trigger loading of a new captcha (or a copy of the current one if it hasn't timed out yet) into the newCaptcha object
- newCaptcha.src = newCaptchaURL();
- //Script continues at captchaLoadOK() or captchaLoadError()
- }
- //Flags to indicate whether the script has been activated or not. Initial value is "false".
- var captchaScriptActivated = false;
- //Try to activate the captcha script, if the page has loaded already at this point.
- if ( !captchaScriptActivated )
- activateCaptchaScript();
- document.addEventListener("DOMContentLoaded",
- function Hook_load(e)
- {
- //Try to activate the captcha script, if it isn't active already. At this point the DOM should be avilable.
- if ( !captchaScriptActivated )
- activateCaptchaScript();
- }, false
- );
- //Additional, faster way for Opera to activate the captcha script.
- //Inline scripts sometimes seem to block the DOMContentLoaded event until they have executed completely.
- //This code will activate the captcha script as soon as (but before) an inline script is called
- //
- //Check if the opera object exists. If yes, this script runs in an opera browser.
- if (window.opera)
- {
- window.opera.addEventListener ('BeforeScript',
- function onDCL(e)
- {
- //Try to activate the captcha script.
- if ( !captchaScriptActivated )
- activateCaptchaScript();
- //Check if the captcha script is activated (either by this function or by the DOMContentLoaded event listener above)
- if ( captchaScriptActivated )
- {
- //Remove this event listener again if button was inserted successfully.
- window.opera.removeEventListener ('BeforeScript', onDCL, false);
- }
- }, false
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment