Replace Text On Webpages

Replaces text on websites. Now supports wildcards in search queries. Won't replace text in certain tags like links and code blocks

< Opiniones de Replace Text On Webpages

Pregunta o comentario

§
Publicado: 16/7/2015

Replace text on website only if the userscript sees trigger word (Greasemonkey)

Before anyone is confused, this userscript belongs to JoeSimmons. Although it works extremely well, I wanted to add an extra functionality. However, I've only worked with C++ and Java before, so I have no idea how to work with Javascript userscripts.

What I want:

For example, if I write this in the var words portion of the userscript,

'Cat' : 'Feline'

'Dog' : 'Canine'

If the userscript sees the word Kitten on the webpage, it would change Cat to Meow instead of Feline. It should do the same with multiple other words if I want it to.

(function () { 'use strict';

/*
NOTE:
You can use \\* to match actual asterisks instead of using it as a wildcard!
The examples below show a wildcard in use and a regular asterisk replacement.
*/

var words = {
///////////////////////////////////////////////////////


// Syntax: 'Search word' : 'Replace word',
'your a' : 'you\'re a',
'imo' : 'in my opinion',
'im\\*o' : 'matching an asterisk, not a wildcard',
'/\\bD\\b/g' : '[D]',


///////////////////////////////////////////////////////
'':''};




//////////////////////////////////////////////////////////////////////////////
// This is where the real code is
// Don't edit below this
//////////////////////////////////////////////////////////////////////////////

var regexs = [], replacements = [],
tagsWhitelist = ['PRE', 'BLOCKQUOTE', 'CODE', 'INPUT', 'BUTTON', 'TEXTAREA'],
rIsRegexp = /^\/(.+)\/([gim]+)?$/,
word, text, texts, i, userRegexp;

// prepareRegex by JoeSimmons
// used to take a string and ready it for use in new RegExp()
function prepareRegex(string) {
return string.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, '\\$1');
}

// function to decide whether a parent tag will have its text replaced or not
function isTagOk(tag) {
return tagsWhitelist.indexOf(tag) === -1;
}

delete words['']; // so the user can add each entry ending with a comma,
// I put an extra empty key/value pair in the object.
// so we need to remove it before continuing

// convert the 'words' JSON object to an Array
for (word in words) {
if ( typeof word === 'string' && words.hasOwnProperty(word) ) {
userRegexp = word.match(rIsRegexp);

// add the search/needle/query
if (userRegexp) {
regexs.push(
new RegExp(userRegexp[1], 'g')
);
} else {
regexs.push(
new RegExp(prepareRegex(word).replace(/\\?\*/g, function (fullMatch) {
return fullMatch === '\\*' ? '*' : '[^ ]*';
}), 'g')
);
}

// add the replacement
replacements.push( words[word] );
}
}

// do the replacement
texts = document.evaluate('//body//text()[ normalize-space(.) != "" ]', document, null, 6, null);
for (i = 0; text = texts.snapshotItem(i); i += 1) {
if ( isTagOk(text.parentNode.tagName) ) {
regexs.forEach(function (value, index) {
text.data = text.data.replace( value, replacements[index] );
});
}
}
}());

Thank you very much in advance!

§
Publicado: 19/10/2015

does this work on https sites? I've been trying to find a way to replace my account numbers with meaningful names on my banking site for ages. I've got this working with other sites. darn, I thought this was the solution.

Publicar respuesta

Inicia sesión para responder.