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....
> 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.
~Brett
> This would be your problem. See pitfall #2 athttp://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greas....
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
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!");
}
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/
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); };
> ...