Here is the client code itself, it needs to be remade under the library in order to connect it to the NodeJS server

(function () { var t = {}; window.containsMat = function (text) { return t.containsMat(text); }; window.antimat = t; t.badPatterns = [ "^к(о|а)зел$", ]; t.goodPatterns = [ ".*психу.*", ]; t.goodWords = [ "дезмонда", "застрахуйте", "одномандатный", "подстрахуй", "психуй" ]; t.letters = { "a": "а", "b": "в", "c": "с", "e": "е", "f": "ф", "g": "д", "h": "н", "i": "и", "k": "к", "l": "л", "m": "м", "n": "н", "o": "о", "p": "р", "r": "р", "s": "с", "t": "т", "u": "у", "v": "в", "x": "х", "y": "у", "w": "ш", "z": "з", "ё": "е", "6": "б", "9": "д" }; t.containsMat = function (text) { text = t.cleanBadSymbols(text.toLowerCase()); var words = text.split(" "); for (var i = 0; i < words.length; i++) { var word = t.convertEngToRus(words[i]); if (t.isInGoodWords(word) && t.isInGoodPatterns(word)) continue; if (t.isInBadPatterns(word)) return true; } if (t.containsMatInSpaceWords(words)) return true; return false; }; t.convertEngToRus = function (word) { for (var j = 0; j < word.length; j++) { for (var key in t.letters) { if (word.charAt(j) == key) word = word.substring(0, j) + t.letters[key] + word.substring(j + 1, word.length) } } return word; }; t.cleanBadSymbols = function (text) { return text.replace(/[^a-zA-Zа-яА-Яё0-9\s]/g, ""); }; t.isInGoodWords = function (word) { for (var i = 0; i < t.goodWords.length; i++) { if (word == t.goodWords[i]) return true; } return false; }; t.isInGoodPatterns = function (word) { for (var i = 0; i < t.goodPatterns.length; i++) { var pattern = new RegExp(t.goodPatterns[i]); if (pattern.test(word)) return true; } return false; }; t.isInBadPatterns = function (word) { for (var i = 0; i < t.badPatterns.length; i++) { var pattern = new RegExp(t.badPatterns[i]); if (pattern.test(word)) return true; } return false; }; t.containsMatInSpaceWords = function (words) { var spaceWords = t.findSpaceWords(words); for (var i = 0; i < spaceWords.length; i++) { var word = t.convertEngToRus(spaceWords[i]); if (t.isInBadPatterns(word)) return true; } return false; }; t.findSpaceWords = function (words) { var out = []; var spaceWord = ""; for(var i=0; i < words.length; i++ ){ var word = words[i]; if(word.length <= 3){ spaceWord += word; continue; } if(spaceWord.length >= 3){ out.push(spaceWord); spaceWord = ""; } } return out; }; t.addBadPattern = function (pattern) { t.badPatterns.push(pattern); }; t.addGoodPattern = function (pattern) { t.goodPatterns.push(pattern); }; t.addGoodWord = function (pattern) { t.goodWords.push(pattern); }; })(); 
  • what? did not understand you - Members
  • tried something yourself? - Grundy
  • yes, but it didn't work out, because the window is the same client parameter - Members
  • what exactly did you do? - Grundy
  • I tried to use jquery in the window, but it didn’t work, I’m not good at js .. - Members

1 answer 1

In Nodejs, any js file can be used as a module .

However, it has two important properties:

  1. Own scope - variables declared inside the module using the keyword var are added to the module, and not to the global object.
  2. Public fields / methods - using exports you can specify the fields / methods that users of this module can call.

If you look at the code provided, you will notice the following:

  1. The scope is limited to the self-calling function.
  2. public methods of the module are added to the global window object.

As you can see, the items are similar to the ones above.

Since in the module the scope is already limited - you need to remove the self-invoking function.
Next, it remains to simply replace the export through window, for export through exports.

The result is something like this:

 var t = {}; exports.containsMat = function (text) { return t.containsMat(text); }; exports.antimat = t; // далее код без изменений 

Let this code be stored in the file myModule.js , then you can use it like this:

 var myModule = require('./myModule.js'); // ... может быть другой код ... var message = ""; // get from somewhere if(myModule.containsMat(message)){ // obscene detected } 
  • and then when sending, how? if (exports.containsMat (message)) {// obscene detected} - Members
  • @Members, at what dispatch? - Grundy
  • Look at this module on the githaba, github.com/itlessons/js-antimat I meant we arrive from the client's message, then we check this content to find an abusive word if we send one, if not, then another - Members
  • pastebin.com/dTqCQGYx right? - Members
  • thanks a lot, it turned out - Members