Using eval + urlfetch to read a plaintext page written in JavaScript

I was wondering if it is possible to write a document that follows JavaScript context in, for example, pastebin.com and use eval + urlfetch to read the JavaScript and create a command.

I want to create a nested Array that depending on the user’s input along with the command, will return a specific place inside the nested array.

For example: Having an array called “characters” full of a list of character names. A twitch chatter calls the command “!frames Ryu 1” all the command will use urlfetch to create a nested array to search for “Ryu” then go into that element’s array and access the 2nd item there and return it.

Currently I have a much more time consuming way of producing essentially the same results by creating individual pastebin pages and creating individual commands for each character. I was just hoping to consolidate it more, make it a bit more fancy ya know?

Thanks for any help!

@runeclawdota

A JSON object with arrays could help you pull off what you need.


characters={"ryuzaki":["shrewd","stoic","reclusive"],"light":["ambitious","cunning","hubristic"],"misa":["ditzy","childish","impulsive"]};

This is a sample JSON object called “characters”. Each property in “characters” is the name of a character, and each character name has an associated array of character traits.

characters[`light`][1]

The above value is equivalent to “cunning”


Create a file on https://pastebin.com following the format as my “characters” JSON object. However, since searching for values in this object using user input is case-sensitive, it would be a good idea to enter your characters’ names in all caps. If you were to also convert your search input string to uppercase, this would provide greater flexibility in searching for values. Once you’ve created your Pastebin file, copy and paste the following command setup into chat. Replace the link in the command response with your raw Pastebin file link:

!addcom -cd=5 !frames $(eval d=decodeURIComponent;a=JSON.parse(`$(urlfetch json https://pastebin.com/raw/fileName)`);try{a[d(`$(querystring $(1))`)][d(`$(querystring $(2))`)];}catch(e){`Invalid input!`;})

Usage: !frames [character name] [number of item in array, starting from 0]

First of all, I love the Death Note example! Secondly, thanks for helping me out here. And thirdly, I created a test pastebin file using only 3 of the characters and my total desired “traits” (which in my sense is frame data) using your exact command of !frames I got back an “Unexpected token c in JSON at position 0” return from nightbot.

Does it have to do with me starting my pastebin with "characters = "? Also here is the paste bin https://pastebin.com/yWy3BFQG

1 Like

There’s nothing wrong with your pastebin file. There’s a few missing parts missing in the JS code I wrote to make it all work. Here’s a version of it that should work. Copy and paste this into chat. I already included your pastebin file link:

!addcom -cd=5 !frames $(eval d=decodeURIComponent;$(urlfetch json https://pastebin.com/raw/yWy3BFQG)try{characters[d(`$(querystring $(1))`).toUpperCase()][parseInt(d(`$(querystring $(2))`))];}catch(e){`Invalid input!`;})

A little note about the pastebin file: Since with this method, you can store as many characters into your file as you wish. Just keep in mind to stick to the format of the JSON object:

characters={"character 1":["item 1","item 2","item 3","etc..."],"character 2":["item 1","item 2","item 3","etc..."],"character 3":["item 1","item 2","item 3","etc..."],"character N...":["item 1","item 2","item 3","etc..."]};

Awesome! Works perfectly! Thanks so much.

And yea, I was just wanting to confirm it worked before putting in the grind of typing out all the information for every character.

Last question! How deep can I nest the JSON object? Also, what would be a great resource to learn basic JSON coding? This seems like something I should start becoming familiar with. Thanks again!

There’s no theoretical limit to how many levels of nesting you can create for a JSON object, though you’ll probably at least hit some practical limit soon enough (Nightbot’s script execution timeout is 1 second).


JSON stands for JavaScript Object Notation. JSON isn’t its own programming language; rather, it is a subset of JavaScript that provides a format for storing information that is easily accessible and readable for humans.


There are many places on the internet where you can learn about this particular subset of JavaScript. One resource I find myself visiting quite often to refresh my knowledge of JS is https://www.w3schools.com/jsref/default.asp

Located on that page is a reference for JSON. Enjoy!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.