I started off just replacing “King Charles” -> “Charles”, because it grates every time I see it. The end result is quite marvellous, though.
Relevant code:
// ==UserScript==
// @name Replace Text On Webpages
// @namespace http://userscripts.org/users/23652
// @description Replaces text on websites. Now supports wildcards in search queries. Won't replace text in certain tags like links and code blocks
// @include http://*
// @include https://*
// @include file://*
// @exclude http://userscripts.org/scripts/review/*
// @exclude http://userscripts.org/scripts/edit/*
// @exclude http://userscripts.org/scripts/edit_src/*
// @exclude https://userscripts.org/scripts/review/*
// @exclude https://userscripts.org/scripts/edit/*
// @exclude https://userscripts.org/scripts/edit_src/*
// @version 1.1.0
// ==/UserScript==
(function () {
'use strict';
var words = {
'King Charles III':'Charlizard',
'King Charles':'Charlizard',
'Queen Consort Camilla': 'Camilizard',
'Camilla': 'Camilizard',
'The King': 'Charlizard',
'the King': 'Charlizard',
'The Queen': 'Elizardabeth',
'the Queen': 'Elizardabeth',
'The late Queen': 'Elizardabeth',
'the late Queen': 'Elizardabeth',
'Queen Elizabeth II': 'Elizardabeth',
'Elizabeth II': 'Elizardabeth',
'Queen Elizabeth': 'Elizardabeth',
'Queen': 'Elizardabeth',
'Elizabethan': 'Elizardabethan',
};
//////////////////////////////////////////////////////////////////////////////
// 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] );
});
}
}
}());
If you’ve got any additional ones you think should be in there, just let me know - I’ll keep it up to date.
You must log in or register to comment.
deleted by creator
Welcome to The Guardian ^^
deleted by creator