I wrote a PHP function 7 days back , which avoids (calculate spamming severity) via manual spamming . So was just a curious to know, what if I create a JS plugin (my first) which can act as an SPAM validator on client side.
So here I am with my new jQuery plugin(beta version) which validate the existence of Spams in online forms etc( user input).
This plugin is in its starting point, and this is my first time when I am creating a jQuery plugin. I will really appreciate the valuable feedback.
Please point me whether I am wrong in my approach and how can I make it even better (is it feasible?).
Plugin code
(function ( $ ) //IIFE
{
$.fn.spamDetector = function(options)
{
var thisObj = this;
var is_spam = 0;
var settings = $.extend({
spamWordsInUri : ['free', 'vote', 'play'],
bannedWords : ['Levitra', 'viagra', 'casino', '*'],
alertText : 'Sorry, your content seems spammy , please check',
onlyReturnStatus: false,
spamLevel : 2,
checkOnType : true,
textLimit : 200,
displayWarning : true,
formObj : false,
warningStyle : { 'color': 'red', 'background-color': 'snow', 'padding': '1px','border': '2px solid red'}
}, options );
$(settings.errorTextContainer).html('<span id="error_text" style="display:none;">'+settings.alertText+'</span>');
$('#error_text').css(settings.warningStyle);
var calculateSpam = function(){
var text = thisObj.val();
var source = (text || '').toString();
var spam_point = 0;
var spamWordsInUri;
var urlArray = [];
var url;
var matchArray;
var return_array = [];
var regexToken = /(www\.|https?:\/\/)?[a-z0-9]+\.[a-z0-9]{2,4}\S*/gi;
while( (matchArray = regexToken.exec( source )) !== null ){
var token = matchArray[0];
urlArray.push( token );
}
var number_of_url = urlArray.length;
if(number_of_url > 0){
if(number_of_url > settings.maxUrlAllowed){
spam_point += settings.spamLevel ;
}
else{
spamWordsInUri = ['free', 'vote', 'play'];
$.each( urlArray, function( index, value ){
if(value.length > settings.maxUrlLength)
spam_point += 1;
$.each( settings.spamWordsInUri, function( index, value_spams ){
if (value.toLowerCase().indexOf(value_spams) >= 0){
spam_point += 1;
}
});
});
}
}
bannedWords = ['Levitra', 'viagra', 'casino', '*'];
$.each( settings.bannedWords, function( index, value ){
if (text.toLowerCase().indexOf(value) >= 0){
spam_point += settings.spamLevel;
}
});
if(settings.textLimit && settings.textLimit != ''){
if(text.length > settings.textLimit){
spam_point += settings.spamLevel;
}
}
if(spam_point >= settings.spamLevel){
if(! settings.onlyReturnStatus){
if(settings.displayWarning){
$('#error_text').show();
}
}
is_spam = 1;
}
else{
if(settings.displayWarning){
$('#error_text').hide();
}
is_spam = 0;
}
return is_spam;
};
if(! settings.onlyReturnStatus)
{
if(settings.checkOnType)
{
$( thisObj ).bind( "keyup keydown", function(e) {
calculateSpam();
if(settings.textLimit && settings.textLimit != ''){
$(settings.limitTextContainer).html("<span id='limit_box'></span>");
$('#limit_box').css(settings.limitStyle);
if(thisObj.val().length > settings.textLimit){
if( e.keyCode === 8 || e.keyCode === 46 ) {
return; // backspace (8) / delete (46)
}
if( e.keyCode >= 37 && e.keyCode <= 40 ) {
return; // arrow keys
}
e.preventDefault();
$('#limit_box').text("you can not exceed "+ ((settings.textLimit))+' characters limit');
}
else{
$('#limit_box').text("you have " + ((settings.textLimit)-thisObj.val().length)+' characters left');
}
}
});
}
if($(settings.formObj)){
$( settings.formObj).bind( "submit", function() {
if(calculateSpam())
return false;
});
}
}
else{
return calculateSpam();
}
};
}( jQuery ));
HTML
<script type="text/javascript">
$( document ).ready(function(){
$( "#commentbox" ).spamDetector({
spamWordsInUri : ['free', 'vote', 'play'],
bannedWords : ['Levitra', 'viagra', 'casino', '*'],
alertText : 'Sorry, your content seems spammy , please check',
onlyReturnStatus : false,
spamLevel : 2,
textLimit : 200,
checkOnType : true,
displayWarning : true,
maxUrlAllowed : 2,
maxUrlLength : 150,
formObj : '#myForm',
errorTextContainer : '#myErrorDiv',
limitTextContainer : '#myLimitDiv',
warningStyle : { 'color': 'black', 'background-color': 'snow', 'padding': '.2px','border': '1px solid red'},
limitStyle : { 'color': '#580000' , 'background-color': '#E0E0E0'}
}
);
});
</script>
<div id='demo'><b>Demo</b></div>
<form id='myForm' >
<textarea rows="12" cols="80" name="comment" name='comment' id='commentbox'>Enter Comments here</textarea>
<div id='myErrorDiv' ></div>
<br/>
<div id='myLimitDiv' ></div>
<input type='submit' value='Enter'/>
</form>
</div>
FYI : configuartion settings
Basic configuration
-
\$\begingroup\$ I would point out that your code style isn't very JavaScript idiomatic. Too much whitespace makes it hard to read and KNF's style (braces on the same line) is preferred over Allman's. \$\endgroup\$elclanrs– elclanrs2014年02月19日 11:18:08 +00:00Commented Feb 19, 2014 at 11:18
1 Answer 1
I don't know if blocking specific few words (aka, creating a black list) is effective against spamming. Minor changes in the spelling will pass through your verification.
I don't really think that many real people will loose their time posting spams. Even if they do, they will easily overcome your verification because there are TOO MANY ways of writing a word that makes it understandable by a human, but not by a machine:
viagra ViAgrA VIAGRA V*iagra Vi*agra V*i*a*g*r*a V/i/a/g/r/a Vi//agra
This cannot be simply preddicted, you'd need an AI algorithm to detect potential spammers.
Other problems is that you prohibit legitimate users from using the viagra word just because it's used on spams, but you can't know for sure.
-
\$\begingroup\$ Yep agreed , as you can See I said "manual spamming" , for automated Bots we can use reCaptcha or so \$\endgroup\$Dimag Kharab– Dimag Kharab2014年02月19日 12:59:48 +00:00Commented Feb 19, 2014 at 12:59
-
\$\begingroup\$ That's true. Didn't noticed it. Edited. \$\endgroup\$Henrique Barcelos– Henrique Barcelos2014年02月19日 13:01:52 +00:00Commented Feb 19, 2014 at 13:01
-
\$\begingroup\$ anyways thanks @henrique for the comment/ suggestion . one thing I would like to point out is that we have a wide varity of option , which will ensure(may be not 100%) in stopping spams. \$\endgroup\$Dimag Kharab– Dimag Kharab2014年02月19日 13:04:11 +00:00Commented Feb 19, 2014 at 13:04
-
\$\begingroup\$ I disagree, see the edited answer. \$\endgroup\$Henrique Barcelos– Henrique Barcelos2014年02月19日 13:14:28 +00:00Commented Feb 19, 2014 at 13:14
-
1\$\begingroup\$ URL length checking is very uneffective, since it's easy to shorten it using bit.ly, goo.gl, etc. I will not insist anymore, but your approach is not effective for preventing human spammers. \$\endgroup\$Henrique Barcelos– Henrique Barcelos2014年02月19日 13:25:08 +00:00Commented Feb 19, 2014 at 13:25