1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

can anyone help me make an ocr for greasemonkey?

Discussion in 'Code Snippets and Tutorials' started by punkpaul, Jan 31, 2009.

  1. punkpaul

    punkpaul Level I

    Joined:
    Jan 17, 2009
    Messages:
    107
    Likes Received:
    1
    yeah, im having trouble and really have no idea as to what to do.
     
  2. chelsea1

    chelsea1 Level IV

    Joined:
    Nov 26, 2006
    Messages:
    2,538
    Likes Received:
    31
    Location:
    London
    pm ricky92, he has one, although i'm pretty sure he bought his
     
  3. punkpaul

    punkpaul Level I

    Joined:
    Jan 17, 2009
    Messages:
    107
    Likes Received:
    1
    its cool, i posted this a while back i got one now. just gotta work on cleaning up my gmab script then i may release it.
     
  4. jazzeh

    jazzeh Level I

    Joined:
    Jan 1, 2008
    Messages:
    144
    Likes Received:
    15
    I was working on one that uses the canvas tag. It should be easy but I can't really wrap my mind around it yet. There's a GM script that uses canvas to solve the megaupload captcha. For neopets, most people embed a swf that "decodes" image
     
  5. Belaarx

    Belaarx Level I

    Joined:
    Sep 23, 2008
    Messages:
    135
    Likes Received:
    16
    Location:
    San Diego
    Code (Text):
    1.  
    2. // Create the canvas element
    3. var canvas = document.createElement("canvas");
    4. canvas.width = image.width;
    5. canvas.height = image.height;
    6.  
    7. // Draw the image onto the canvas
    8. var ctx = canvas.getContext("2d");
    9. ctx.drawImage(image, 0, 0);
    10.  
    11. // Get the pixel data
    12. var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    13.  
    14. // Loop through imageData.data - an array with 4 values per pixel: red, green, blue, and alpha
    15. for (int x = 0; x < imageData.width; x++) {
    16.     for (int y = 0; y < imageData.height; y++) {
    17.         var index = 4 * (y * imageData.width + x);
    18.         var r = imageData.data[index];
    19.         var g = imageData.data[index + 1];
    20.         var b = imageData.data[index + 2];
    21.         var a = imageData.data[index + 3];
    22.  
    23.         // Do whatever you need to do with the rgba values
    24.     }
    25. }
    26.  
     
  6. Jubixion

    Jubixion Level II

    Joined:
    Jun 11, 2009
    Messages:
    205
    Likes Received:
    14
    This is posted by Belaarx though he mentioned that he found it somewhere.
    Code (Text):
    1.     // Last Updated: 2nd Feb 2009
    2.     // Lead Programmer: Laser Wave
    3.     //
    4.     // ==UserScript==
    5.     // @name          Neopets Auto Haggle
    6.     // @namespace     http://www.neofriends.net/
    7.     // @description   Auto haggle in main shops
    8.     // @include       http://neopets.com/haggle.phtml*
    9.     // @include       http://www.neopets.com/haggle.phtml*
    10.     // @include       www.neopets.com/haggle.phtml*
    11.     // @include       neopets.com/haggle.phtml*
    12.     // ==/UserScript==
    13.  
    14.     function run_cap()
    15.     {
    16.         if (document.location.href.match('haggle.phtml') && (document.body.innerHTML.indexOf("captcha_show.phtml") > -1))
    17.         {
    18.             if(document.body.innerHTML.indexOf("I wont take less than ") > -1)
    19.             {
    20.                 start_pos = document.body.innerHTML.indexOf("I wont take less than ") + 22;
    21.                 end_pos = document.body.innerHTML.indexOf(" Neopoints for it");
    22.                 raw_price = document.body.innerHTML.substr(start_pos,end_pos-start_pos);
    23.                 raw_price = raw_price.replace(",","");
    24.                
    25.                 document.body.innerHTML = document.body.innerHTML.replace("value=\"0\"","value=\""+haggle_price(raw_price)+"\"");  
    26.             }
    27.            
    28.             if(document.body.innerHTML.indexOf("I want at least ") > -1)
    29.             {
    30.                 start_pos = document.body.innerHTML.indexOf("I want at least ") + 16;
    31.                 end_pos = document.body.innerHTML.indexOf(" Neopoints for this great item");
    32.                 raw_price = document.body.innerHTML.substr(start_pos,end_pos-start_pos);
    33.                 raw_price = raw_price.replace(",","");
    34.        
    35.                 document.body.innerHTML = document.body.innerHTML.replace("value=\"0\"","value=\""+haggle_price(raw_price)+"\"");
    36.             }  
    37.            
    38.             allForms = document.evaluate("//form[@name='haggleform']",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
    39.            
    40.             for (var i = 0; i < allForms.snapshotLength; i++)
    41.             {
    42.                 thisForm = allForms.snapshotItem(i);
    43.                
    44.                 allImgs = document.evaluate("//input[@type='image']",document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
    45.            
    46.                 for (var i = 0; i < allImgs.snapshotLength; i++)
    47.                 {
    48.                     var image = allImgs.snapshotItem(i);
    49.                    
    50.                     if(image)
    51.                     {
    52.                         var newImg = document.createElement("img");
    53.                         newImg.src = image.src;
    54.                        
    55.                         var canvas = unsafeWindow.document.createElement("canvas");
    56.                         canvas.width = newImg.width;
    57.                         canvas.height = newImg.height;
    58.                    
    59.                         canvas.getContext("2d").drawImage(newImg, 0, 0);
    60.                        
    61.                         var image_data = canvas.getContext("2d").getImageData(0, 0, newImg.width, newImg.height);
    62.                        
    63.                         var lowy = 999;
    64.                         var lowx = 999;
    65.                         var low = 999;
    66.                        
    67.                         for (var x = 0; x < image_data.width; x++)
    68.                         {
    69.                             for (var y = 0; y < image_data.height; y++)
    70.                             {
    71.                                 var i = x*4+y*4*image_data.width;
    72.                                
    73.                                 var avg = Math.floor((image_data.data[i]+image_data.data[i+1]+image_data.data[i+2])/3);
    74.                                
    75.                                 if (avg < low)
    76.                                 {
    77.                                     low = avg;
    78.                                     lowx = x;
    79.                                     lowy = y;
    80.                                 }
    81.                             }
    82.                         }
    83.                              
    84.                         var newInput = document.createElement("input");
    85.                         newInput.type="hidden";
    86.                         newInput.name="x";
    87.                         newInput.value=lowx;
    88.                         thisForm.appendChild(newInput);
    89.                        
    90.                         var newInput = document.createElement("input");
    91.                         newInput.type="hidden";
    92.                         newInput.name="y";
    93.                         newInput.value=lowy;
    94.                         thisForm.appendChild(newInput);
    95.                        
    96.                         thisForm.submit();
    97.                     }else{
    98.                         alert("Image error");
    99.                     }
    100.                 }          
    101.             }
    102.         }
    103.     }
    104.  
    105.     function haggle_price(raw_price)
    106.     {
    107.         var iVal = new Array(2);
    108.        
    109.         iVal[0] = raw_price.substr(0,1);
    110.         iVal[1] = raw_price.substr(1,1);
    111.  
    112.         var x = 0;
    113.         var end_price = "";
    114.        
    115.         for(x=0; x<raw_price.length; x++)
    116.         {
    117.             end_price += iVal[(x%2)];
    118.         }
    119.  
    120.         return end_price;
    121.     }
    122.  
    123.     window.addEventListener('load', run_cap, false);
    I've tried it, it works very well.