User:Marolysis/common.js

function normalize(text) { return text.replace(/'\b/g, "\u2018") // Opening singles .replace(/\b'/g, "\u2019") // Closing singles .replace(/"\b/g, "\u201c") // Opening doubles             .replace(/\b"/g, "\u201d")  // Closing doubles .replace(/--+/g, "\u2014") // em-dashes .replace(/([”’])([,.!?])/g, "$2$1"); // Closing before punctuation }

function normalizeTextNode(element) { element.nodeValue = normalize(element.nodeValue); }

function forEachElementWithTagName(tagName, f) { var elements = document.getElementsByTagName(tagName); for (var i = 0; i < elements.length; i++) f(elements[i]); }

function forEachTextNode(f) { function recurse(element) { if (element.childNodes.length > 0) for (var i = 0; i < element.childNodes.length; i++) recurse(element.childNodes[i]);

if (element.nodeType == Node.TEXT_NODE) f(element); }

var html = document.getElementsByTagName('html')[0]; recurse(html); }

forEachTextNode(normalizeTextNode);