How to make scripts for Ogame (no skills needed)

    • Here is my English translation of the first chapter – Introduction. I'll post next chapters if I have some time to translate them.

      [name=1]
      I) Introduction
      [/name]

      What is GreaseMonkey? :?:
      GreaseMonkey is an add-on for Firefox which allow to execute JavaScript scripts on any Web page. It is downloadable here: addons.mozilla.org/en/firefox/addon/748.


      How to install a script?
      All you have to do is to click on links given by scripters on various topics in this section, or if it is a link on Userscript, to click on this button:
      Edit by Valent | Removed Imageshack Ad


      And if I do not use Firefox?
      With Google Chrome , you just have to click on installation links – no need of GreaseMonkey or an equivalent.

      With Opera , you have to download scripts and put them all in the same folder.
      To download them, click on links given by scripters, then do File > Save As – you can rename the script but do not touch the .user.js extension!
      Next, go to Tools > Preferences... > Advanced > Content > JavaScript Options... and in User JavaScript folder, select the directory in which you have downloaded your scripts.

      With Internet Explorer : [Greasemonkey] Script installation under Internet Explorer [Fr].

      NB: Every scripts work with FireFox, but a few works with other browsers. ;)

      Where can I find a list of all allowed scripts?
      Here! ;) ^^ This is not an exhaustive list, but principal scripts are there – do not hesitate to suggest a script, by respecting the template :P.

      [to=12]Scripts for the redesigned OGame.[/to]


      How to edit, desactive or uninstall a script?
      Right click on the Monkey's head, Manage scripts... then all you have to do is to select a script and do the wanted action by clicking the said button at the bottom of the window.


      How to create a script?
      It would be better to use a suitable software to create or edit scripts. Indeed, even if in theory you can use the notepad, I advise you to install Notepad++ or another similar editor. ;)

      It will be more clear. :D

      DarkFire67 wrote:

      I just want to add that it is possible to change the script editor for certain who have made the mistake of choosing a software like the notepad...

      By opening a new Firefox tab, type about:config in the address bar – promise that you will be very careful! – and search for the greasemonkey.editor keyword.

      Replace in value the current path with Notepad++'s, probably C:\Program Files\Notepad++\notepad++.exe.

      Finally, close the tab. When you choose to edit a script, GreaseMonkey will open it in Notepad++. :)


      Nextly, to create your script, right click on the monkey's head then New script. you will arrive here:
      Edit by Valent | Removed Imageshack Ad

      Fill the three first cases as you want. For the 4th, it is the Internet address(es) where your script will be executed, and the latest is where your script will not.

      You can use some * which mean "anything". For example with:

      Source Code

      1. http://*.ogame.*/game/index.php*

      the script will be executed on pages which contains http://[something].ogame.[something]/game/index.php[something]. In other words, on any OGame page on any servers.

      When this step is done, a new window is opening with Notepad++ – or choose it as default editor:
      Edit by Valent | Removed Imageshack Ad
      In green, it is informations that you have filled in – included/excluded pages, name, ...

      !!If you change these informations and you save the file, this will not affect the script functioning!!

      For example if you change the pages where the script is executed, modifications will not be taken in account. If you want that it will be the case, you have whether to upload the script and download it or whether to do Manage scripts... and edit pages via this menu.

      You code should be under this green code, where I have written:

      Source Code

      1. alert('test de fonctionnement');


      The alert function is the function which create pop-ups. Roughly, this script send a pop-up on every where it is executed:
      Edit by Valent | Removed Imageshack Ad

      It is a good way to check if the script is well executed where it should be. ;)
      Do not forget, test your code snippets as often as possible, in order to know quickly where errors occur if the script does not work, that happens all the time. :lol: And if it is buggy since the 2nd line and you have made 50 lines after which also contain errors, what a pain! It is better to solve errors gradually.


      [name=2]
      II) Variables
      [/name]

      Well, now let's talk about the most interesting: the coding. :)

      First of all, how to declare these variables? It is quite simple, all you have to do is put "var" before the name of your variable to create it:

      Source Code

      1. var nickname;

      Here, you are creating a variable called "nickname" which contains... nothing. ^^

      In order to give it a value, you have to use the "=" sign:

      Source Code

      1. var nickname;
      2. nickname = 'Vulca';
      3. alert(nickname);

      Here, you create the "nickname" variable, you assign it the "Vulca" value, and you display it in a pop-up – thanks to the alert() function.

      !!Look out for ";" after each instructions, they are obligatory!!

      Note also how apostrophes are used: what it is between them – displayed in light gray on Notepad++ – is text, everything outside them are variables.


      For example, if I have written:

      Source Code

      1. nickname = Vulca;

      The nickname variable will not contain "Vulca" as before, but the value of the variable called Vulca – which is not defined yet, so it will crash.

      And if I write:

      Source Code

      1. alert('nickname');

      The pop-up will not display "Vulca", but simply "nickname" – as it is between apostrophes, it is considered as text, and not like a variable.

      Do not hesitate to abuse of the copy-paste for your variable names, just a misplaced letter or a wrong case will break the script. ;) And use also explicit variable names to find your way around well.


      There is roughly 3 types of variables:
      Edit by Valent | Removed Imageshack Ad

      How to put variables end-to-end? For example, here is what you have:

      Source Code

      1. var hello = 'Hello';
      2. var howRU = ', how are you?';

      To get the whole sentence, you will do:

      Source Code

      1. var sentence = hello + howRU;


      How to append some text between both variables? In the same way:

      Source Code

      1. var sentence = hello + ' Vulca' + howRU;


      Well, I think that we have examinated variables from all sides.


      [name=3]
      III) Retrieve informations on OGame pages
      [/name]

      Now, we will see how to retrieve informations on OGame's pages in order to fill our variables. :) To do so, you have to take a look at the source code of the page. I advise you to install the Firebug add-on if you are using Firefox. It will allow you to easily navigate in the source code of web pages.

      We will try to find the nickname of player who is using the script – basically, yours nickname ^^. Go to an OGame page, then click on the Firebug's icon – near to the monkey's head of GreaseMonkey.
      Edit by Valent | Removed Imageshack Ad

      Then we explore the page by click on "+" icons. By hovering names in front of "+" icons with the mouse, Firebug highlights what it matches on the page, so you just have to click on what highlights the nickname – located at the top left of the page.

      We come to that point:
      Edit by Valent | Removed Imageshack Ad

      So the nickname is in a div that has "playerName" as id. How to retrieve it? With the document.getElementById() function:

      Source Code

      1. var nickname = document.getElementById("playerName").innerHTML;
      2. alert(nickname);

      What does it mean? We retrieve the element which has "playerName" as id, and we take its HTML content – .innerHTML.

      Here is what it should display:
      joueur: <span class="textBeefy">vulca</span>


      We are almost there. :) We see that the nickname is in the span with "textBeefy" as class. Here, we have 2 solutions:
      Edit by Valent | Removed Imageshack Ad

      Display Spoiler

      Source Code

      1. var nickname = document.getElementById("playerName").getElementsByTagName('span')[0].innerHTML;
      2. var nickname = document.getElementById("playerName").getElementsByClassName('textBeefy')[0].innerHTML;


      getElementsByTagName('TAG'): Retrieve each tags which are called "TAG", it's span tags here – but it work with any tag: body, strong, th, td...

      getElementsByClassName('Class'): Retrieve each tags which has "Class" as class, here it is "textBeefy".

      getElementsByTagName and getElementsByClassName work as getElementById EXCEPT that on a page, it is possible to use several duplicated tags and classes... So these functions return an array.

      !!It is necessary to put .innerHTML only at the end during a sequence of functions!! (NDT: WTF?!)

      By writing getElementsByClassName('textBeefy')[0], we take the content of the first tag with the "textBeefy" class – which is included in the tag with the "playerName" id – array's rows are numbered starting from 0.

      If the wanted information is in the third tag with a "erkkk" class, we will do:
      document.getElementsByClassName("erkkk")[2];

      Another example, if we want to retrieve the energy, we see that it is located in a tag with "resources_energy" as id:

      Source Code

      1. var energy = document.getElementById("resources_energy").innerHTML;
      2. alert(energy);



      Now, we complicate a little. :D We want to retrieve the maximal temperature.
      (EDIT: This code no longer work since OGame v1.2.4.)

      After a quick search, we can see that it is located in a tag with "temperatureContentField" as id, so:

      Source Code

      1. var temperature = document.getElementById("temperatureContentField").innerHTML;
      2. alert(temperature);


      But here is what it shows: :o
      about 57 °C to 97°C


      The temperature is not alone is a tag... We need to sort out. :mrgreen:

      Source Code

      1. var temperatureSplit = temperature.split('°C');


      The split() function is used for splitting character strings, here I split each times that there is "°C". So the temperatureSplit variable will contain 3 pieces of sentences – it is an array, like with getElements...
      temperatureSplit[0] will contain "about 57 ",
      temperatureSplit[1] will content " to 97" and
      temperatureSplit[2] will contain "" – because of the last "°C".

      We are almost there, there is just to remove " to ". The simpliest way is to replace " to " with nothing:

      Source Code

      1. var temperature = temperatureSplit[1].replace(' to ', '');

      Or more generally, we replace everything that it is not a number with nothing:

      Source Code

      1. var temperature = temperatureSplit[1].replace( /[^0-9-]/g, "");


      (Do not ask too much questions about the latest line, it is not really the purpose of this tutorial. ^^ Fetch more informations about RegEx on Google if you want to go into detail. ;))

      Which gives:
      Edit by Valent | Removed Imageshack Ad

      You should also know that your scripts can use InfoCompte script variables with the new version – and SSU script variable with the old version – in order to make your life simpler. ;)

      [Greasemonkey] InfoCompte [SSU] - Discover your account composition and your progression [Compatible with OGame Redesign] – cf. the spoiler.
      [Greasemonkey] Seyguai's Script Utilities - OGame Librairie

      The post was edited 2 times, last by cedricpc: The last part of the chapter 3 – ~40 % translated. ().