Automatic verification code input via JavaScript scripts

Source: Internet
Author: User

  Many sites in the user to make a click, such as online shopping confirmation purchase, the user will be asked to enter a verification code, which in general is not a problem, but in the user needs to buy frequently or snapped when it is very annoying. In fact, the site's verification code is usually generated by the JS script, so you can also write your own JS script to implement the verification Code automatic input.

The approximate principle, first of all to observe the original page pop-up verification code picture, here give two examples:, they are the foreground black and white, background color, width 100 high 20 of the picture, further put the picture into the PS open can be found that each number occupies a width of 8 high 10 of the rectangle, and the positions and relative distances of the 5 rectangles are fixed. Therefore, the principle is very simple, in advance for each number and letter to make a unique code table, each code is 80-dimensional 0-1 encoding, 0 means the current number (letter) in this small rectangle is the background, 1 is the foreground, so all the numbers and letters have their own unique code. Each time a new code map randomly generated, as long as the 5 numbers (letters) corresponding to the small rectangle to take out, and then go through the 80 pixels, to get a length of 80 encoding, and then to match the encoding table one by one, you know exactly which number (letter). The code is as follows:

1 var captcha = '; * * Verification code * /2 var canvas = document.createelement (' canvas ');3 canvas.width = ' 100 ';/* Verify code picture Width * /4 canvas.height = ' 20 ';/* Verify code picture Height * /5 canvas.style.display = ' none ';6 document.body.appendChild (canvas);/* Add this section to the page code */7 var context = Canvas.getcontext (' 2d ');8 var image = new Image ();9 image.src = document.getElementById (' Coderefresh '). Src;Ten image.onload = function () { One context.drawimage (image, 0, 0); Afor (var i = 0; i<5; i++) {/* 5-bit verification code, processed in turn * / - var dbstring= "; -         var canvasdata= Context.getimagedata (+ 9 * I, 5, 8, 10); /* Above example the coordinates of the upper left corner of the first small rectangle are (27,5), then offset by 9 is the next small rectangle */ the For (Var j= 0;J < CanvasData.data.length; J += 4){ - var R= Canvasdata.data[j+ 0]; - var g= Canvasdata.data[j+ 1]; - var b= Canvasdata.data[j+ 2]; + / * Verify code character only black and white, background color is other RGB values * / - if (! ( (R== 0 && G== 0 && B== 0) | | (R== 255 && G== 255 && B== 255 ))) + dbstring += ' 0 '; A             Else at dbstring += ' 1 '; -         } - For (var k= 0;k <; k++) {/* Go to Encoding table for matching * / - if (dbstring== Characters[0][k]/* Match success * / - Break ; -         } in Captcha += Characters[1][k]; -     } to / * Add to Cart * / + var reslut_list= newArray (); - Reslut_list.push (Window.location.pathname.match (/\d+/) [0]); the Reslut_list.push (CAPTCHA); * AddToCart (reslut_list); $ }

Automatic verification code input via JavaScript scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.