A Lubemonkey example

Lets get started right away,
very straight forward and the same as greasemonkey:

// coding: utf-8
// ==UserScript==
// @name        An example
// @namespace   www.gamecore.org
//
@version 1
// @author GBoz
// @description The simple example
// @include    http://*.gamecore.org/*
//
==/UserScript==

Now here is a trick to check on what we are running, is it Lubemonkey or Greasemonkey?
If its greasemonkey this sample will offer a download:

//greasemonkey need lubrication
try{
     //The function below does not exist on Greasemonkey so this will fail on it
     GM_dbbegin();//always use transactions in your scripts , its faster and data has more consistency
     //best practice is to use GM_dbbegin() here, that way we check for Lubemonkey and also start
     // a transaction in one step
} catch(e){
    if (confirm('This script only works with Lube Monkey\n Would you like to download Lube Monkey now?')) {
location.href = 'http://www.gamecore.org/sites/default/files/lubemonkey-0.9.15-fx.xpi';
    }
}

OK, we got this far lets get down to business.
This sample function will check if you have already run and initialized the Lubemonkey DB:

function CheckInstall(){
    try {
        var inst=GM_dbselect("select value from vars where name='Installed'")[0];
    } catch(e){
       //if we get here it means that our db is not initialized so,
       //there are 2 ways:
       //add a resource with the db installation like here
       // @resource   dbinstall   http://....../dbinstall.sql //-- this goes at the beginning of the script, the userscript section
       //GM_dbsimplesql(GM_getResourceText('dbinstall')); //this could be used here if we use a db install script
       //method 2 is use plain sql like here
       GM_dbexec('DROP TABLE IF EXISTS "vars"'); //remove old table if it exists
       GM_dbexec('CREATE TABLE "vars" ("name" VARCHAR PRIMARY KEY  NOT NULL , "value" TEXT)');      
       GM_dbexec('INSERT INTO "vars" VALUES(\'Installed\',\'1\')');
       //our first custom table with a primary key, we will use it later
       GM_dbexec('CREATE TABLE `fun` (`id` INTEGER NOT NULL,`name` varchar NOT NULL,PRIMARY KEY (`id`))');
    }
    if (inst){
        //alert(inst['value']);
    }

}

Of course you could create a lot of tables, in fact as many as you like but you should always consider that a script has its own database to be maintained.
This means that if you change and evolve you script in the future you have to somehow keep track of what table structure is used in the installed database version of the user.

Thats why the above used variable named "Installed" has a value of 1, marking the version of the database just installed on user-space. In the future if you release a new script which has a different table structure you may check for this variables value and act accordingly(alter tables, add rows or update etc).

Keep in mind that if the user reinstalls the script this does not mean that the database will be deleted, in fact the database may be deleted only if Firefox is uninstalled and the users .mozilla directory is removed.

Lets do something useful now, we will store some data using the "fun" table:

//lets call checkinstall first to initialize everything
CheckInstall();
try {
GM_dbexec('INSERT INTO "fun" VALUES('+Math.floor(Math.random()*5)+',\'Lubemonkey\')');
} catch(e){
   alert(e); //you will get here if you get an sql error, eventually if you reload the page a couple of times the script will try to insert the same value twice
   //violating the primary key, hence the query will fail
}
//lets write a select statement now to count the rows on the fun table
var result=GM_dbselect("select count(*) cnt from fun");
if (result.length) //a nice way to check if we have any rows or a data result in general
{
  alert('Number of rows in fun table: '+result[0]['cnt']); //throw out the column value of column "cnt" on row 0 (zero=first row)
}
GM_dbcommit();//always commit at the end of the script or the db will get unresponsive and the data corrupted
//you should also do some error checking during the script

Pretty straightforward isn't it. I know that there is room for improvements but once you get the hang of it
you will understand why I did it this way.

A very nice SQLite manager for Firefox is located here.

So you can manage and edit data on your tables. The Sqlite file is located in the top directory of the greasemonkey scripts directory, the same directory that holds the other ".sqlite" files of firefox (Usually located under document setting/application data/mozilla/firefox/profiles/??????????? and on linux under the users ".mozilla" dir.
And for Mac, well... I do not care about mac :p

You may download the example script at our script archive by clicking here.