diff --git a/demo/jquery.profanityfilter.js b/demo/jquery.profanityfilter.js index c867a20..7463806 100644 --- a/demo/jquery.profanityfilter.js +++ b/demo/jquery.profanityfilter.js @@ -42,7 +42,8 @@ $.fn.profanityFilter = function (settings, callback) { var options = $.extend({}, defaults, settings), - localStorageIsEnabled; + localStorageIsEnabled, + badWords; localStorageIsEnabled = function() { var uid = new Date(), @@ -85,16 +86,20 @@ return closed; } - function readJsonFromController(file) { + function readJsonFromController(file, callback) { var request = new XMLHttpRequest(); - request.open('GET', file, false); + request.open('GET', file); request.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); + request.onload = function() { + var parsedJson; + try { + parsedJson = JSON.parse(request.responseText); + } catch (e) { + parsedJson = ''; + } + callback.call(this, parsedJson); + }; request.send(null); - try { - return JSON.parse(request.responseText); - } catch (e) { - return ''; - } } var lastRandomNumber = null; @@ -118,80 +123,98 @@ return randomNumber; } - - return this.each(function () { - - var badWords, - i, - nodes = allTextNodes(this), - re, - rep, - x, - inputs = $(this).find(':input'), - profane = false, - data = []; - + function collateBadWords (callback) { if (options.externalSwears !== null) { if (localStorageIsEnabled) { if (localStorage.getItem('localSwears') === null) { - // stringify the array so that it can be stored in local storage - localStorage.setItem('localSwears', JSON.stringify(readJsonFromController(options.externalSwears))); + readJsonFromController.call(this, options.externalSwears, function(externalBadWords) { + // stringify the array so that it can be stored in local storage + localStorage.setItem('localSwears', JSON.stringify(externalBadWords)); + externalFileChecked.call(this, externalBadWords); + }); + return; } - badWords = JSON.parse(localStorage.getItem('localSwears')); - } else { - badWords = readJsonFromController(options.externalSwears); + externalFileChecked.call(this, JSON.parse(localStorage.getItem('localSwears'))); + return; } - if (options.customSwears !== null) { - badWords = badWords.concat(options.customSwears).unique(); - } - } else { - if (options.customSwears !== null) { - badWords = options.customSwears; - } - } - - // GET OUT, there are no Swears set either custom, external OR local. - if (badWords === null) { + readJsonFromController.call(this, options.externalSwears, externalFileChecked.bind(this)); return; } + externalFileChecked.call(this, null); - // We've got an array of swears, let's proceed with removing them from the element. - for (i = 0; i < badWords.length; i += 1) { - re = new RegExp('\\b' + badWords[i] + '\\b', 'gi'); + function externalFileChecked(externalBadWords) { + var badWords = []; - 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 (externalBadWords !== null) { + badWords = badWords.concat(externalBadWords).unique(); } - // Text nodes - for (x = 0; x < nodes.length; x += 1) { - if (re.test(nodes[x].nodeValue)) { - profane = true; - data.push(badWords[i]); - if (options.filter) { - nodes[x].nodeValue = nodes[x].nodeValue.replace(re, rep); - } - } + if (options.customSwears !== null) { + badWords = badWords.concat(options.customSwears).unique(); } - // Text input values - for (var x = 0; x < inputs.length; x++) { - if (re.test(inputs[x].value)) { - profane = true; - data.push(badWords[i]); - if (options.filter) { - $(inputs[x]).val(inputs[x].value.replace(re, rep)); - } - } - } + callback.call(this, badWords); } + } - if (profane) { - options.profaneText(data.unique()); - }; + collateBadWords.call(this, function(badWords) { + + // GET OUT, there are no Swears set either custom, external OR local. + if (badWords === null) { + return; + } + + this.each(function () { + + var i, + nodes = allTextNodes(this), + re, + rep, + x, + inputs = $(this).find(':input'), + profane = false, + data = []; + + // We've got an array of swears, let's proceed with removing them from the element. + 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); + } + + // Text nodes + for (x = 0; x < nodes.length; x += 1) { + if (re.test(nodes[x].nodeValue)) { + profane = true; + data.push(badWords[i]); + if (options.filter) { + nodes[x].nodeValue = nodes[x].nodeValue.replace(re, rep); + } + } + } + + // Text input values + for (var x = 0; x < inputs.length; x++) { + if (re.test(inputs[x].value)) { + profane = true; + data.push(badWords[i]); + if (options.filter) { + $(inputs[x]).val(inputs[x].value.replace(re, rep)); + } + } + } + } + + if (profane) { + options.profaneText(data.unique()); + }; + }); }); + + return this; }; })(jQuery); \ No newline at end of file diff --git a/jquery.profanityfilter.js b/jquery.profanityfilter.js index f7480bc..7463806 100644 --- a/jquery.profanityfilter.js +++ b/jquery.profanityfilter.js @@ -42,7 +42,8 @@ $.fn.profanityFilter = function (settings, callback) { var options = $.extend({}, defaults, settings), - localStorageIsEnabled; + localStorageIsEnabled, + badWords; localStorageIsEnabled = function() { var uid = new Date(), @@ -50,7 +51,7 @@ try { localStorage.setItem("uid", uid); - result = localStorage.getItem("uid") == uid; + result = localStorage.getItem("uid") === uid; localStorage.removeItem("uid"); return result && localStorage; } catch(e) {} @@ -85,16 +86,20 @@ return closed; } - function readJsonFromController(file) { + function readJsonFromController(file, callback) { var request = new XMLHttpRequest(); - request.open('GET', file, false); + request.open('GET', file); request.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); + request.onload = function() { + var parsedJson; + try { + parsedJson = JSON.parse(request.responseText); + } catch (e) { + parsedJson = ''; + } + callback.call(this, parsedJson); + }; request.send(null); - try { - return JSON.parse(request.responseText); - } catch (e) { - return ''; - } } var lastRandomNumber = null; @@ -118,83 +123,98 @@ return randomNumber; } - - return this.each(function () { - - var badWords, - i, - nodes = allTextNodes(this), - re, - rep, - x, - inputs = $(this).find(':input'), - profane = false, - data = [], - localSwearsKey = 'localSwears' + options.externalSwears; - + function collateBadWords (callback) { if (options.externalSwears !== null) { if (localStorageIsEnabled) { - var badWordsJSON = localStorage.getItem(localSwearsKey); - if (badWordsJSON === null) { - // stringify the array so that it can be stored in local storage - badWordsJSON = JSON.stringify(readJsonFromController(options.externalSwears)); - localStorage.setItem(localSwearsKey, badWordsJSON); + if (localStorage.getItem('localSwears') === null) { + readJsonFromController.call(this, options.externalSwears, function(externalBadWords) { + // stringify the array so that it can be stored in local storage + localStorage.setItem('localSwears', JSON.stringify(externalBadWords)); + externalFileChecked.call(this, externalBadWords); + }); + return; } - badWords = JSON.parse(badWordsJSON); - } else { - badWords = readJsonFromController(options.externalSwears); + externalFileChecked.call(this, JSON.parse(localStorage.getItem('localSwears'))); + return; } - if (options.customSwears !== null) { - badWords = badWords.concat(options.customSwears).unique(); - } - } else { - if (options.customSwears !== null) { - badWords = options.customSwears; - } - } - - // GET OUT, there are no Swears set either custom, external OR local. - if (badWords === null) { + readJsonFromController.call(this, options.externalSwears, externalFileChecked.bind(this)); return; } + externalFileChecked.call(this, null); - // We've got an array of swears, let's proceed with removing them from the element. - for (i = 0; i < badWords.length; i += 1) { - re = new RegExp('\\b' + badWords[i] + '\\b', 'gi'); + function externalFileChecked(externalBadWords) { + var badWords = []; - 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 (externalBadWords !== null) { + badWords = badWords.concat(externalBadWords).unique(); } - // Text nodes - for (x = 0; x < nodes.length; x += 1) { - if (re.test(nodes[x].nodeValue)) { - profane = true; - data.push(badWords[i]); - if (options.filter) { - nodes[x].nodeValue = nodes[x].nodeValue.replace(re, rep); - } - } + if (options.customSwears !== null) { + badWords = badWords.concat(options.customSwears).unique(); } - // Text input values - for (var x = 0; x < inputs.length; x++) { - if (re.test(inputs[x].value)) { - profane = true; - data.push(badWords[i]); - if (options.filter) { - $(inputs[x]).val(inputs[x].value.replace(re, rep)); - } - } - } + callback.call(this, badWords); } + } - if (profane) { - options.profaneText(data.unique()); - }; + collateBadWords.call(this, function(badWords) { + + // GET OUT, there are no Swears set either custom, external OR local. + if (badWords === null) { + return; + } + + this.each(function () { + + var i, + nodes = allTextNodes(this), + re, + rep, + x, + inputs = $(this).find(':input'), + profane = false, + data = []; + + // We've got an array of swears, let's proceed with removing them from the element. + 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); + } + + // Text nodes + for (x = 0; x < nodes.length; x += 1) { + if (re.test(nodes[x].nodeValue)) { + profane = true; + data.push(badWords[i]); + if (options.filter) { + nodes[x].nodeValue = nodes[x].nodeValue.replace(re, rep); + } + } + } + + // Text input values + for (var x = 0; x < inputs.length; x++) { + if (re.test(inputs[x].value)) { + profane = true; + data.push(badWords[i]); + if (options.filter) { + $(inputs[x]).val(inputs[x].value.replace(re, rep)); + } + } + } + } + + if (profane) { + options.profaneText(data.unique()); + }; + }); }); + + return this; }; -})(jQuery); +})(jQuery); \ No newline at end of file