Skip to content

Commit

Permalink
Allow the filter to be turned off and a callback to fire when there i…
Browse files Browse the repository at this point in the history
…s profanity.
  • Loading branch information
mkilpatrick committed Jun 16, 2015
1 parent ecd24a6 commit 8ec602e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 32 deletions.
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The `jQuery.profanityFilter` has the ability to filter out profane words on the

This was built in order to allow users to "Opt-in" to profanity filtering, and offload all of the work to the client, saving the headache on the server. The `jQuery.profanityFilter` also attempts to use Local Storage on the client machine in order to reduce lookups at the `externalSwears` URL.

***Obvious warning:*** *The `swearWord` lists as well as code examples contain material that many will find offensive. (But that's the point!)*
***Obvious warning:*** *The `swearWord` lists as well as code examples contain material that many will find offensive. (But that's the point!)*
*note: localization support provided by shutterstock's [List of Dirty, Naughty, Obscene, and Otherwise Bad Words](https://github.com/shutterstock/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words)*

###**Usage:**###
Expand All @@ -18,23 +18,23 @@ Option 1:

// Filter the word "shit" every time it shows up inside the element "SomeElement"
$('#SomeElement').profanityFilter({
customSwears: ['shit']
});`
customSwears: ['shit']
});

Option 2:

// Filter an external array of words on the entire document
$(document).profanityFilter({
externalSwears: '/Path/To/Json/Swears/'
});
externalSwears: '/Path/To/Json/Swears/'
});

Option 3:

// Change the replacement character from an astrisk (*) to a pound sign (#)
$('#SomeElement').profanityFilter({
externalSwears: '/Path/To/Json/Swears/',
replaceWith: '#'
});
externalSwears: '/Path/To/Json/Swears/',
replaceWith: '#'
});

Option 4:

Expand All @@ -48,16 +48,31 @@ Option 5:

// Combine an externl Swear list with a custom list (don't worry, we'll remove duplicates)
$('#SomeElement').profanityFilter({
customSwears: ['monkeyass'],
externalSwears: '/Path/To/Json/Swears/'
});
customSwears: ['monkeyass'],
externalSwears: '/Path/To/Json/Swears/'
});

Option 6:

// Don't filter anything. Useful in conjunction with the profaneText callback (which only
// fires when profanity exists).
// Ex. Check if the element contains profanity and throw an alert.
$('#SomeElement').profanityFilter({
customSwears: ['shit'],
filter: false,
profaneText: function() {
alert('That is vulgar!');
}
});




###**Bug Tracker:**###

---

Find a bug? Please create an issue here on GitHub!
Find a bug? Please create an issue here on GitHub!
https://github.com/ChaseFlorell/jQuery.profanityFilter/issues


Expand All @@ -72,8 +87,8 @@ This plugin requires:
###**Author:**###

---
Chase Florell

Chase Florell

- http://github.com/ChaseFlorell
- http://twitter.com/ChaseFlorell
Expand Down
31 changes: 31 additions & 0 deletions demo/index.htm
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,37 @@ <h2>a simple client side profanity filter</h2>
</script>
</div>
</section>
<section>
<pre class="prettyprint code box">
// code:
$('#five').profanityFilter({
customSwears: ['ass'],
filter: true,
profaneText: function() {
$('#five')
.css('color', 'blue');
}
}) </pre>
<div class="description-example-wrapper">
<div class="description box">
This container won't filter anything, but it will do something clever because
it contains the word "ass"
</div>
<div id="five" class="example box">
Often when I go to the bathroom, shit comes out of my ass.
</div>
<script>
$('#five').profanityFilter({
customSwears: ['ass'],
filter: false,
profaneText: function() {
$('#five').css('color', 'blue');
}
})
</script>
</div>
</section>


<footer class="box">
<p>Like what you see?<br>
Expand Down
29 changes: 20 additions & 9 deletions demo/jquery.profanityfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@
var defaults = {
replaceWith: '*',
customSwears: null,
externalSwears: null
externalSwears: null,
filter: true,
profaneText: function () {}
};


/// <summary>jQuery plugin used to filter profanity on the attached element</summary>
/// <param name="settings">user overridden settings</param>
/// <returns>text from an element but blots out the swear words</returns>
$.fn.profanityFilter = function (settings) {
$.fn.profanityFilter = function (settings, callback) {

var profane = false;

var options = $.extend({}, defaults, settings),
localStorageIsEnabled;
Expand Down Expand Up @@ -66,7 +70,7 @@
return out;
}

var cursor,
var cursor,
closed = [],
open = getChildNodes(parent);

Expand Down Expand Up @@ -104,14 +108,14 @@
randomNumber +=1;
}
}

if (randomNumber > max) {
//set it back to zero
randomNumber = 0;
}

lastRandomNumber = randomNumber;

return randomNumber;
}

Expand Down Expand Up @@ -153,18 +157,25 @@
for (x = 0; x < nodes.length; x += 1) {
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);
}
if (re.test(nodes[x].nodeValue)) {
nodes[x].nodeValue = nodes[x].nodeValue.replace(re, rep);
profane = true;
if (options.filter) {
nodes[x].nodeValue = nodes[x].nodeValue.replace(re, rep);
}
}
}
}

if (profane) {
options.profaneText();
}
});
};
})(jQuery);
29 changes: 20 additions & 9 deletions jquery.profanityfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@
var defaults = {
replaceWith: '*',
customSwears: null,
externalSwears: null
externalSwears: null,
filter: true,
profaneText: function () {}
};


/// <summary>jQuery plugin used to filter profanity on the attached element</summary>
/// <param name="settings">user overridden settings</param>
/// <returns>text from an element but blots out the swear words</returns>
$.fn.profanityFilter = function (settings) {
$.fn.profanityFilter = function (settings, callback) {

var profane = false;

var options = $.extend({}, defaults, settings),
localStorageIsEnabled;
Expand Down Expand Up @@ -66,7 +70,7 @@
return out;
}

var cursor,
var cursor,
closed = [],
open = getChildNodes(parent);

Expand Down Expand Up @@ -104,14 +108,14 @@
randomNumber +=1;
}
}

if (randomNumber > max) {
//set it back to zero
randomNumber = 0;
}

lastRandomNumber = randomNumber;

return randomNumber;
}

Expand Down Expand Up @@ -153,18 +157,25 @@
for (x = 0; x < nodes.length; x += 1) {
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);
}
if (re.test(nodes[x].nodeValue)) {
nodes[x].nodeValue = nodes[x].nodeValue.replace(re, rep);
profane = true;
if (options.filter) {
nodes[x].nodeValue = nodes[x].nodeValue.replace(re, rep);
}
}
}
}

if (profane) {
options.profaneText();
}
});
};
})(jQuery);

0 comments on commit 8ec602e

Please sign in to comment.