GM doesn't seem to execute

38 views
Skip to first unread message

bpat1434

unread,
Mar 19, 2007, 10:45:45 AM3/19/07
to greasemonkey-users
Hi all. I'm having a bit of an issue. I've got a pretty simple
javascript that I'm trying to run through GM when a page loads.
Basically, it does two simple regex's and shows a div that is in
userChrome.css based upon the script output.

I've set it up so it runs only on one site. I've also got a check in
to make sure that the comment tags around the part I want to run the
regex on is there. If i copy the html from the site, and copy the JS
to a new html page. On the new page the script executes as expected
and I see the div and link properly. If it runs on the site, nothing
happens.

here's the javascript:
// ==UserScript==
// @name PHPBuilder Moderation Queue Alert
// @namespace http://www.bpatterson.net
// @description Uses a pure CSS style pop-up to alert you to any
posts or threads in the moderation queue
// @include http://www.phpbuilder.com/board*
// @include http://phpbuilder.com/board*
// ==/UserScript==

function getInfo() {
var start = document.body.innerHTML.indexOf('<!-- Quick Moderation --
>');
var end = document.body.innerHTML.indexOf('<!-- / Quick Moderation --
>', start);

checkMod(document.body.innerHTML.substring(start, end));
}

function checkMod(str) {
var tRegEx = /Threads<\/a>\: <b>(\d+)<\/b>/i;
var pRegEx = /Posts<\/a>\: <b>(\d+)<\/b>/i;

var t = str.match(tRegEx);
var p = str.match(pRegEx);

if(t[1] > 0 || p[1] > 0)
showQueue(t[1], p[1]);
else
return false;
}

function showQueue(threads, posts) {
var modCon = document.createElement('div');
var modInfo = document.createElement('div');
var link = document.createElement('a');
var link2 = document.createElement('a');
var theBR = document.createElement('br');

modCon.setAttribute('id', 'phpBuilderMod');
modInfo.setAttribute('id', 'phpBM_info');

var textLink = document.createTextNode('Moderate Threads and Posts');
var closeButton = document.createTextNode('Close');

link.setAttribute('href', 'mod/moderation.php?do=posts');
link.setAttribute('target', '_blank');
link.appendChild(textLink);

modInfo.appendChild(link);
modInfo.appendChild(theBR);
modInfo.appendChild(link2);

modCon.appendChild(modInfo);
modCon.onclick = function () { this.parentNode.removeChild(this); };

document.body.appendChild(modCon);
}

window.onload = function () { setTimeout('getInfo()',3000); }


Is there a reason this wouldn't work? Like I said, the script works
as expected, but not when I visit the site. GM shows the script as
"installed", but it doesn't seem to execute. And when there really
aren't any matches, I get an error of "Component Unavailable".

Running GM 0.6.7.20070131.0 - About to upgrade manually....

Henrik Nyh

unread,
Mar 19, 2007, 10:50:16 AM3/19/07
to greasemon...@googlegroups.com
bpat1434 wrote:
> Hi all. I'm having a bit of an issue. I've got a pretty simple
> javascript that I'm trying to run through GM when a page loads.

> window.onload = function () { setTimeout('getInfo()',3000); }

This would be your problem. See pitfall #2 at
http://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greasemonkey-pitfalls.html?page=3.

bpat1434

unread,
Mar 19, 2007, 11:25:44 AM3/19/07
to greasemonkey-users
Thanks for the info. I'm guessing I should use the
addEventListener.... we'll see if that works.

~Brett

> This would be your problem. See pitfall #2 athttp://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greas....

bpat1434

unread,
Mar 19, 2007, 1:51:42 PM3/19/07
to greasemonkey-users
On Mar 19, 11:25 am, "bpat1434" <brett...@gmail.com> wrote:
> Thanks for the info. I'm guessing I should use the
> addEventListener.... we'll see if that works.
>
> ~Brett
>
> On Mar 19, 10:50 am, Henrik Nyh <hen...@nyh.se> wrote:
>
> > bpat1434 wrote:
> > > Hi all. I'm having a bit of an issue. I've got a pretty simple
> > > javascript that I'm trying to run through GM when a page loads.
> > > window.onload = function () { setTimeout('getInfo()',3000); }
>
> > This would be your problem. See pitfall #2

It would seem that now I have another issue...... I get the following
error:

[Exception... "Component is not available" nsresult: "0x80040111
(NS_ERROR_NOT_AVAILABLE)" location: "JS frame ::
file:///C:/Documents%20and%20Settings/Brett/Application%20Data/Mozilla/Firefox/Profiles/7otpix7y.default/extensions/%7Be4a8a97b-f2ed-450b-b12d-ee082ba24781%7D/components/greasemonkey.js
:: showQueue :: line 339" data: no]

The javascript is fine.... The code really hasn't changed much....
except that I chaged window.onload to
window.addEventListener("load", getInfo, false);

~Brett

Henrik Nyh

unread,
Mar 19, 2007, 2:15:29 PM3/19/07
to greasemon...@googlegroups.com
bpat1434 wrote:
>> On Mar 19, 10:50 am, Henrik Nyh <hen...@nyh.se> wrote:
>>
>>> bpat1434 wrote:
>>>> Hi all. I'm having a bit of an issue. I've got a pretty
>>>> simple javascript that I'm trying to run through GM when a page
>>>> loads. window.onload = function () {
>>>> setTimeout('getInfo()',3000); }
>>> This would be your problem. See pitfall #2
>
> It would seem that now I have another issue...... I get the following
> error:
>
> [Exception... "Component is not available" nsresult: "0x80040111
> (NS_ERROR_NOT_AVAILABLE)" location: "JS frame ::
> file:///C:/Documents%20and%20Settings/Brett/Application%20Data/Mozilla/Firefox/Profiles/7otpix7y.default/extensions/%7Be4a8a97b-f2ed-450b-b12d-ee082ba24781%7D/components/greasemonkey.js
> :: showQueue :: line 339" data: no]
>
> The javascript is fine.... The code really hasn't changed much....
> except that I chaged window.onload to window.addEventListener("load",
> getInfo, false);

Remove pieces of code until you find the offender. Shouldn't be that
line, since this test script works fine for me:

// ==UserScript==
// @name Test onload
// @namespace http://henrik.nyh.se
// @description Testing.
// @include *
// ==/UserScript==

window.addEventListener("load", my_function, false);

function my_function() {
alert("Load!");
}

Johan Sundström

unread,
Mar 20, 2007, 9:42:12 AM3/20/07
to greasemon...@googlegroups.com

It is also pitfall #1, same page. And re: pitfall #2, it's typically
rather roundabout to replace it with an addEventListener for the load
event, as the script gets run at DOMContentLoaded, which is more or
less always what you wanted anyway, so a simple

setTimeout( getInfo, 3000 );

is probably what you wanted to do, anyway, or even more likely a plain

getInfo();

unless you for some private rather than technical reason of your own
want the script not to execute for the first three seconds after
having loaded the page. Both "safeguards" hint that you're afraid of
an unready DOM or something like that, and you never have that when GM
is invoked, so you are most likely chasing problems that don't exist,
out of common webside habits.

--
/ Johan Sundström, http://ecmanaut.blogspot.com/

esquifit

unread,
Mar 24, 2007, 3:37:41 AM3/24/07
to greasemon...@googlegroups.com
Direct assignment of event handlers through DOM properties is also
verboten in GM (this is also Pitfall #2). This means that, apart from
the hints received so far, you will have to change:

modCon.onclick = function () { this.parentNode.removeChild(this); };

by something like

modCon.addEventListener('click',
function (ev) {

ev.target.parentNode.removeChild(ev.target);
} ,
true);

2007/3/19, bpat1434 <bret...@gmail.com>:
> ...


> modCon.appendChild(modInfo);
> modCon.onclick = function () { this.parentNode.removeChild(this); };

> ...

Arvid Jakobsson

unread,
Mar 24, 2007, 8:27:21 AM3/24/07
to greasemon...@googlegroups.com
Pitfall bingo anyone?
Reply all
Reply to author
Forward
0 new messages