From 85692feb3433632c01bf9d725323bc6231bd68c0 Mon Sep 17 00:00:00 2001 From: Joe Strong Date: Fri, 12 Oct 2018 18:31:06 +0100 Subject: [PATCH 1/2] Moves collecting bad words outside the elements loop --- demo/jquery.profanityfilter.js | 42 +++++++++++++++----------- jquery.profanityfilter.js | 55 ++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 44 deletions(-) diff --git a/demo/jquery.profanityfilter.js b/demo/jquery.profanityfilter.js index c867a20..d2676f0 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(), @@ -118,19 +119,8 @@ return randomNumber; } - - return this.each(function () { - - var badWords, - i, - nodes = allTextNodes(this), - re, - rep, - x, - inputs = $(this).find(':input'), - profane = false, - data = []; - + function collateBadWords () { + var badWords; if (options.externalSwears !== null) { if (localStorageIsEnabled) { if (localStorage.getItem('localSwears') === null) { @@ -150,10 +140,26 @@ } } - // GET OUT, there are no Swears set either custom, external OR local. - if (badWords === null) { - return; - } + return badWords; + } + + badWords = collateBadWords(); + + // GET OUT, there are no Swears set either custom, external OR local. + if (badWords === null) { + return; + } + + 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) { diff --git a/jquery.profanityfilter.js b/jquery.profanityfilter.js index f7480bc..d2676f0 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) {} @@ -118,29 +119,15 @@ 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 () { + var badWords; if (options.externalSwears !== null) { if (localStorageIsEnabled) { - var badWordsJSON = localStorage.getItem(localSwearsKey); - if (badWordsJSON === null) { + if (localStorage.getItem('localSwears') === null) { // stringify the array so that it can be stored in local storage - badWordsJSON = JSON.stringify(readJsonFromController(options.externalSwears)); - localStorage.setItem(localSwearsKey, badWordsJSON); + localStorage.setItem('localSwears', JSON.stringify(readJsonFromController(options.externalSwears))); } - badWords = JSON.parse(badWordsJSON); + badWords = JSON.parse(localStorage.getItem('localSwears')); } else { badWords = readJsonFromController(options.externalSwears); } @@ -153,10 +140,26 @@ } } - // GET OUT, there are no Swears set either custom, external OR local. - if (badWords === null) { - return; - } + return badWords; + } + + badWords = collateBadWords(); + + // GET OUT, there are no Swears set either custom, external OR local. + if (badWords === null) { + return; + } + + 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) { @@ -197,4 +200,4 @@ }; }); }; -})(jQuery); +})(jQuery); \ No newline at end of file From 8b56a65970e4bd3597a26c57a17c584615d35093 Mon Sep 17 00:00:00 2001 From: Joe Strong Date: Tue, 16 Oct 2018 00:00:12 +0100 Subject: [PATCH 2/2] Makes getting external swears asynchronous --- demo/jquery.profanityfilter.js | 161 ++++++++++++++++++--------------- jquery.profanityfilter.js | 161 ++++++++++++++++++--------------- 2 files changed, 178 insertions(+), 144 deletions(-) diff --git a/demo/jquery.profanityfilter.js b/demo/jquery.profanityfilter.js index d2676f0..7463806 100644 --- a/demo/jquery.profanityfilter.js +++ b/demo/jquery.profanityfilter.js @@ -86,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; @@ -119,85 +123,98 @@ return randomNumber; } - function collateBadWords () { - var badWords; + 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); - } - if (options.customSwears !== null) { - badWords = badWords.concat(options.customSwears).unique(); - } - } else { - if (options.customSwears !== null) { - badWords = options.customSwears; + externalFileChecked.call(this, JSON.parse(localStorage.getItem('localSwears'))); + return; } + readJsonFromController.call(this, options.externalSwears, externalFileChecked.bind(this)); + return; } + externalFileChecked.call(this, null); - return badWords; - } - - badWords = collateBadWords(); - - // GET OUT, there are no Swears set either custom, external OR local. - if (badWords === null) { - return; - } - - return this.each(function () { - - var i, - nodes = allTextNodes(this), - re, - rep, - x, - inputs = $(this).find(':input'), - profane = false, - data = []; + function externalFileChecked(externalBadWords) { + var badWords = []; - // 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); + 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 d2676f0..7463806 100644 --- a/jquery.profanityfilter.js +++ b/jquery.profanityfilter.js @@ -86,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; @@ -119,85 +123,98 @@ return randomNumber; } - function collateBadWords () { - var badWords; + 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); - } - if (options.customSwears !== null) { - badWords = badWords.concat(options.customSwears).unique(); - } - } else { - if (options.customSwears !== null) { - badWords = options.customSwears; + externalFileChecked.call(this, JSON.parse(localStorage.getItem('localSwears'))); + return; } + readJsonFromController.call(this, options.externalSwears, externalFileChecked.bind(this)); + return; } + externalFileChecked.call(this, null); - return badWords; - } - - badWords = collateBadWords(); - - // GET OUT, there are no Swears set either custom, external OR local. - if (badWords === null) { - return; - } - - return this.each(function () { - - var i, - nodes = allTextNodes(this), - re, - rep, - x, - inputs = $(this).find(':input'), - profane = false, - data = []; + function externalFileChecked(externalBadWords) { + var badWords = []; - // 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); + 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