// Name: Unescape Clipboard String // Description: Convert JSON-style escaped text on the clipboard into proper HTML and copy it back. // Author: tayiorbeii // GitHub: tayiorbeii import "@johnlindquist/kit" const input = await clipboard.readText() if (!input || !input.trim()) { await notify({ title: "Unescape Clipboard", body: "Clipboard is empty or not text." }) exit() } const tryJsonParse = (s: string): string | null => { try { const v = JSON.parse(s) return typeof v === "string" ? v : null } catch { return null } } const unescapeFromLiteral = (s: string): string => { // Build a safe JSON string literal without altering backslashes that form escapes const safe = s.replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r") try { return JSON.parse(`"${safe}"`) } catch { return s } } const manualUnescape = (s: string): string => { return s .replace(/\\r\\n/g, "\r\n") .replace(/\\n/g, "\n") .replace(/\\t/g, "\t") .replace(/\\r/g, "\r") .replace(/\\f/g, "\f") .replace(/\\b/g, "\b") .replace(/\\"/g, '"') .replace(/\\'/g, "'") .replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))) .replace(/\\\\/g, "\\") } let output = tryJsonParse(input) if (output === null) output = unescapeFromLiteral(input) if (output === input) output = manualUnescape(input) await clipboard.writeText(output) await toast("Unescaped HTML copied to clipboard")