-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.word.censor.js
68 lines (46 loc) · 1.48 KB
/
jquery.word.censor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(function($, window, document, undefined){
var WordCensor = {
init : function( options, elem ){
var self = this;
self.container = elem;
self.removeWords ='';
//extending default options with the user option
self.options = $.extend( {}, $.fn.censor.options, options );
self.setFilters();
},
setFilters : function(){ //set the code for remove words
var self = this;
// set the code for object of badwords
if(typeof(self.options.badWords) === 'object'){
self.totalBadWords = self.options.badWords.length;
$.each(self.options.badWords, function(index, badWord) {
self.removeWords += badWord;
if(index<(self.totalBadWords-1)) {
self.removeWords += "|" ;
}
});
} else { // set the code for string of badwords
self.removeWords = self.options.badWords;
}
self.doCensor();
},
doCensor : function(){
var self = this;
htmlContent = $(self.container).html();
if(htmlContent!=null){
var content = htmlContent.replace( new RegExp(self.removeWords, "igm"), self.options.replaceWord );
$(self.container).html(content);
}
}
};
$.fn.censor = function( options ){
var elem = this.selector;
return this.each(function(){
censorObj = Object.create( WordCensor );
censorObj.init( options, elem );
});
};
$.fn.censor.options = {
replaceWord : '**censored**'
};
})(jQuery, window, document)