User:Killr833/usspell.js

/** * usspell.js * * Display Britain spelling as American, for RS:SG states that British * spelling must be used. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * This script is a modification of Gary King's Comments in Local Time * from Wikipedia.

$(document).ready(function {

var node = document.getElementById('bodyContent');

replace_text(node, /colour/gi, 'color'); replace_text(node, /grey/gi, 'gray'); replace_text(node, /defence/gi, 'defense'); replace_text(node, /humour/gi, 'humor'); replace_text(node, /storey/gi, 'story'); replace_text(node, /armour/gi, 'armor'); replace_text(node, /jewellery/gi, 'jewelry'); replace_text(node, /behaviour/gi, 'behavior'); replace_text(node, /artefact/gi, 'artifact'); replace_text(node, /centre/gi, 'center');

function replace_text(node, search, replace) {	if (node.nodeType == 3) {		var value = node.nodeValue; var matches = value.match(search); if (matches != null) {			var node_parent_node = node.parentNode; var old_node = node; // old_node_list = node.parentNode.childNodes; for (match = 0; match < matches.length; match++) {				// Create MATCHES[MATCH] var position; if (after_match != null && length != null) position = after_match.search(search) + before_match.length + length; else position = value.search(search); var length = matches[match].toString.length; var before_match = value.substring(0, position); var after_match = value.substring(position + length); var span = document.createElement('span'); /***				 * Replaced text has a span tag, you can manipulate it here. */				span.appendChild(document.createTextNode(matches[match].toString.replace(search, replace))); var new_node = document.createDocumentFragment; new_node.appendChild(document.createTextNode(before_match)); new_node.appendChild(span); new_node.appendChild(document.createTextNode(after_match)); node_parent_node.replaceChild(new_node, old_node); break; }		}	}	else {		var children = [], child = node.firstChild; while (child) {			children[children.length] = child; child = child.nextSibling; }		for (var child = 0; child < children.length; child++) replace_text(children[child], search, replace); } }

});