简体   繁体   English

聊天消息不会通过过滤器发送

[英]Chat Messages Will Not Send With Filter

Me and my friend are making a chat room for students at our school to use during free time and stuff.我和我的朋友正在为我们学校的学生建造一个聊天室,供他们在空闲时间使用。 I am trying to add a filter so that it will remain SFW.我正在尝试添加一个过滤器,以便它保持 SFW。 I am very new to JavaScript so when I added the filter you couldn't send messages in the chat anymore.我对 JavaScript 非常陌生,所以当我添加过滤器时,您无法再在聊天中发送消息。 However when I make the filter script a comment and disable it, the messages send just fine.但是,当我使过滤器脚本成为评论并禁用它时,消息发送得很好。

Filter Code:过滤器代码:

    var array = array.js
message.replace(array, "****");

Messaging Code:消息代码:

        if (message && connected) {
  $inputMessage.val("");
  addChatMessage({
    username: username,
    message: message
  });

  socket.emit("new message", message);
}}

My friend is responsible for the Messaging code since he is much more experienced at JavaScript, but he is very busy and can't add it right now so I am trying to.我的朋友负责消息传递代码,因为他在 JavaScript 方面更有经验,但他很忙,现在无法添加,所以我正在尝试。

Assuming you have a array to filter which is array you could split your message by white space and then check each word to see if it contains a bad word.假设您有一个数组来过滤哪个array ,您可以用空格分割您的message ,然后检查每个单词以查看它是否包含坏词。

Two methodologies (FYI neither is perfect. both of these solutions have plenty of edge cases/loopholes: I'd recommend a js package that has more thought put into it npm: bad-words , npm: censor-sensor , or npm: swearhar ) Two methodologies (FYI neither is perfect. both of these solutions have plenty of edge cases/loopholes: I'd recommend a js package that has more thought put into it npm: bad-words , npm: censor-sensor , or npm: swearhar )

Both split the message message.split(' ') and then iterate over each word to see if it needs replacing .map .两者都拆分消息message.split(' ')然后遍历每个单词以查看它是否需要替换.map The first checks to see if the current word is in array and the second sees if array words contain the current word, then if so replace with ****第一个检查当前单词是否在array中,第二个检查array单词是否包含当前单词,如果是则替换为****

 const array = ['badword', 'nsfw'] let message = 'my message is full of badwords which may be nsfw' message = message.split(' ').map(word => array.includes(word.toLowerCase())? '*****': word).join(' ') console.log(message) message = 'my message is full of badwords which may be nsfw' message = message.split(' ').map(word => array.some(nsfword=>word.toLowerCase().includes(nsfword))? '*****': word).join(' ') console.log(message)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM