【问题标题】:How do I use twitter-text in a browser如何在浏览器中使用 twitter-text
【发布时间】:2021-12-03 02:36:34
【问题描述】:

我想计算推文长度。 Twitter 让这变得非常非常困难。

他们有这个 repo:https://github.com/twitter/twitter-text,其中包含这个文件夹:https://github.com/twitter/twitter-text/tree/master/js,据说它支持 javascript。

当我尝试使用这样的 cdn 中的脚本时:https://www.jsdelivr.com/package/npm/twitter-text

上面写着require is not defined

我不明白我应该做什么。如果是用于 javascript,为什么还要使用 require?

【问题讨论】:

  • 因为它是为 node.js 设计的,而您正在尝试从浏览器中使用它。
  • 我不认为你可以使用这样的脚本。按照他们的指示。
  • 你需要使用模块打包器(webpack?rollup?我不确定JS生态系统现在使用什么......)为浏览器编译你的代码。
  • js/pkg/twitter-text-3.1.0.js 可能无需打包程序或 npm 即可工作

标签: javascript node.js twitter


【解决方案1】:
  1. 安装 Browserify
  2. 从此文件夹下载最新的 twitter-text 脚本:https://github.com/twitter/twitter-text/tree/master/js/pkg
  3. 使用以下内容创建index.jswindow.twitter = require('./twitter-text-3.1.0.min.js');
  4. 运行browserify index.js -o twitter-text-js.js

现在可以在浏览器中运行输出文件并使用全局 twitter 访问。

(或直接在此处下载预打包版本:https://gist.github.com/skeddles/3841236279efebf053dfa39dfdbae2c2#file-twitter-text-js-js

【讨论】:

    猜你喜欢
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2017-05-22
    • 2020-12-24
    • 1970-01-01
    • 2015-01-17
    相关资源
    最近更新 更多