Update
The root cause appears to be a bug in Greasemonkey. Essentially, GM would start running in the main document space (window.self == window.top), but never finish!
Instead, GM would switch over to an iframe, on the select Google pages, and finish there. This does not seem to be 2 instances of GM, otherwise there would be no problem. After the iframe switch, the GM instance still sees the same state of global variables that were set before the switch. But any attempts to alter those globals (needed for state control) results in a silent GM-instance crash.
Furthermore, Google has, for now, changed their pages, so I can no longer duplicate the problem or test my workaround. I have a to-do item to generate my own trigger page but that is now very low priority.
.
Original Post
Hi, I have a Greasemonkey script that processes Google search results. But it's failing in a few instances, when xpath searches (and document body) appear to be empty.
Running the code in Firebug's console works every time. It only fails in a Greasemonkey script. Greasemonkey sees an empty document.body.
I've boiled the problem down to a test, greasemonkey script, below.
I'm using Firefox 3.5.9 and Greasemonkey 0.8.20100408.6 (but earlier versions had the same problem).
Problem:
Greasemonkey sees an empty document.body.
Recipe to Duplicate:
- Install the Greasemonkey script.
- Open a new tab or window.
- Navigate to Google.com (http://www.google.com/).
- Search on a simple term like "cats".
- Check Firefox's Error console (Ctrl-shift-J) or Firebug's console. The script will report that document body is empty.
- Hit refresh. The script will show a good result (document body found).
- Note that the failure only reliably appears on Google results obtained this way, and on a new tab/window.
- Turn javascript off globally (javascript.enabled set to false in about:config).
- Repeat steps 2 thru 5. Only now the Greasemonkey script will work.
It seems that Google javascript is killing the DOM tree for greasemonkey, somehow. I've tried a time-delayed retest and even a programmatic refresh; the script still fails to see the document body.
Test Script:
//
// ==UserScript==
// @name TROUBLESHOOTING 2 snippets
// @namespace http://www.google.com/
// @description For code that has funky misfires and defies standard debugging.
// @include http://*/*
// ==/UserScript==
//
function LocalMain (sTitle)
{
var sUserMessage = '';
//var sRawHtml = unsafeWindow.document.body.innerHTML; //-- unsafeWindow makes no difference.
var sRawHtml = document.body.innerHTML;
if (sRawHtml)
{
sRawHtml = sRawHtml.replace (/^\s\s*/, ''). substr (0, 60);
sUserMessage = sTitle + ', Doc body = ' + sRawHtml + ' ...';
}
else
{
sUserMessage = sTitle + ', Document body seems empty!';
}
if (typeof (console) != "undefined")
{
console.log (sUserMessage);
}
else
{
if (typeof (GM_log) != "undefined")
GM_log (sUserMessage);
else
if (!sRawHtml)
alert (sUserMessage);
}
}
LocalMain ('Preload');
window.addEventListener ("load", function() {LocalMain ('After load');}, false);