User:Max Duchess/termhighlighter.js

/*
 * User:Max_Duchess/termhighlighter.js
 * v0.1
 * This script highlights specified terms in the content of articles,
 * makes it easier to spot frequently misspelled words.
 * v0.2
 * It now supports replacing specified terms in the edit box by switching
 * the fixContent variable to true.
 * TODO: If the replaced word was at the beginning of a sentence, capitalize!
 * the fixContent variable to true.
 * TODO: If the replaced word was at the beginning of a sentence, capitalize!
 * TODO: If the replaced word was at the beginning of a sentence, capitalize!

function doHighlightTerms(bodyText, searchTerm) { highlightStartTag = ""; highlightEndTag = " ";

var newText = ""; var i = -1; var lcSearchTerm = searchTerm[0].toLowerCase; var lcBodyText = bodyText.toLowerCase;

while (bodyText.length > 0) { i = lcBodyText.indexOf(lcSearchTerm, i+1); if (i < 0) { newText += bodyText; bodyText = ""; } else { // skip anything inside an HTML tag if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) { // skip anything inside a block if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("= lcBodyText.lastIndexOf("{{", i)) { // skip anything inside a wikilink if (lcBodyText.lastIndexOf("]]", i) >= lcBodyText.lastIndexOf("[[", i)) { // skip anything inside a block if (lcBodyText.lastIndexOf("/textarea>", i) >= lcBodyText.lastIndexOf(" 0) { i = lcBodyText.indexOf(lcSearchTerm, i+1); if (i < 0) { newText += editText; editText = ""; } else { // skip anything inside an HTML tag if (editText.lastIndexOf(">", i) >= editText.lastIndexOf("<", i)) { // skip anything inside a block if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("= lcBodyText.lastIndexOf("{{", i)) { // skip anything inside a wikilink if (lcBodyText.lastIndexOf("]]", i) >= lcBodyText.lastIndexOf("[[", i)) { if (isUpper(editText[editText.substring(0, i+1).lastIndexOf(" ")+1])) { newText += editText.substring(0, i) + searchTerm[1].substring(0, 1).toUpperCase + searchTerm[1].substring(1, searchTerm[1].length); } else { newText += editText.substring(0, i) + searchTerm[1]; }             editText = editText.substr(i + searchTerm[0].length); lcBodyText = editText.toLowerCase; i = -1; }         }        }      }    }  }  return newText; }

function isUpper(aChar) { myCharCode = aChar.charCodeAt(0); if((myCharCode > 64) && (myCharCode < 91)) {   return true; } return false; }

function highlightSearchTerms(searchTextArray, fixContent) { var editContent = document.getElementsByName("wpTextbox1")[0]; var bodyContent = document.getElementById("bodyContent"); if (document.body && searchTextArray != null && searchTextArray.length > 0) { if (bodyContent != null) { var bodyText = bodyContent.innerHTML; for (var i = 0; i < searchTextArray.length; i++) { bodyText = doHighlightTerms(bodyText, searchTextArray[i]); bodyContent.innerHTML = bodyText; }   }    if (fixContent && editContent != null) { var editText = editContent.innerHTML; for (var i = 0; i < searchTextArray.length; i++) { editText = doReplaceTerms(editText, searchTextArray[i]); editContent.innerHTML = editText; }   }      } }