- Register
Lang: [automatic], [fr], [en], … | Go to the top!
Information: Register ot log in to be able to participate to Olissea forums.
BBS List:
[JavaScript] GreaseMonkey Experience
Page 1 / 1
JeromeJ
[Avatar de JeromeJ]
posted on
10-25-2014 at 1:00 am
Légende vivante
[Answer already read]
Throughout my tedious journey with GreaseMonkey, I had to learn how to deal with some quirks the "hard-way" (By going through them painfully).

So, for you guys, I'll gather here some of the fruits of the experience I gained… Things to avoid, little advices, etc.

Note: I'm personally currently using Palemoon 25.0.1 (x64), Win7 and GreaseMonkey 0.9.22.

1) Don't use the "shortcuts" of everything that can also start with "window." in front of it

For instance, NEVER!!! use setTimeout directly!!
Always use window.setTimeout instead!!

2) Whenever in doubts, catch exceptions and log intensively

If your code suddenly doesn't work remember going through those steps:

- Live-test as much as you can in the interactive console. You might just be idiot or not familiar with JavaScript or the problem you are dealing with enough just yet.

- Test your code often, step-by-step, and log log log…
It will only make easier to track bugs down the road without having to look for a needle in a haystack

- Doesn't work anymore? Never hesitate to surround with some try-catch blocks the parts of your code you suspect they might be crashing your app as Firebug (at the very least) will stay mute as GreaseMonkey is running in another environment ^^'

3) My humble advise: Stick those at the beginning of your code

 
/** SHORTCUTS **/
// GreaseMonkey doesnt offer you the shortcuts like document.body
// so that might be handy down the line. Save yourself some time, will you?
body = document.getElementsByTagName('body')[0]
 
/** LOG CRASH-WORRIES-FREE **/
// (Not from me but adapted to work with GreaseMonkey environment)
// If Firebug isn't open and you try to log something, your app will crash silently
// This allows you to log freely without worries. Firebug isn't open, throw those logs to /dev/null!!
if(!console.log) {
// @source http://stackoverflow.com/a/11663507/1524913
// Modified by JeromeJ ( http://www.olissea.com/ ) to fit GreaseMonkey environment
console = {log: function(){}}
}


4) If you want some control, prefer a simulated setInterval than an actual one

I didn't any way (but that might be just me) to stop an interval itself within GreaseMonkey.
Simply 'cause the GreaseMonkey environment has already been all deleted by the time I try stopping my surviving interval. (Even the workarounds didn't work for me. I might be the issue here again)

So if you wish to do so, I'd advise you to simulate an interval by re-creating one using setTimeout with a named callback and just stop calling the callback over and over again when you wanna stop the interval! Finally did the trick for me!

 
// Whatever you do, DON'T FORGET the "window." in front of setTimeout :)
window.setTimeout(function myLoop() {
// Your code here…
 
// Wanna continue? Just call it again!
window.setTimeout(myLoop, 1000)
}, 1000)
 


5) When using setTimeout/setInterval, give it the callback by reference, not as a string to be eval'd

This wont work cause the reference will be wiped out with the GreaseMonkey environment
window.setTimeout('foo()', 5000)


This will, a reference to the object will be kept internally by setTimeout:
window.setTimeout(foo, 5000)


If you need the function to be called with parameters, you can use a lambda for instance:
window.setTimeout(function(){foo(33);}, 5000)


That's it for today!!
------------------------------------------
"Olissea en force | (╯°□°)╯︵ ┻━┻ Hmm… ಠ_ಠ | Vertuous circles ☺"
Page: 1
Answer this topic
Quick answer: