Last active
December 4, 2024 01:21
-
-
Save Yash-Singh1/13844254c28944a50272549b97ea9206 to your computer and use it in GitHub Desktop.
Quick CLI script for Bun to scrape revision history for a JSFiddle by fetching each revision number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { JSDOM } from "jsdom"; | |
import { parseArgs } from "node:util"; | |
import * as fs from "node:fs"; | |
import { execFileSync, spawnSync } from "node:child_process"; | |
const { values, positionals } = parseArgs({ | |
args: Bun.argv, | |
options: { | |
url: { | |
type: "string", | |
short: "u", | |
}, | |
}, | |
strict: true, | |
allowPositionals: true, | |
}); | |
const url = values.url; | |
if (!url) { | |
throw new Error("Specify url"); | |
} | |
const revisionId = Number(url?.split("/").at(-2)); | |
if (fs.existsSync("./fiddle")) { | |
fs.rmSync("./fiddle", { recursive: true, force: true }); | |
} | |
fs.mkdirSync("./fiddle"); | |
execFileSync("git", ["init"], { | |
cwd: "./fiddle", | |
}); | |
for (let i = 1; i <= revisionId; i++) { | |
const currentUrl = url.replace(/\/\d+\/$/, `/${i}/`); | |
const html = await fetch(currentUrl).then((response) => response.text()); | |
const { window } = new JSDOM(html); | |
const scripts = window.document.querySelectorAll("script:not([type])"); | |
for (const script of scripts) { | |
if (script.textContent?.includes("EditorConfig")) { | |
var Track; | |
const EditorConfig = eval(script.textContent! + "\n\nEditorConfig"); | |
fs.writeFileSync("fiddle/index.html", EditorConfig.value.html); | |
fs.writeFileSync("fiddle/index.js", EditorConfig.value.js); | |
fs.writeFileSync("fiddle/index.css", EditorConfig.value.css); | |
} | |
} | |
execFileSync("git", ["add", "."], { | |
cwd: "./fiddle", | |
}); | |
console.log( | |
i, | |
spawnSync("git", ["commit", "-m", "revision " + i, "--allow-empty"], { | |
stdio: ["inherit", "inherit", "pipe"], | |
cwd: "./fiddle", | |
}).stderr.toString() | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment