Readme

Check Bare Bones Tiki to see when their sea light swizzles become available.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { DOMParser, Node } from "https://esm.sh/linkedom@0.16.1";
import { email } from "https://esm.town/v/std/email?v=9";
import { fetchText } from "https://esm.town/v/stevekrouse/fetchText?v=5";
function isHTMLElement(node: Node): node is HTMLElement {
return node.nodeType === Node.ELEMENT_NODE;
}
const URL = "https://www.barebonestiki.com/shop/swizzles";
// Toggle where an email should be sent ONLY when swizzles are available,
// or sent every time this is run, regardless of status.
const SEND_ONLY_WHEN_AVAILABLE = false;
export const webscrapeBareBonesTiki = async () => {
const html = await fetchText(URL);
const document = new DOMParser().parseFromString(html, "text/html");
// Grab every .grid-title, because the products listed on the page have
// identical classes, they're only differentiated by the text inside
const nodeList: NodeListOf<HTMLElement> = document.querySelectorAll(".grid-title");
// Find the title that matches the product we're interested in
const titleNodes = Array.from(nodeList).filter((node) => {
if (isHTMLElement(node)) {
return node.textContent?.trim() === "Sea Light Swizzles";
}
return false;
});
// Traversal time: go up to find the ancestor that contains both this title
// and the "sold out" status
const parent = titleNodes[0]?.closest(".grid-meta-wrapper");
// Then go back down to find the status element
const status = parent.querySelector(".grid-meta-status").textContent.trim();
const isAvailable = status !== "SOLD OUT";
if (!SEND_ONLY_WHEN_AVAILABLE || isAvailable) {
await email({
subject: `Sea Light Swizzles: ${isAvailable ? "Available! 🐡" : "Not Available 🚫"}`,
html: `<a href="${URL}">Sea Light Swizzles</a> are ${isAvailable ? "available!" : "currently sold out."}`,
});
}
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.