[Guide]Customise and Improvise Your GreaseMonkey Scripts.

Discussion in 'Neopets Guides' started by Jubixion, Jun 13, 2009.

  1. Jubixion

    Jubixion Level II

    Joined:
    Jun 11, 2009
    Messages:
    205
    Likes Received:
    14
    This is mainly for people who uses GreaseMonkey Scripts but have totally no idea how to do things like randomising your refresh rates, making your own script that goes to a certain link and click it and stuff. I'm no professional so I would use simple codings though they aren't the best way, but it gets the job done xD

    Randomised Refresh Rates
    Here is a simple example of a typical autobuying script that you can install from userscripts.org:
    Code (Text):
    1. // ==UserScript==
    2. // @name           Autoshopgrabber #13 Pharmacy
    3. // @description       Grabs Items Quickly
    4. // @namespace      http://userscripts.org/scripts/show/46625
    5. // @include        http://www.neopets.com/objects.phtml?type=shop&obj_type=13
    6. // ==/UserScript==
    7. var x = 3000 //set the refresh rate here.
    8.  
    9. if(document.body.innerHTML.indexOf('Sporkle Syrup') != -1){
    10. var item= document.evaluate('//b[. = "Sporkle Syrup"]',document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);if (item.snapshotLength > 0){item = item.snapshotItem(0);selectedlink=item.previousSibling.previousSibling;window.location = selectedlink}return;}
    11.  
    12. else if(document.body.innerHTML.indexOf('Tasty Pie') != -1){
    13. var item= document.evaluate('//b[. = "Tasty Pie"]',document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);if (item.snapshotLength > 0){item = item.snapshotItem(0);selectedlink=item.previousSibling.previousSibling;window.location = selectedlink}return;}
    14.  
    15.  
    16. else { window.setTimeout(function(){window.location.reload() ;},x) ;}return;
    Of course I deleted the other items that it'll scan for just to save space.

    Now, these are the codes which affects an autobuying script's refresh rates:

    Code (Text):
    1. var x = 3000 //set the refresh rate here.
    2.  
    3. and
    4.  
    5. else { window.setTimeout(function(){window.location.reload() ;},x) ;}return;
    "window.setTimeout" is a so called "waiting" function in GreaseMonkey which basically waits for a certain number of milliseconds specified by the scripter before it does the function in the brackets, in this case it is:
    Code (Text):
    1. window.location.reload()
    The general format for "window.setTimeout" is like so:
    Code (Text):
    1. window.setTimeout(-your function-, -waiting time in milliseconds-);
    Now, we will improve the refresh rates for this script as it is humanly impossible to refresh continuously at exactly 3 seconds.

    You can delete off the "var x = 3000" or whatever number it is and add this on:

    Code (Text):
    1. var minTime = 3;
    2. var maxTime = 5;
    3. var refreshRate = (maxTime - minTime)*Math.random() + minTime;
    4.  
    "var" stands for variable, meaning you can change the numbers to whatever you want. Math.random() generates a random number between 0-1. Thus, refreshRate would be equal to a random number between 3-5.

    Last thing you need to do is to change the "x" in the window.setTimeout function to "refreshRate" like so:

    Code (Text):
    1. else { window.setTimeout(function(){window.location.reload() ;},x) ;}return;
    2.  
    3. change to
    4.  
    5. else { window.setTimeout(function(){window.location.reload() ;},refreshRate) ;}return;
    Restock Ban Checker
    Most or maybe all GreaseMonkey autobuying scripts do not include a restock ban checker in their coding that checks if you have been restock banned. Here I've came up with a simple way of checking whether you've been restock banned by going to the tiki tack shop and checking if there's any items in stock there.

    First, we must add in a code which checks our current shop to see if there's any items selling. If there isn't, it will travel to the tiki tack shop. If there is, it wouldn't do anything.

    Code (Text):
    1. else if(window.find("Sorry, we are sold out of everything!")) {
    2.     location.href = 'http://www.neopets.com/objects.phtml?type=shop&obj_type=21';
    3. }
    "window.find(' ')" is a very useful function when creating your own script for neopets or any other browser based games. It will search the browser for the words in between the ' '. "location.href" changes your current webpage to the one you specify. What the above script does is, if the browser finds "Sorry, we are sold out of everything!", it will browse to the tiki tack shop.

    *Reminder: Do NOT mix up single quotations ' ' with double quotations " " in programming. It can cause an entire script to not run at all.

    Now we will do the same thing for tiki tack shop. Add this in between:

    Code (Text):
    1.     if(window.find("Sorry, we are sold out of everything!")) {
    2.         alert('Restock Banned.');
    3.     }
    4.     else {
    5.         location.href = 'http://www.neopets.com/objects.phtml?type=shop&obj_type=13';
    6.     }
    7.  
    8.  
    9. So together it'll look something like this:
    10.  
    11.  
    12. else if(window.find("Sorry, we are sold out of everything!")) {
    13.     location.href = 'http://www.neopets.com/objects.phtml?type=shop&obj_type=21';
    14.     if(window.find("Sorry, we are sold out of everything!")) {
    15.         alert('Restock Banned.');
    16.     }
    17.     else {
    18.         location.href = 'http://www.neopets.com/objects.phtml?type=shop&obj_type=13';
    19.     }
    20. }  
    Thus, it'll scan for no items in the shop again. If it finds the "sold out of everything" notice, it'll give you a popup saying "Restock Banned". However, if tiki tack shop still has items, it'll travel back to the pharmacy and continue autobuying there. It might just be an occasional restock

    One last thing, for this to work, you have to add the tiki tack shop url into this script's "Included Pages". This tells the script to run in the pages specified in "Included Pages". If you do not add this, the script will just come to a stop at the tiki tack shop, not alerting or referring you back to the pharmacy. To add, go to Tools > Greasemonkey > Manage User Scripts. Find for your script name and click on it. Press "Add..." and paste the tiki tack shop url in.



    Auto Opening and Closing Tabs
    This section talks about tabs that automatically open and do their stuff, and then close again when its done. This can be used for Snowager and Deadly Dice or even your Dailies. However, you need to at least have one tab to be in a neopets page.

    First, we will improvise from a [url:http://userscripts.org/scripts/show/34084]simple snowager visiting script[/url]. All this script does is to open a tab with the snowager's webpage when it is sleeping. What we want it to do is to open the tab, click on "get prize" or what ever the button is, claim the prize, and then close the tab. Unless you want to keep it open to see what you got then you can always not add in the closeTab part later on.

    This is the main part of the script:

    Code (Text):
    1. (function recursive()
    2. {    // script scope
    3.  
    4.     var user = {
    5.         'increment':GM_getValue('increment',    0)    // milisseconds
    6.     };
    7.  
    8.     var nst = Neopets.Time();
    9.     nst.setMinutes(0,0,0);
    10.  
    11.     var h = nst.getHours();
    12.     if (GM_getValue("lastAccess","0") != nst.valueOf() && (""+h).match(/^(?:0?6|14|22)$/))
    13.     {
    14.         GM_setValue("lastAccess",""+nst.valueOf());
    15.         GM_openInTab("http://www.neopets.com/winter/snowager.phtml");
    16.     }
    17.  
    18.     var snowagerDate = new Date(nst);
    19.     snowagerDate.setHours(h+8-((2+h) % 8),0,0,0);
    20.  
    21.     setTimeout(recursive, snowagerDate.valueOf() - nst.valueOf() + user.increment);
    22. })();
    When looking at a script's code, it isn't necessary to understand every single part of the code to be able to improve it with your own additions here and there. I myself do not understand a couple of codings from above, but I know what I want to add on, and I know what codes I should not touch.

    For your info, this is the part that opens a new tab, in case you'll like to experiment with scripting yourself

    Code (Text):
    1. GM_openInTab("http://www.neopets.com/winter/snowager.phtml");
    First, we want to make it such that it claims the prize when it reaches the snowager page. Before adding the code, we must remember once again to add the snowager url into the "Included Pages" thing.

    Anyway, here's the code we will add in:

    Code (Text):
    1. if(window.find('The Snowager is currently sleeping')) {
    2.      location.href = 'http://www.neopets.com/winter/snowager2.phtml';
    3. }
    You realise that we did not have to go through the hassle to code something that actually clicks the button and such. A simple code will always suffice. All this does is scan for the sleeping text and then go to the page where the snowager would reward prize.

    If you would like to close your tab afterwards, you have to once again add the snowager2.phtml page into the "included pages" for this part of the script to work. Once you're done, add this in:

    Code (Text):
    1. if(window.find('The Snowager')) {
    2.      window.setTimeout("window.close()",6000);
    3. }
    By now you should be able to understand what this code does. The new function introduced here is "window.close()", this basically closes the tab. You may realise that in the snowager.phtml page, there, too, is a text with "The Snowager", wouldn't this script close that tab there and thus, not making it possible to collect the prize? One thing about programming is that the script runs in sequence. Since we placed the "collecting prize" code first, it will run that piece of instruction before running this. Therefore it will not close till it reaches snowager2.phtml


    Renaming Browser Title

    This is just a small little idea I had for reminders or alerts. Sometimes those popup things just irritate the crap out of me, so I would, instead of having alerts(), change my browser titles to a message. It is quite easy to see the message since no website would ever have a title such as "Change Potion" or "RS Banned" lol.

    Anyway, the code is simple.

    Code (Text):
    1. document.title = 'Your Title';
    If you wanna use this for RS Ban alerts, it'll look like this:
    Code (Text):
    1. else if(window.find("Sorry, we are sold out of everything!")) {
    2.     location.href = 'http://www.neopets.com/objects.phtml?type=shop&obj_type=21';
    3.     if(window.find("Sorry, we are sold out of everything!")) {
    4.         document.title = 'Restock Banned.';
    5.     }
    6.     else {
    7.         location.href = 'http://www.neopets.com/objects.phtml?type=shop&obj_type=13';
    8.     }
    9. }
    Clicking on Buttons
    Now, if you really have no other alternative but to have to code the script to click buttons, here is on way of doing it. This method provides a list/array(hopefully I used the term right) of links to click. Thus when the script recognises a certain link that you have added into the array, it will click on it.

    This is the general format of the function:

    Code (Text):
    1. function button(){
    2.     var script = {      
    3.      
    4.         values:[  
    5.                 '-value of the button1-';
    6.                 '-value of the button2-';
    7.                
    8.         pages:[
    9.                '-action of button1-';
    10.                '-action of button2-';
    11.                 ]
    12.     };
    13.     var i = 0;
    14.     while(i < script.values.length)
    15.     {
    16.        
    17.         var sform = document.evaluate('//form[contains(@action,"' + script.pages[i] + '")]/input[@value="'+script.values[i] +'"]',
    18.                                     document,
    19.                                     null,
    20.                                     XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    21.                                     null).snapshotItem(0);
    22.      
    23.         if(!!sform)
    24.         {  
    25.             sform = sform.form;
    26.             window.setTimeout(function(){sform.submit();},refreshRate);
    27.             break;
    28.         }
    29.         i++;
    30.     }
    31. }
    There is only one part of this script(found it on some other script I used and found it useful) which I do not understand. I know its some build-in XPath function thingy but its still slightly confusing to me though I tried to read it up. This is the part:
    Code (Text):
    1. var sform = document.evaluate('//form[contains(@action,"' + script.pages[i] + '")]/input[@value="'+script.values[i] +'"]',
    2.                                     document,
    3.                                     null,
    4.                                     XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    5.                                     null).snapshotItem(0);
    Anyway, there are three things for you to change when you use this function. Take note, this is only a FUNCTION. Used like a variable for those who do not know what I mean. For example, if I were to add that code above into a totally new javascript file and safe it, it will not do anything at all. We will have to call that function to be able for it to work its magic. Here's an example of calling a function:
    Code (Text):
    1. if(1+2=3) {
    2.      function();
    3. }
    So in layman terms, calling a function is basically using it. If it helps you understand better, think of variables. If I just wrote "var number = 1", but nowhere in my script has the term "number" in it, I would not be using the variable at all.

    So moving on, the first thing you need to know is how to find the "-button value-" and "-button action-". We'll use the Healing Springs as an example.

    Go to the Healing Springs webpage. Right click > view page source. Ctrl+F and type in "submit". You'll find a couple of codes with "submit". This is the code your looking for:

    Your "-button value-" in this case is "Heal my Pets". Your "-button action-" will be "springs.html". Remember that in the code, use single quotation marks!

    In the function I used above, you can expand the list to however long you want. Thats the function I use for my dailies script. All you need to do is find the values, actions, and also remember to paste the URLs into the "Included Pages". Very IMPORTANT.

    There is a slight problem with this function though. For some dailies, such as the geraptiku one, one of the buttons that the script has to click has the value of "Continue on, at the risk of never returning. There's no turning back." Maybe you can't see the problem now, but if I typed it into the code box...

    Code (Text):
    1. Values:[
    2.      'Continue on, at the risk of never returning.  There's no turning back.'
    3. ],
    Now you realise that the apostrophe of in "there's" cuts the value short. However, there is a way to overcome this problem. You can simply type a forward(or backward, no idea which is which) slash before the apostrophe, like this:
    Code (Text):
    1. Values:[
    2.      'Continue on, at the risk of never returning.  There\'s no turning back.'
    3. ],
    There! The value is no longer being cut off! I took AGES to find the solution for this lol.



    Anyway, this is more or less what I can think of that can help you to improve and customise your own scripts to do what you want, hopefully you guys found this useful even though it is awesomely long to read xD

    *Oh and btw, if you guys want to create your own autobuying script but can't seem to find a download for it on userscripts.org, you can first go to the stickied autobuying list thread and find the list for the shop you want, then click on the program link in my siggy and use that program to create a greasemonkey autobuying script Sadly it has been placed into Level 1 when I intended it to be in Newbies so that all can use it. You can always ask the Admin and see if he could move it :/ Also, the script produced is a typical userscript.org, no randomised refresh rates and stuff. You can follow the guide above and add it in yourself


    Please comment and point out any mistakes or improvements I can make to the guide :)
     
    3dee and (deleted member) like this.
  2. ilovevicky

    ilovevicky Level II

    Joined:
    May 26, 2009
    Messages:
    248
    Likes Received:
    7
    This is a guide i have wanted to see for a while now thanks for the time and effort :) +rep
     
  3. sasuke123456

    sasuke123456 Newbie

    Joined:
    Jul 1, 2009
    Messages:
    23
    Likes Received:
    0
    whats greasymonkey??? is it a score sender cuz if it is i dont rlly get how it works cna osmeone please help me
     
  4. Rhett

    Rhett Level IV

    Joined:
    Dec 2, 2007
    Messages:
    1,429
    Likes Received:
    76
    https://addons.mozilla.org/en-US/firefox/addon/748 --> Grease monkey (GM). GM allows you to manipulate web pages. You can give it certain commands and it does them (Like refreshing every five seconds, entering in 98% of the haggle price for auto haggling, clicking a link at a certain time, etc). Unfortunately, it isn't a score sender, but there are a lot of other very useful GM scripts, even for auto buying. Here (link) are some very useful scripts, thanks to Zer0 for finding them.
     
  5. sasuke123456

    sasuke123456 Newbie

    Joined:
    Jul 1, 2009
    Messages:
    23
    Likes Received:
    0
    o so thats what it is thnxs but i still dont rlly get how to use it T-T
     
  6. Rhett

    Rhett Level IV

    Joined:
    Dec 2, 2007
    Messages:
    1,429
    Likes Received:
    76
    First, install grease monkey --> https://addons.mozilla.org/en-US/firefox/addon/748
    Then install scripts --> viewtopic.php?f=80&t=18253 OR http://userscripts.org/scripts/search?q=neopets&x=0&y=0 I went ahead and narrowed all userscripts.org scripts to ones that work on neopets, but if you do download scripts from userscript, or anywhere else, beware of cookie grabbers. If it is a cookie grabber, when you click on the script, it should have a comment about it. CG (cookie grabber) scripts on userscript normally look something like this
    [​IMG]
    Last, enjoy. Here is a user script that plays Dice-a-Roo for you. -->Dice-A-Roo
     
  7. rustyhouse

    rustyhouse Level I

    Joined:
    Nov 11, 2009
    Messages:
    122
    Likes Received:
    1
    Great Guide; I recently got into Grease MOnkey and This really helps. Reminds of me when I first got into Visual Basics. Small question about GM Scripts: Can someone/anyone create a script to finish the Neoquest II game by itself? IS that possible or will the random factor of the game have a big role to play for individual players, IE/ Different random battles, different moves, different events within the game which are unpredictible?
     
  8. vivitarium

    vivitarium Level I

    Joined:
    Nov 20, 2009
    Messages:
    88
    Likes Received:
    0
    How long are these scripts typically usable for?

    I see that the first few posts are from awhile ago, are they still usable now? Can TNT track them, etc.?
     
  9. klover33

    klover33 Newbie

    Joined:
    Jul 23, 2010
    Messages:
    18
    Likes Received:
    0
    Great Guide! Thank you :)