feat: add URL support to TTS for fetching web page text

This commit is contained in:
mitchell 2026-05-12 00:03:17 -04:00
parent 8cb1191fde
commit 91175c70b6
3 changed files with 41 additions and 1 deletions

22
tts.ts
View file

@ -1,7 +1,10 @@
import os from "node:os";
import process from "node:process";
import { parseArgs } from "node:util";
import { Readability } from "@mozilla/readability";
import { $, which } from "bun";
import chalk from "chalk";
import { Window } from "happy-dom-without-node";
const start = Date.now();
@ -10,13 +13,30 @@ const { values: args } = parseArgs({
options: {
wait: { type: "boolean" },
w: { type: "boolean" },
url: { type: "boolean" },
u: { type: "boolean" },
},
strict: true,
allowPositionals: true,
});
const wait = args.wait ?? args.w;
const text = await Bun.stdin.text();
const url = args.url ?? args.u;
let text = await Bun.stdin.text();
if (url) {
const result = await fetch(text.trim());
const window = new Window({ url: text.trim() });
const doc = window.document;
doc.write(await result.text());
await window.happyDOM.waitUntilComplete();
const readable = new Readability(doc).parse();
if (readable?.textContent) text = readable.textContent;
if (text.trim() === result.url) {
console.error("Unable to read web page.");
process.exit(1);
}
}
if (!text) {
console.error("No input given.");