Search code examples
javascriptnode.jstwitter

How do I use twitter-text in a browser


I want to count tweet length. Twitter makes this very very hard.

They have this repo: https://github.com/twitter/twitter-text which contains this folder: https://github.com/twitter/twitter-text/tree/master/js which supposedly supports javascript.

When I try to use the script from a cdn like this: https://www.jsdelivr.com/package/npm/twitter-text

it says require is not defined

I don't understand what I'm supposed to do. Why is it using require if it's for javascript?


Solution

    1. Install Browserify
    2. Download the latest twitter-text script from this folder: https://github.com/twitter/twitter-text/tree/master/js/pkg
    3. Create index.js with the contents: window.twitter = require('./twitter-text-3.1.0.min.js');
    4. Run browserify index.js -o twitter-text-js.js

    The output file can now be run in browser and accessed with the global twitter.

    (or just download the prepackaged version here: https://gist.github.com/skeddles/3841236279efebf053dfa39dfdbae2c2#file-twitter-text-js-js)