Files

117 lines
4.7 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex">
<title>Moving your data</title>
<style>
:root { --bg: #f6f5f1; --fg: #1f1f1d; --muted: #6a6861; --accent: #2f6fde; }
@media (prefers-color-scheme: dark) { :root { --bg: #161615; --fg: #ecebe6; --muted: #9c9a93; --accent: #7aa7ff; } }
body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: var(--bg); color: var(--fg);
font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
main { max-width: 30rem; padding: 24px 16px; }
h1 { font-size: 1.25rem; margin: 0 0 .5rem; }
p { margin: .5rem 0; color: var(--muted); }
a { color: var(--accent); }
label { display: inline-block; margin-top: .75rem; }
</style>
</head>
<body>
<main>
<h1 id="title">Bringing your data over…</h1>
<p id="msg">This takes a second.</p>
<div id="file" hidden>
<label>Backup file from the old address: <input type="file" id="pick" accept="application/json,.json"></label>
</div>
</main>
<script>
// Receives data handed over by the old krburchel.github.io copy of an app and
// stores it for the same app here, then opens the app. It only accepts each
// app's own keys, never overwrites anything already saved here, and never
// accepts sign-in tokens (those are signed in again on the new address).
const RULES = {
"card-collection": { prefixes: ["cc:"], exclude: ["cc:ghToken"] },
"baseballscorecard": { prefixes: ["baseball_scorecard_"], keys: ["sb-theme"] },
"brainfeed": { prefixes: ["bf-"], exclude: ["bf-push-on"] },
"grocery-list": { keys: ["profile", "shoppingMode"] },
"taskboard": { keys: ["taskboard-ui", "taskboard-cache", "taskboard-dirty"] },
"draft-board": { keys: ["dcc.board.autosave.v1"] },
"nfl-picks": { keys: ["nfl-picks-request-id"] },
};
const APPS = ["card-collection", "pokemontracker", "taskboard", "nascar-fantasy", "grocery-list", "brainfeed",
"baseballscorecard", "gamehub-info", "nfl-picks", "draft-board", "stream-log"];
const $ = (id) => document.getElementById(id);
const allowed = (app, k) => {
const r = RULES[app]; if (!r) return false;
if ((r.exclude || []).includes(k)) return false;
return (r.keys || []).includes(k) || (r.prefixes || []).some((p) => k.startsWith(p));
};
async function decode(s) {
const kind = s[0], b64 = s.slice(1).replace(/-/g, "+").replace(/_/g, "/");
const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
if (kind === "p") return JSON.parse(new TextDecoder().decode(bytes));
if (kind === "z") {
const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream("deflate-raw"));
return JSON.parse(await new Response(stream).text());
}
throw new Error("unknown format");
}
function store(app, data) {
let saved = 0, kept = 0;
for (const [k, v] of Object.entries(data || {})) {
if (!allowed(app, k) || typeof v !== "string") continue;
if (localStorage.getItem(k) !== null) { kept++; continue; }
localStorage.setItem(k, v); saved++;
}
return { saved, kept };
}
function target(app, rest) {
const url = new URL("/" + app + "/" + (rest || "").replace(/^\/+/, ""), location.origin);
return url.origin === location.origin && url.pathname.startsWith("/" + app + "/") ? url.href : "/" + app + "/";
}
function fail(app, why) {
$("title").textContent = "Your data didn't come over";
$("msg").innerHTML = "";
$("msg").append(why + " ");
const a = document.createElement("a"); a.href = "/" + app + "/"; a.textContent = "Open the app anyway";
$("msg").append(a);
}
(async () => {
const h = new URLSearchParams(location.hash.slice(1));
const app = h.get("app"), rest = h.get("to") || "", d = h.get("d");
history.replaceState(null, "", location.pathname); // keep the data out of the address bar
if (!APPS.includes(app)) { $("title").textContent = "Nothing to move"; $("msg").textContent = ""; return; }
if (h.get("file") === "1") {
$("title").textContent = "Bring your data over";
$("msg").textContent = "Pick the backup file you saved on the old address.";
$("file").hidden = false;
$("pick").addEventListener("change", async () => {
try {
const data = JSON.parse(await $("pick").files[0].text());
if (data.app !== app) throw new Error("That file is for a different app.");
store(app, data.data);
location.replace(target(app, rest));
} catch (e) { fail(app, e.message || "That file couldn't be read."); }
});
return;
}
try {
if (d) store(app, await decode(d));
location.replace(target(app, rest));
} catch (e) {
fail(app, "Something went wrong reading it (" + (e.message || e) + ").");
}
})();
</script>
</body>
</html>