forked from ChaseFlorell/jQuery.ProfanityFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.profanityfilter.min.js
1 lines (1 loc) · 2.78 KB
/
jquery.profanityfilter.min.js
1
(function($){"use strict";String.prototype.repeat=function(num){return new Array(num+1).join(this)};Array.prototype.unique=function(){var a,i,j;a=this.concat();for(i=0;i<a.length;++i){for(j=i+1;j<a.length;++j){if(a[i]===a[j]){a.splice(j,1)}}}return a};var defaults={replaceWith:"*",customSwears:null,externalSwears:null,filter:true,profaneText:function(){}};$.fn.profanityFilter=function(settings,callback){var options=$.extend({},defaults,settings),localStorageIsEnabled;localStorageIsEnabled=function(){var uid=new Date,result;try{localStorage.setItem("uid",uid);result=localStorage.getItem("uid")===uid;localStorage.removeItem("uid");return result&&localStorage}catch(e){}}();function allTextNodes(parent){function getChildNodes(parent){var x,out=[];for(x=0;x<parent.childNodes.length;x+=1){out[x]=parent.childNodes[x]}return out}var cursor,closed=[],open=getChildNodes(parent);while(open.length){cursor=open.shift();if(cursor.nodeType===1){open.unshift.apply(open,getChildNodes(cursor))}if(cursor.nodeType===3){closed.push(cursor)}}return closed}function readJsonFromController(file){var request=new XMLHttpRequest;request.open("GET",file,false);request.setRequestHeader("X-Requested-With","XMLHttpRequest");request.send(null);try{return JSON.parse(request.responseText)}catch(e){return""}}var lastRandomNumber=null;function generateRandomNumber(max){var randomNumber=Math.floor(Math.random()*max);if(lastRandomNumber==null){lastRandomNumber=randomNumber}else{if(randomNumber==lastRandomNumber&&max!=0){randomNumber+=1}}if(randomNumber>max){randomNumber=0}lastRandomNumber=randomNumber;return randomNumber}return this.each(function(){var badWords,i,nodes=allTextNodes(this),re,rep,x,inputs=$(this).find(":input"),profane=false;if(options.externalSwears!==null){if(localStorageIsEnabled){if(localStorage.getItem("localSwears")===null){localStorage.setItem("localSwears",JSON.stringify(readJsonFromController(options.externalSwears)))}badWords=JSON.parse(localStorage.getItem("localSwears"))}else{badWords=readJsonFromController(options.externalSwears)}if(options.customSwears!==null){badWords=badWords.concat(options.customSwears).unique()}}else{if(options.customSwears!==null){badWords=options.customSwears}}if(badWords===null){return}for(i=0;i<badWords.length;i+=1){re=new RegExp("\\b"+badWords[i]+"\\b","gi");var rand=generateRandomNumber(options.replaceWith.length-1);rep=options.replaceWith[rand];if(typeof options.replaceWith=="string"){rep=options.replaceWith[rand].repeat(badWords[i].length)}for(x=0;x<nodes.length;x+=1){if(re.test(nodes[x].nodeValue)){profane=true;if(options.filter){nodes[x].nodeValue=nodes[x].nodeValue.replace(re,rep)}}}for(var x=0;x<inputs.length;x++){if(re.test(inputs[x].value)){profane=true;if(options.filter){$(inputs[x]).val(inputs[x].value.replace(re,rep))}}}}if(profane){options.profaneText()}})}})(jQuery);