我试图在我的网站上添加https://github.com/coffee-and-fun/google-profanity-words个,但我看到的唯一 Select 是使用Node.js,这对我来说不是选项,因为我需要在服务器上安装它并从那里使用(如果我错了,请更正我)

我找到了一些像这样的CDN https://www.skypack.dev/view/@coffeeandfun/google-profanity-words但是没能成功.

我试过了—

import { ProfanityEngine } from '@coffeeandfun/google-profanity-words';

let profanity = new ProfanityEngine();

profanity.all(); // returns all bad words as an array.

profanity.search('bad word'); // returns true if the word is found in the list.

但是得到一个错误,ProfanityEngine没有定义.

你能提供一个例子,这在网页上工作吗?

推荐答案

本质上,它是一个包含单词的text file,然后围绕它构建了methods.

所以,你只需要获取txt文件的内容,方法几乎是将list转换为字符串数组(而不是将其用作Web服务,这将需要设置模块和API端点处理程序,这里你可以只发送txt文件,并在前端有简单的JS).

您可以简单地复制/粘贴脚本中的列表,或者您可以在服务器/CDN上托管文件(设置一个定期提取新文件的计划作业(job)),然后使用一个简单的脚本来提取文件,并expose 与原始方法相同的方法:

class MyProfanityEngine {

  constructor(fileUrl) {
    this.fileUrl = fileUrl;
  }

  // fetch the file from your CDN
  async init() {
    try {
      const res = await fetch(this.fileUrl);
      const list = await res.text();
      this.terms = list.split('\n');
      return true;
    } catch (err) {
      throw err;
    }
  }
// use the original methods..

然后在脚本中初始化它:

  // pass file url
  const profanity = new MyProfanityEngine(fileUrl);

  // wait for file fetch and init
  await profanity.init();

  console.log(await profanity.all()); // returns all bad words as an array.

  console.log(await profanity.search('bad word'));

示例:(note that this example uses the original code, and it is just for showcase, you should use your own file CDN)

class MyProfanityEngine {

  constructor(fileUrl) {
    this.fileUrl = fileUrl;
  }

  // fetch the file
  async init() {
    try {
      const res = await fetch(this.fileUrl);
      const list = await res.text();
      this.terms = list.split('\n');
      return true;
    } catch (err) {
      throw err;
    }
  }

  // original methods
  async hasCurseWords(sentence) {

    if (!this.terms?.length) throw new Error('init first');

    const wordsInSentence = sentence.split(/\s+/);
    const lowerCasedTerms = this.terms.map((term) => term.toLowerCase());

    for (const word of wordsInSentence) {
      const lowerCasedWord = word.toLowerCase();
      if (lowerCasedTerms.includes(lowerCasedWord)) {
        return true;
      }
    }

    return false;
  }

  async all() {

    if (!this.terms?.length) throw new Error('init first');

    return this.terms;
  }

  async search(term) {

    if (!this.terms?.length) throw new Error('init first');

    return this.terms.includes(term);
  }
}



(async() => {


  // pass file url, use your own CDN to fetch txt file
  const profanity = new MyProfanityEngine('https://raw.githubusercontent.com/coffee-and-fun/google-profanity-words/main/data/en.txt');

  // wait for file fetch and init
  await profanity.init();

  console.log(await profanity.all()); // returns all bad words as an array.

  console.log(await profanity.search('bad word')); // returns true if the word is found in the list.



})();

Javascript相关问答推荐

如何让\w token 在此RegEx中表现得不贪婪?

使用print This时, map 容器已在LeafletJS中初始化

如何从调整大小/zoom 的SVG路径定义新的d属性?""

我的角模板订阅后不刷新'

我正在建立一个基于文本的游戏在react ,我是从JS转换.我怎样才能使变量变呢?

Chart.js-显示值应该在其中的引用区域

如何将数组用作复合函数参数?

Angular 形式,从DOM中删除不会删除指定索引处的内容,但会删除最后一项

Webpack在导入前混淆文件名

如何在FastAPI中为通过file:/URL加载的本地HTML文件启用CORS?

如何在Java脚本中对列表中的特定元素进行排序?

更新Redux存储中的对象数组

如何在AG-Grid文本字段中创建占位符

如何在脚本编译后直接将RxJ模块导入浏览器(无需Angel、webpack、LiteServer)

JAVASCRIPT SWITCH CASE语句:当表达式为';ALL';

通过解构/功能组件接收props-prop验证中缺少错误"

为什么我的Navbar.css没有显示在本地主机页面上?

在Puppeteer中使用promise进行日志(log)记录时出现TargetCloseError

Firebase函数中的FireStore WHERE子句无法执行

将字符串解释为数字;将其重新编码为另一个基数