1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { unescape } from "https://deno.land/std/html/entities.ts";
const isValidKey = (key: string) => /^[A-Za-z0-9]+$/.test(key);
export const set = async (key: string, value = "") => {
if (!isValidKey(key)) throw new Error(`Invalid note.ms key`);
const res = await fetch(`https://note.ms/${key}`, {
headers: {
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
Referer: `https://note.ms/${key}`,
},
body: new URLSearchParams({ t: value }),
method: "POST",
});
return res.ok;
};
export const get = async (key: string) => {
if (!isValidKey(key)) throw new Error(`Invalid note.ms key`);
const res = await fetch(`https://note.ms/${key}`, { headers: { Referer: `https://note.ms/${key}` } });
if (!res.ok) throw new Error(res.statusText);
const matched = (await res.text()).match(/<textarea class="content">([\s\S]*?)<\/textarea>/);
if (!matched) throw new Error("Unexpected error when matching <textarea>");
const raw = matched[1];
return unescape(raw);
};
export const notems = (key: string, val: string) => (typeof val === "string" ? set(key, val) : get(key));
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v4
December 16, 2023