stevekrouse-xeval.web.val.run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { eval_ } from "https://esm.town/v/stevekrouse/eval_";
import { fetchTweet } from "https://esm.town/v/dpetrouk/fetchTweet?v=35";
export let xeval = async (req: Request) => {
let tweet, code, result;
try {
tweet = await fetchTweet(req.url);
code = tweet.text.split("```")[1]
.trim()
.replaceAll(/&lt;/g, "<")
.replaceAll(/&gt;/g, ">")
.replaceAll(/&amp;/g, "&");
result = await eval_(code, [req]);
return result;
}
catch (e) {
return Response.json({ code, tweet, result }, { status: 500 });
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
8
pomdtr avatar

It would be cool to have a bot for this, that answer with the output

vladimyr avatar

FYI this won't work until this PR gets merged because Twitter/X now requires an additional magical token to be sent to the tweet fetching/syndication endpoint ⚠️

Luckily folks from Vercel reverse-engineered it as part of their react-tweet package: https://github.com/vercel/react-tweet/blob/a292ca7/packages/react-tweet/src/api/fetch-tweet.ts#L27-L31

stevekrouse avatar

Great point!

vladimyr avatar

I couldn't resist extending it to support Bluesky posts too, so here it is https://www.val.town/v/vladimyr/postEval 🎉

I had to change the URL format slightly to include the domain and also made it so that you can simply prepend https://vladimyr-posteval.web.val.run/ in front of the post URL. To give you an example:

  1. using the original @stevekrouse's tweet: https://twitter.com/stevekrouse/status/1687468019880964097 https://vladimyr-posteval.web.val.run/https://twitter.com/stevekrouse/status/1687468019880964097 which will redirect you to: https://vladimyr-posteval.web.val.run/twitter.com/stevekrouse/status/1687468019880964097
  2. using my Bluesky post: https://bsky.app/profile/vladimyr.bsky.social/post/3kmjorjmtae2u https://vladimyr-posteval.web.val.run/https://bsky.app/profile/vladimyr.bsky.social/post/3kmjorjmtae2u which will redirect you to: https://vladimyr-posteval.web.val.run/bsky.app/profile/vladimyr.bsky.social/post/3kmjorjmtae2u

To make it even easier to play with it here is a bookmarklet that will eval post you are currently viewing: postEval source code:

window.location = new URL(`/${document.URL}`, "https://vladimyr-posteval.web.val.run/");
vladimyr avatar

Since my bookmarklet code got sanitized:

javascript:(function()%7Bwindow.location%20%3D%20new%20URL(%60%2F%24%7Bdocument.URL%7D%60%2C%20%22https%3A%2F%2Fvladimyr-posteval.web.val.run%2F%22)%3B%7D)()%3B
stevekrouse avatar

This is awesome!!!

stevekrouse avatar

And your code is so simple

vladimyr avatar

Thanks! When it comes to code my personal philosophy is unless it looks and reads like haiku - it sucks 😁 Coincidentally it fits nicely into the following narrative: https://en.wikiquote.org/wiki/Brian_Kernighan#:~:text=Everyone%20knows%20that,edition%2C%20chapter%202.

October 23, 2023