💻 Scraping Slack Emojis

April 15, 2020 00:00 • Category: Code Bytes

Say you needed to export all the emojis in your Slack workspace. How would you do that? There isn't (to my knowledge) a way to get them all. My company's workspace has 10k custom emojis and that increases everyday.

The best way I've found is to grab each CDN url and then run it through a wget loop. Slack's emojis CDN is seemingly public, if you have the URL, you have the emoji. Security risk? Possibly, if you store secrets in emojis for some reason.

When you're on the emoji page in Slack, it does one of those continuous loading things as you scroll. When you get to the bottom, more load. This means using the typical getElementsByClassName() method won't suffice when you're scrolling through hundreds of pages of emojis.

We need the MutationObserver. It will monitor for changes in a given element tree and apply a provided callback function to those changes. In our case, more emojis elements are added to the emoji list as we scroll down.

var allEmojis = [];
var slackEmojiDomain = 'emoji.slack-edge.com';
new MutationObserver(() => {
    document.querySelectorAll('[class="p-customize_emoji_list__image"]').forEach((e) => {
        if (e.src.includes(slackEmojiDomain) & !allEmojis.includes(e.src)) {
            allEmojis.push(e.src);
            console.log(`New Emoji Found! We now have ${allEmojis.length}`);
        }
    })
}).observe(document, {childList:true, subtree:true});

Create an array for all the emojis, and start a new MutationObserver that grabs all the emoji elements, checks if the url matches the Slack emoji CDN and if this url is already in our list. Then append that url to the list.

Once you get all the emojis urls, write them to the page and save as a text file.

document.write(allEmojis.join("<br>"))

Once you have the text file, just run it through this bash one-liner and download all the emojis.

cat emojis.txt | while read line; do wget $line; done

Done! Enjoy your emojis 😃