[Javascript/GM] Neopets Shop OCR

Discussion in 'Code Snippets and Tutorials' started by Belaarx, Apr 12, 2009.

  1. Belaarx

    Belaarx Level I

    Joined:
    Sep 23, 2008
    Messages:
    135
    Likes Received:
    16
    Location:
    San Diego
    Don't ask where I got this...
    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);
     
    tharoux likes this.
  2. interhacker

    interhacker Level IV

    Joined:
    Jul 24, 2007
    Messages:
    1,637
    Likes Received:
    0
    Thats from "neocodex", laserwave lives up the road from me, my my your in trouble for this :)
     
  3. SoC

    SoC Moderator
    Staff Member

    Joined:
    Jan 24, 2007
    Messages:
    4,551
    Likes Received:
    105
    Location:
    Maaaaaaanchester!
    That was obvious who's it was, and where he got it from
    and he acknowledged that it wasn't his with the "don't ask where i got this from"

    he hasn't tried to claim credit for it, but i'll see what admins say :)
     
  4. interhacker

    interhacker Level IV

    Joined:
    Jul 24, 2007
    Messages:
    1,637
    Likes Received:
    0
    I dont care either way :) i hate him i got banned from neocodex for life for leaking his ip address + a cake load more.
     
  5. Rider

    Rider Level IV

    Joined:
    Jun 25, 2008
    Messages:
    941
    Likes Received:
    72
    Location:
    Canada =D
    BTW the forum link is included in the script. So a mod/admin might want to take that part out.
     
  6. Brian

    Brian Level I

    Joined:
    Apr 14, 2009
    Messages:
    92
    Likes Received:
    8
    Location:
    Daytona Beach, FL
    lol giving credit to this guy?
    I am sure that laserwave just got the source from a non-neopets related script posted on userscripts. I could even pull the source for the ocr if you want...
     
  7. Anonymous

    Anonymous Guest

    Quick question, does this only auto haggle, or will it click the pet as well?
     
  8. Brian

    Brian Level I

    Joined:
    Apr 14, 2009
    Messages:
    92
    Likes Received:
    8
    Location:
    Daytona Beach, FL
    yes, there is an auto haggle feature.
    yes, it does click the pet.