simple script working but not everywhere, advice please?

20 views
Skip to first unread message

SA

unread,
Aug 5, 2011, 4:42:10 AM8/5/11
to greasemonkey-users
I have a simple script to search and replace stuff on the web - as a
trivial example here:

// ==UserScript==
// @name nobble
// @namespace http://news.bbc.co.uk/weather/forecast/346
// @include *
// ==/UserScript==
textNodes = document.evaluate( "//text()", document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var searchRE = new RegExp('mb','g');// I know doesn't need regexp for
this
var replace = 'MB';
for (var i=0;i<textNodes.snapshotLength;i++){
var node = textNodes.snapshotItem(i);
node.data = node.data.replace(searchRE, replace);
}

On the bbc weather forecast this works fine initially:
http://news.bbc.co.uk/weather/forecast/4168
but if the 24hour forecast button is pressed the resulting webpage is
unaffected.

Any ideas?

LouCypher

unread,
Aug 5, 2011, 7:14:00 AM8/5/11
to greasemon...@googlegroups.com
Old and deprecated method:
You must add listener on mutation event to target window or element
which will slow browser performance.
http://www.w3.org/TR/DOM-Level-3-Events/#events-mutationevents

// ==UserScript==
// @name nobble
// @namespace http://news.bbc.co.uk/weather/forecast/346

// @include http://news.bbc.co.uk/weather/forecast/*
// ==/UserScript==

var div = document.getElementById("Forecast_main");
div.addEventListener("DOMNodeInserted", function() {
var textNodes = document.evaluate( "//text()", document, null,


XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var searchRE = new RegExp('mb','g');// I know doesn't need regexp for this
var replace = 'MB';
for (var i=0;i<textNodes.snapshotLength;i++) {
var node = textNodes.snapshotItem(i);
node.data = node.data.replace(searchRE, replace);
}

}, false);


Newer and better method(s):
http://www.w3.org/2008/webapps/wiki/MutationReplacement

> --
> You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
> To post to this group, send email to greasemon...@googlegroups.com.
> To unsubscribe from this group, send email to greasemonkey-us...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/greasemonkey-users?hl=en.
>
>

(David *) Frenkiel

unread,
Aug 5, 2011, 11:00:15 AM8/5/11
to greasemonkey-users
First of all, changes to the page after it has been loaded don't
automatically go thru the greasemonkey script, unless you've installed
relevant event handlers. It seems that the 24 hour tab button just
changes the layout of the page and maybe fetches data, but does not
reload and thus does not trigger another invocation of the
greasemonkey script.
In addition, document.evaluate may fail when the page has a default
name space and you've failed to provide a resolver. For example, if
you're looking for divs and the page's default namespace is
xmlns="http://www.w3.org/1999/xhtml"

then your document.evaluate should be written this way:

document.evaluate("//a:div", document, resolver, ...

where resolver is a function() { return "http://www.w3.org/1999/
xhtml"; }
Reply all
Reply to author
Forward
0 new messages