Greasemonkey is a great Firefox add-on that allows you to add custom JavaScript to any web page. This simple concept allows you to do extraordinary changes to a web page and you can find a lot of pre-made scripts around. But if you want to do your own scripts, there's a few things that you should know before you start...
Registering events
Let's say you want to catch a button click. Here's how to do it:
var button = document.getElementById(buttonId);
button.addEventListener('click', myFunction, false);
Don't do stuff like button.onclick = "myFunction()" or even button.onclick = myFunction. It won't work.
Accessing element properties
Suppose you want to add some information to a link that you created to use during the onclick event. You must do it with:
lnk.setAttribute("attrName", value);
and fetch it with:
lnk.getAttribute("attrName");
Doing lnk.attrName or lnk["attrName"] will not work, and will return null when you access it.
Parameters in Event Handlers
Let's say you registered an event like mentioned above. The event always receives a MouseEvent argument, so you should declare the function like myFunction(evt). This is important because if you by accident register as an event handler a function that takes a boolean paramenter, like myFunction(flag), flag will always be "true" when called from the event.
Stopping the bubble
Assuming you defined your function like mentioned earlier, myFunction(evt), you can stop the event from bubbling by calling
evt.preventDefault();
Testing functions
Because Firebug is such a great tool to debug your scripts, you may want to check for Grasemonkey functions before you use them. Do it with something like:
if (typeof(GM_getValue) != "undefined")
And you should be able to run the script outside Greasemonkey for easier debugging.
More tips
Well, so far these were the problems I faced! :-) I'll update this as I keep discovering Greasemonkey's idiosyncrasies.
Showing posts with label firefox. Show all posts
Showing posts with label firefox. Show all posts
Thursday, November 22, 2007
Friday, August 24, 2007
Internet privacy with Firefox
If you don't want your girlfriend to know what you're watching online, you can use Firefox profiles to clear your footsteps. Here's how:
Start by printing this page, because you'll have to close Firefox in order for this to work. Next, launch Firefox with the -p option. If you're in Ubuntu, you can do this by pressing Alt-F2 and typing firefox -p at the "Run Application" window.
The profiles window will appear with only one profile called "default". Create a new one (let's call it private), select it and start Firefox. Next, go to "Edit", "Preferences" and in the "Privacy" tab untick all the "History" options. Tick the "Always clear my private data when I close Firefox", click the "Settings" button and tick all the boxes. Your private profile is now ready!
Now close Firefox and open it again with the -p option. Select the "default" profile and set the "don't ask me on startup" option. Run Firefox and make sure you have the old profile back (with history and all). Of course I'm assuming that's what you want by default.
From now on, each time you want to navigate to websites you don't want your girlfriend to know about (say, because you want to surprise her with a valentines gift), just execute Firefox by typing firefox -p private.
Start by printing this page, because you'll have to close Firefox in order for this to work. Next, launch Firefox with the -p option. If you're in Ubuntu, you can do this by pressing Alt-F2 and typing firefox -p at the "Run Application" window.
The profiles window will appear with only one profile called "default". Create a new one (let's call it private), select it and start Firefox. Next, go to "Edit", "Preferences" and in the "Privacy" tab untick all the "History" options. Tick the "Always clear my private data when I close Firefox", click the "Settings" button and tick all the boxes. Your private profile is now ready!
Now close Firefox and open it again with the -p option. Select the "default" profile and set the "don't ask me on startup" option. Run Firefox and make sure you have the old profile back (with history and all). Of course I'm assuming that's what you want by default.
From now on, each time you want to navigate to websites you don't want your girlfriend to know about (say, because you want to surprise her with a valentines gift), just execute Firefox by typing firefox -p private.
Subscribe to:
Posts (Atom)