[Javascript][source]Neopets Dailies

Discussion in 'Code Snippets and Tutorials' started by Siren, Jun 8, 2013.

  1. Siren

    Siren Level I

    Joined:
    Jun 27, 2011
    Messages:
    81
    Likes Received:
    2
    Userscript (Greasemonkey + Tampermonkey compatible) that appends GUI to footer, automatically does a "daily" preference'd by the user.

    I hope this helps anyone brand new to javascript :)

    Code (Javascript):
    1. // ==UserScript==
    2. // @name       Neopets Dailies
    3. // @namespace  neofriends.net
    4. // @version    0.2
    5. // @description  Daily helper for neopets
    6. // @include    http://www.neopets.com/*
    7. // @exclude    http://www.neopets.com/winter/igloo*
    8. // @match      http://www.neopets.com/bank.phtml
    9. // @match      http://www.neopets.com/games/
    10. // @match      http://www.neopets.com/prehistoric/omelette.phtml
    11. // @match      http://www.neopets.com/jelly/jelly.phtml
    12. // @require    http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js
    13. // @grant          GM_getValue
    14. // @grant          GM_log
    15. // @grant          GM_openInTab
    16. // @grant          GM_registerMenuCommand
    17. // @grant          GM_setValue
    18. // @copyright  Carter 'Siren' June 2013
    19. // ==/UserScript==
    20. //VARS
    21. var randNumMin = GM_getValue("mindelay"); //min in seconds
    22. var randNumMax = GM_getValue("maxdelay"); //max in seconds
    23.  
    24. var randdelay = (Math.random() * (randNumMax - randNumMin + 1) + randNumMin) * 1000; //in ms, not floor'd
    25. var shares = 1000;
    26. var choice;
    27.  
    28.  
    29. //checks
    30. console.log(shares + " : shares");
    31. console.log(randdelay + " : randdelay on start");
    32.  
    33. //remove #footer's background image for settings
    34. $("#footer").css('background-image', 'none');
    35. $("#footer").css('background-color', 'rgb(207, 201, 47)');
    36. $("#footer").css('height', '300px');
    37. $("#footer").css('width', '700px');
    38. $("#footer").css('text-align', 'center');
    39.  
    40. //GUI
    41. document.getElementById('footer').innerHTML = "<html><script type='text/javascript'>var saveopts = function(){var choice;if(document.getElementById('bankintcheckbox').checked==true){choice=true}else{choice=false}GM_setValue('dobank',choice);console.log(GM_getValue('dobank') + ' : gm_getvalue of dobank');}</script>"
    42. + "<body><center style=''><h2>Carter 'Siren' Daily Doer</h2></center><p>"
    43. + "Minimum Delay: <input type='text' id='txtmindelay' size='1'> seconds<br>Maximum Delay: <input type='text' id='txtmaxdelay' size='1'> seconds<br>"
    44. + "<span style=';'>collect bank interest: </span><input name='bankcheckbox' id='bankintcheckbox' type='checkbox' value='bankcheckbox'><br><span style=''>do stock:</span><input name='stockcheckbox' id='stockcheckbox' type='checkbox' value='stockcheckbox'>"
    45. + "<br><span style=''>do jelly:</span><input name='jellycb' id='jellycb' type='checkbox' value='jellycb'>"
    46. + "<br><span style=''>do tomb:</span><input name='tombcb' id='tombcb' type='checkbox' value='tombcb'>"
    47. + "<br><span style=''>do tdmbgpop (plushie):</span><input name='plushiecb' id='plushiecb' type='checkbox' value='plushiecb'>"
    48. + "<br><span style=''>do tombola:</span><input name='tombolacb' id='tombolacb' type='checkbox' value='tombolacb'>"
    49. + "<br><span style=''>do shop of offers:</span><input name='slorgcb' id='slorgcb' type='checkbox' value='slorgcb'>"
    50. + "</p><p></p><input name='save' id='savebtn' type='button' value='save preferences'>"
    51. + "<input name='goomelette' id='goomelette' type='button' value='Go To Omelette'>"
    52. + "<input name='gotomb' id='gotomb' type='button' value='Go To Deserted Tomb'>"
    53. + "<input name='gobank' id='gobank' type='button' value='Go To Bank'>"
    54. + "<input name='gojelly' id='gojelly' type='button' value='Go To Jelly'>"
    55. + "<input name='gostock' id='gostock' type='button' value='Go To Stock Market'>"
    56. + "<input name='goplushie' id='goplushie' type='button' value='Go To Blue Plushie'>"
    57. + "<input name='goslorg' id='goslorg' type='button' value='Go To Shop Of Offers (slorg)'>"
    58. + "<input name='gotombola' id='gotombola' type='button' value='Go To Tombola'>"
    59. + "<input name='checkgmval' id='checkgmval' type='button' value='Check Current Preferences (console logs)'>"
    60. + "<br><i>Instructions: all preferences are saved across page loads; you don't need to reset them every time you go to a new page. In order to change them, check/uncheck each checkbox and add numbers for new min/max delay on pages. (default min/max are 1,3 seconds)</i>"
    61. + "</body></html>";
    62.  
    63. if (GM_getValue("doslorg") == "undefined"){
    64.     GM_setValue("doslorg", true);
    65. }
    66.  
    67. document.getElementById('txtmindelay').value = GM_getValue("mindelay");
    68.  
    69. if(document.getElementById('txtmindelay').value == "NaN"){
    70.     GM_setValue("mindelay", 1); //default: 1 second min
    71.     document.getElementById('txtmindelay').value = parseInt(GM_getValue("mindelay"));
    72. }
    73.  
    74. document.getElementById('txtmaxdelay').value = GM_getValue('maxdelay');
    75.  
    76. if(document.getElementById('txtmaxdelay').value == "NaN"){
    77.     GM_setValue("maxdelay", 3); //default: 3 seconds max
    78.     document.getElementById('txtmaxdelay').value = parseInt(GM_getValue("maxdelay"));
    79. }
    80.  
    81. //BANK interest
    82. //location of submit: document.getElementsByClassName('content')[0].getElementsByTagName('table')[7].getElementsByTagName('tbody')[0].getElementsByTagName('tr')[0].getElementsByTagName('td')[0].getElementsByTagName('table')[0]
    83. if (document.location.href == "http://www.neopets.com/bank.phtml" && GM_getValue("dobank") == true) {
    84.     window.setTimeout("document.forms[3].submit();", randdelay);
    85. }
    86.  
    87. //TOMBOLA
    88. if ((document.location.href == "http://www.neopets.com/island/tombola.phtml" || document.location.href == "http://www.neopets.com/island/tombola2.phtml") && GM_getValue("dotombola") == true){
    89.     window.setTimeout("document.forms[1].submit();", randdelay);
    90. }
    91.  
    92.  
    93. //OMELETTE
    94. //location of submit button: [@id="content"]/table/tbody/tr/td[2]/center[2]/form/input[2]
    95. if (document.location.href == "http://www.neopets.com/prehistoric/omelette.phtml" && GM_getValue("doomelette") == true){
    96.     setTimeout("document.forms[1].submit();", randdelay); //grab
    97. }
    98. if (document.evaluate("//*[@id='content']/table/tbody/tr/td[2]/center/p[1]/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue == " <b>You approach the massive omelette...</b>"){
    99.     console.log("omelette content value found");
    100.     setTimeout("document.forms[1].submit();", randdelay); //leave
    101. }
    102.  
    103. //*[@id="content"]/table/tbody/tr/td[2]/center/p[1]/b   = xpath for "you approach.."
    104.  
    105.  
    106. //JELLY
    107. if ((document.location.href == "http://www.neopets.com/jelly/jelly.phtml?" || document.location.href == "http://www.neopets.com/jelly/jelly.phtml?") && GM_getValue("dojelly") == true){
    108.     setTimeout("document.forms[1].submit();", randdelay); //document.forms[1] becomes the return submit form. no need for return line
    109. }
    110.  
    111.  
    112.  
    113. //TOMB
    114. if (document.location.href == "http://www.neopets.com/worlds/geraptiku/tomb.phtml" && GM_getValue("dotomb") == true){
    115.     setTimeout("document.forms[1].submit();", randdelay);
    116. }
    117. if (document.location.href == "http://www.neopets.com/worlds/geraptiku/process_tomb.phtml" && GM_getValue("dotomb") == true){
    118.     setTimeout("document.forms[1].submit();", randdelay);
    119. }
    120.  
    121.  
    122. //FRUIT MACHINE
    123. if (document.location.href == "http://www.neopets.com/desert/fruit/index.phtml" && GM_getValue("dofruit") == true){
    124.     document.forms[1].submit();
    125.     setTimeout("document.forms[1].submit()", 10000); //the animation has to complete. NOT YET FINISHED
    126. }
    127.  
    128.  
    129. //blue PLUSHIE thing
    130. if (document.location.href == "http://www.neopets.com/faerieland/tdmbgpop.phtml" && GM_getValue("doplushie") == true){
    131.     setTimeout("document.forms[1].submit();", randdelay); //"talk to plushie" and return to world
    132. }
    133.  
    134.  
    135.  
    136. //STOCK, much thanks to Heya
    137. if (document.location.href == "http://www.neopets.com/stockmarket.phtml?type=list&full=true" && GM_getValue("dostock") == true) {
    138.     for(i=1;i<42; i++) {
    139.         if(document.getElementsByClassName('content')[0].getElementsByTagName('table')[0].getElementsByTagName('tr')[i].getElementsByTagName('td')[5].innerHTML == '<b>15</b>') {
    140.             setTimeout("document.location.href = document.getElementsByClassName('content')[0].getElementsByTagName('table')[0].getElementsByTagName('tr')[i].getElementsByTagName('a')[0].href;", randdelay);
    141.         }
    142.     }
    143. } else if (document.location.href.match('type=profile&company_id=')) {
    144.     setTimeout("document.location.href = document.getElementsByClassName('content')[0].getElementsByTagName('table')[0].getElementsByTagName('a')[0].href;", randdelay);
    145. } else if (document.location.href.match('type=buy&ticker=')) {
    146.     document.getElementsByName('amount_shares')[0].value = shares;
    147.     setTimeout("document.forms[1].submit();", randdelay);
    148. }
    149.  
    150.  
    151. //go-to-location btn event listeners
    152. document.getElementById('goplushie').addEventListener('click', function(event){
    153.         document.location.href = "http://www.neopets.com/faerieland/tdmbgpop.phtml"
    154.     },true)
    155.  
    156. document.getElementById('gotomb').addEventListener('click', function(event){
    157.         document.location.href = "http://www.neopets.com/worlds/geraptiku/tomb.phtml"
    158.     },true)
    159.  
    160.  
    161. document.getElementById('goomelette').addEventListener('click', function(event){
    162.      document.location.href = "http://www.neopets.com/faerieland/tdmbgpop.phtml"
    163. },true)
    164.  
    165. document.getElementById('gostock').addEventListener('click', function(event){
    166.         document.location.href = "http://www.neopets.com/stockmarket.phtml?type=list&full=true"
    167.     },true)
    168.  
    169. document.getElementById('gobank').addEventListener('click', function(event){
    170.         document.location.href = "http://www.neopets.com/bank.phtml"
    171.     },true)
    172.  
    173. document.getElementById('gojelly').addEventListener('click', function(event){
    174.         document.location.href = "http://www.neopets.com/jelly/jelly.phtml"
    175.     },true)
    176.  
    177. document.getElementById('gotombola').addEventListener('click', function(event){
    178.     document.location.href = "http://www.neopets.com/island/tombola.phtml"
    179. }, true)
    180.  
    181. document.getElementById('goslorg').addEventListener('click', function(event){
    182.     document.location.href = "http://www.neopets.com/shop_of_offers.phtml?slorg_payout=yes"
    183. }, true)
    184.  
    185. document.getElementById('checkgmval').addEventListener('click', function(event){
    186.     console.log("CURRENT PREFERENCES: ");
    187.     console.log(GM_getValue("dostock") + " : dostock");
    188.     console.log(GM_getValue("dobank") + " : dobank");
    189.     console.log(GM_getValue("doomelette") + " : doomelette");
    190.     console.log(GM_getValue("dojelly") + " : dojelly");
    191.     console.log(GM_getValue("dotomb") + " : dotomb");
    192.     console.log(GM_getValue("dotombola") + " : dotombola");
    193.     console.log(GM_getValue("doslorg") + " :doslorg");
    194.                          
    195. }, true)
    196.  
    197. //SAVEBTN.click
    198.     document.getElementById('savebtn').addEventListener( 'click', function( event ) {
    199.         console.log("SAVED PREFERENCES: ");
    200.         if (document.getElementById('stockcheckbox').checked == true){
    201.             GM_setValue("dostock", true);
    202.          
    203.         } else {
    204.             GM_setValue("dostock", false);
    205.         }
    206.         console.log(GM_getValue("dostock") + " : dostock");
    207.      
    208.         if ( document.getElementById('bankintcheckbox').checked == true ) {
    209.             GM_setValue("dobank", true);
    210.         } else {
    211.             GM_setValue("dobank", false);
    212.         }
    213.         console.log(GM_getValue("dobank") + " : dobank");
    214.      
    215.         if (document.getElementById('jellycb').checked == true) {
    216.             GM_setValue("dojelly", true);
    217.         } else {
    218.             GM_setValue("dojelly", false);
    219.         }
    220.         console.log(GM_getValue("dojelly") + " : dojelly");
    221.      
    222.         if (document.getElementById('tombcb').checked == true) {
    223.             GM_setValue("dotomb", true);
    224.         } else {
    225.             GM_setValue("dotomb", false);
    226.         }
    227.         console.log(GM_getValue("dotomb") + " : dotomb");
    228.      
    229.         if (document.getElementById('plushiecb').checked == true) {
    230.             GM_setValue("doplushie", true);
    231.         } else {
    232.             GM_setValue("doplushie", false);
    233.         }
    234.         console.log(GM_getValue("doplushie") + " : doplushie");
    235.      
    236.         if (document.getElementById('tombolacb').checked == true){
    237.             GM_setValue("dotombola", true);
    238.         } else {
    239.             GM_setValue("dotombola", false);
    240.         }
    241.         console.log(GM_getValue("dotombola") + " : dotombola");
    242.      
    243.         if (document.getElementById('slorgcb').checked == true){
    244.             GM_setValue("doslorg", true);
    245.         } else {
    246.             GM_setValue("doslorg", false);
    247.         }
    248.         console.log(GM_getValue("doslorg") + " : doslorg");
    249.      
    250.         GM_setValue("mindelay", parseInt(document.getElementById('txtmindelay').value));
    251.         console.log(GM_getValue("mindelay") + " : mindelay");
    252.      
    253.         GM_setValue("maxdelay", parseInt(document.getElementById('txtmaxdelay').value));
    254.         console.log(GM_getValue("maxdelay") + " : maxdelay");
    255.      
    256.     }, true );
    257.  
     
    Lightning likes this.
  2. Lightning

    Lightning Administrator
    Staff Member

    Joined:
    Nov 8, 2008
    Messages:
    3,021
    Likes Received:
    195
    Gender:
    Male
    Location:
    Florida, USA
    Nice release man! +rep

    So you were able to figure out how to fix the problem you had?
     
  3. Siren

    Siren Level I

    Joined:
    Jun 27, 2011
    Messages:
    81
    Likes Received:
    2

    I spent more time researching it and understanding the aspects of it.

    Sadly, there is no function to serialize nodes of XPath :-(
    Thus, if I get <ObjectHTML...>, I can not extract the "innerHTML" (XML) of it.

    Would have been devilishly amazing if that was a function :-\

    There is supposedly a collection of functions meant to deal with XML conversions -- hopefully including from xpath -- called XSLT. I'll look into it later to see if I can find a solution. If I do find a (working) solution, I'll post an explanation for others :D

    I was reading about the XSL functions here (I hope external link is allowed; it's too w3schools)
     
  4. fail

    fail Level IV

    Joined:
    Aug 31, 2007
    Messages:
    1,676
    Likes Received:
    32
    anyone wanna help me learn java?