Greasemonkey is getting an empty document.body on select Google pages.
Posted
by Brock Adams
on Stack Overflow
See other posts from Stack Overflow
or by Brock Adams
Published on 2010-05-23T11:04:29Z
Indexed on
2010/05/23
11:10 UTC
Read the original article
Hit count: 260
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);
© Stack Overflow or respective owner