From d6c5189b179acd1d2248d1f3ce42f6014e0291cf Mon Sep 17 00:00:00 2001 From: Jan Dahl <jandahl@users.noreply.github.com> Date: Tue, 8 Jul 2014 14:05:57 +0200 Subject: [PATCH] Using magic array to add extensibility --- Source/content_script.js | 42 ++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/Source/content_script.js b/Source/content_script.js index 5a61aaa..22b2652 100644 --- a/Source/content_script.js +++ b/Source/content_script.js @@ -1,10 +1,8 @@ -walk(document.body); - function walk(node) { // I stole this function from here: // http://is.gd/mwZp7E - + var child, next; switch ( node.nodeType ) @@ -27,16 +25,32 @@ function walk(node) } } -function handleText(textNode) -{ - var v = textNode.nodeValue; - - v = v.replace(/\bThe Cloud\b/g, "My Butt"); - v = v.replace(/\bThe cloud\b/g, "My butt"); - v = v.replace(/\bthe Cloud\b/g, "my Butt"); - v = v.replace(/\bthe cloud\b/g, "my butt"); - - textNode.nodeValue = v; -} +function handleText(textNode){ + var textArray = [{ + originalText: "The Cloud", + updatedText: "My Butt" + },{ + originalText: "The cloud", + updatedText: "My butt" + }, { + originalText: "the Cloud", + updatedText: "my Butt" + }, { + originalText: "the cloud", + updatedText: "my butt", + } + ]; + + for (var enumeration = textArray.length - 1; enumeration >= 0; enumeration--) { + // console.log(textArray[enumeration]); + var nodeTextToFix = textNode.nodeValue; + + var huntedPhrase = new RegExp(textArray[enumeration].originalText, "g") + + + textNode.nodeValue = nodeTextToFix.replace(huntedPhrase, textArray[enumeration].updatedText); + }; +} +walk(document.body); -- GitLab