技术文摘

(干货)纯js结合文韵坊即时通讯聊天室敏感词把文章内容敏感词变成***

作者:雨祺   发表于:
浏览:85次    字数:881  电脑原创
级别:站长   总稿:79篇, 月稿:10
纯js结合文韵坊即时通讯聊天室敏感词把文章内容敏感词变成***.
看看我们的聊天室手动设置的敏感词哈。


那我们前端就首要AJAX请求敏感词H5会话缓存上。比如小编的代码

  1. //敏感词加载(来源于聊天室) 
  2. $.post('/e/extend/chat/', { enews: "mingganci" }, function(json) { 
  3. sessionStorage.setItem('chatuser_ai_minganci', JSON.stringify(json.minganci)); 
  4. }, 'json'); 
接下来就好办了,毕竟我们已经把敏感词会话保存到用户的浏览器了,那我们通过JQ获取文章的标题与内容,在自定义个函数进行替换就OK了,比如小编的代码
  1. function replaceSensitiveWords() { 
  2. let forbiddenWordsStr = sessionStorage.getItem('chatuser_ai_minganci'); 
  3. let forbiddenWords = []; 
  4. if (forbiddenWordsStr) { 
  5. forbiddenWords = forbiddenWordsStr.split(',').map(word => word.trim()); 
  6. let booknameContent = $('#bookname').text(); 
  7. let showContent = $('#content').html(); 
  8. let combinedRegex; 
  9. if (forbiddenWords.length > 0) { 
  10. combinedRegex = new RegExp(forbiddenWords.filter(word => word).join('|'), 'ig'); 
  11. function replaceInText(text) { 
  12. let hasSensitiveWord = false
  13. if (combinedRegex) { 
  14. if (combinedRegex.test(text)) { 
  15. hasSensitiveWord = true
  16. text = text.replace(combinedRegex, '***'); 
  17. if (hasSensitiveWord) { 
  18. setTimeout(() => { 
  19. layer.msg("有敏感词,已替换", { icon: 5 }); 
  20. }, 100); 
  21. return text; 
  22. booknameContent = replaceInText(booknameContent); 
  23. showContent = replaceInText(showContent); 
  24. $(function () { 
  25. $('#bookname').text(booknameContent); 
  26. $('#content').html(showContent); 
  27. }); 
敏感词自定义函数也完成了,最后我们调用这个自定义函数
  1. replaceSensitiveWords(); 
这样我们就完成了纯js结合文韵坊即时通讯聊天室敏感词把文章内容敏感词变成***。效果截屏

【审核人:站长】

99Ai聊天   收藏   加好友   海报   39分享
点赞(0)
打赏
标签:即时通讯聊天室干货

发布者资料

热门文章

技术文摘

查看更多技术文摘
    首页
    栏目
    搜索
    会员
    投稿