[PLUG] Firefox string substitution

Galen Seitz galens at seitzassoc.com
Sat Jan 3 12:57:54 PST 2015


There is a Firefox extension called FoxReplace which will replace 
arbitrary text strings on a web page, but I'm curious how to do it in 
js.  If you're looking to waste some time, below is 'my' greasemonkey 
script. It's basically a cut-and-paste from a tutorial I found.  It 
successfully replaces strings within the text portion of the page, but 
I'm not sure how to modify it to perform the substitution on the 
data-description tag for images.

Here's an example page from the Oregonian.

<http://www.oregonlive.com/blazers/index.ssf/2014/12/no_strangers_to_the_comeback_trail_blazers_do_it_a.html>

When the below script is applied to this page, 'Moda Center' becomes 
'Rose Garden', but only in the text.  The captions for the images remain 
unchanged.  I'd like to know how to modify the captions too.


// ==UserScript==
// @name        rose_garden
// @namespace   http://localhost
// @description replace the corporate sponsor
// @include     http://www.oregonlive.com/*
// @version     1
// @grant       none
// ==/UserScript==
textNodes = document.evaluate("//text()", document, null, 
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var searchRE = new RegExp('the moda center','gi');
var replace = 'moda center';
for (var i=0;i<textNodes.snapshotLength;i++) {
   var node = textNodes.snapshotItem(i);
   node.data = node.data.replace(searchRE, replace);
}
var searchRE = new RegExp('moda center','gi');
var replace = 'the Rose Garden';
for (var i=0;i<textNodes.snapshotLength;i++) {
   var node = textNodes.snapshotItem(i);
   node.data = node.data.replace(searchRE, replace);
}



thanks,
galen
-- 
Galen Seitz
galens at seitzassoc.com





More information about the PLUG mailing list