Skip to content

Instantly share code, notes, and snippets.

@Antti
Created March 26, 2010 19:50
Show Gist options
  • Save Antti/345303 to your computer and use it in GitHub Desktop.
Save Antti/345303 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
var censorwords_list=[];
function addOption (oListbox, text, value, isDefaultSelected, isSelected)
{
var oOption = document.createElement("option");
oOption.appendChild(document.createTextNode(text));
oOption.setAttribute("value", value);
if (isDefaultSelected) oOption.defaultSelected = true;
else if (isSelected) oOption.selected = true;
oListbox.appendChild(oOption);
}
function add_word()
{
word = document.getElementById("censoreword").value;
censorwords_list.push(word);
addOption(document.getElementById("censorelist"),word,0,false,false);
}
function perform_censorecheck()
{
var counter=0;
text = document.getElementById("text").value;
for (w in censorwords_list)
{
word = censorwords_list[w];
replacement = word.replace(/./g,"*");
reg = new RegExp(word, "g");
match=text.match(reg);
if (!match)
continue;
count=match.length;
if(count>0)
{
counter+=count;
text = text.replace(reg , replacement);
}
}
document.getElementById("text").value = text;
alert("Replaced "+counter+" words");
}
function remove_word()
{
list=document.getElementById("censorelist");
word = list.options[list.selectedIndex].text;
for(w in censorwords_list)
{
if (censorwords_list[w]==word)
censorwords_list.splice(w,1);
}
list.remove(list.selectedIndex);
}
</script>
<div style="float:left"><textarea rows="9" cols="40" id="text"></textarea></div>
<div style=""><select size="6" id="censorelist"></select><br />
<button onclick="remove_word()">Remove</button><br />
<input type="text" id="censoreword" /><button onclick="add_word()">add</button>
</div>
<br />
<div style="float:left"><button onclick="perform_censorecheck()">Check censore!</button></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment