首页站内杂志技术文摘
文章内容页

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

  • 作者:雨祺
  • 来源: 电脑原创
  • 发表于2025-03-16 21:18:36
  • 阅读0
  • 纯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结合文韵坊即时通讯聊天室敏感词把文章内容敏感词变成***。效果截屏

    【审核人:站长】

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

        本文链接:https://www.wenyunfang.com/zazhi/zhongwangjiaocheng/1268.html

        赞一下

        深度阅读

        • 您也可以注册成为文韵坊的作者,发表您的原创作品、分享您的心情!

        阅读记录

          关注文韵坊