I have written a userscript, and run it in Greasemonkey 0.9.13.
If I remove the only try... catch block (line 54-66) but keep the code inside it, it will throw exceptions like below:
uncaught exception: [Exception… “Operation is not supported” code:
“9” nsresult: “0x80530009 (NS_ERROR_DOM_NOT_SUPPORTED_ERR)” location:
“resource://greasemonkey/runScript.js Line: 29”]
However, stripped scripts run well in Chrome w/o any problems.
However, it will NOT print anything into console, which means codes in try block don’t throw exception if wrapped by try block.
Can you guys tell me why it works?
This is the code snippet from that linked source:
var streamItems = $('div.main-content div.stream-item');
var streamItemsLength = streamItems.length;
var innerHeight = window.innerHeight;
var scrollY = window.scrollY;
var y = scrollY + innerHeight;
var tweet;
var tweetHeight = 0;
try {
for (var sumHeight = getHeaderHeight(), num = 0; sumHeight < y; num++, sumHeight += tweetHeight) {
tweet = streamItems[num];
tweetHeight = getHeight(tweet);
if (tweetHeight == 0) {
removeClass(tweet, 'hidden-tweet');
tweetHeight = getHeight(tweet);
}
}
}
catch (e) {
console.log(e.stack);
}
That script needs the
tryblock (as you can see) because the logic of theforloop is very poor (it will often overrun thestreamItemsarray and probably has a race condition when removing thehidden-tweetclass).You say it doesn’t throw exceptions in Chrome, but it looks like this is just happenstance. With the right conditions, it should fail in Chrome too — or maybe Chrome’s node manipulation is just enough different. It doesn’t matter, the code is poor.
As for it not printing anything to the console, are you sure? It does in my tests.
But sometimes between anonymous function wrappers and event listeners, maybe the error
stackmight appear to be empty.