// The frame. Runs on thesalarybox.com inside the employer's page, or in its own window: either popped out of the // frame, or opened from a plain link in a posting (/embed?locator=...), where the candidate copies the token back. // It sends the minimum to /api/bid on this origin and hands only the resulting ticket to the employer's page. // // Messages to the employer's page, all { source: "salarybox", type, ... }: // ready the box has loaded // resize { height } so the employer can size the iframe // ticket { ticket } the candidate's number has been compared; submit this with the application (() => { const $ = (id) => document.getElementById(id); const q = new URLSearchParams(location.search); const locator = q.get("locator") || ""; const parentOrigin = q.get("origin") || "*"; // the ticket reveals nothing, so "*" is safe; a named origin is tidier const standalone = parent === window; // in its own window or tab // The frame that popped this window out, if there is one and it's still open. Reading the origin of any other // opener throws, so a window opened from an employer's link never tries to hand its ticket to that page. const home = () => { try { if (q.get("pop") === "1" && opener && !opener.closed && opener.location.origin === location.origin) return opener; } catch { /* not ours */ } return null; }; const tellParent = (m) => { if (!standalone) parent.postMessage({ source: "salarybox", ...m }, parentOrigin); }; // What the number covers: the readable half of the locator. let basis = ""; try { const [, b, sealed] = locator.split("."); if (!sealed) throw 0; const bin = atob(b.replace(/-/g, "+").replace(/_/g, "/")); basis = new TextDecoder().decode(Uint8Array.from(bin, (c) => c.charCodeAt(0))); } catch { /* handled below */ } if (!basis) { $("form").querySelectorAll("input, button").forEach((el) => (el.disabled = true)); showError("This box was not set up correctly by the employer, so it cannot take a number."); } else { $("basis").textContent = `In ${basis}.`; } // Pop-out: same box, its own window, so the address bar shows where you are. It keeps a link back to this // frame (window.opener) to return the ticket; if the browser drops that link, it shows the token to paste. const popUrl = new URL(location.href); popUrl.searchParams.set("pop", "1"); $("pop").href = popUrl.toString(); if (standalone) $("pop").hidden = true; $("pop").addEventListener("click", (e) => { const w = window.open(popUrl.toString(), "salarybox", "width=520,height=640"); if (w) e.preventDefault(); else $("pasteWrap").hidden = false; // the plain link opens a tab with no way back, so the token comes back by paste }); // A token pasted back from a window that couldn't return it itself. Only the ticket's shape is checked here; // whether it's real is the employer's check. $("paste").addEventListener("input", () => { const t = $("paste").value.trim(); if (/^[A-Za-z0-9_-]{43}$/.test(t)) sealed(t); }); // A ticket coming back from our own pop-out window. addEventListener("message", (e) => { if (e.origin !== location.origin || e.data?.source !== "salarybox" || e.data.type !== "relay") return; sealed(e.data.ticket); }); // Digits only, shown with separators. const input = $("min"); const digits = () => input.value.replace(/\D/g, ""); input.addEventListener("input", () => { const d = digits().slice(0, 12); input.value = d ? Number(d).toLocaleString("en-US") : ""; }); $("form").addEventListener("submit", async (e) => { e.preventDefault(); const minimum = digits(); if (!minimum || Number(minimum) <= 0) return showError("Enter a number."); $("go").disabled = true; showError(""); try { const r = await fetch("/api/bid", { method: "POST", body: new URLSearchParams({ locator, minimum }) }); input.value = ""; // the page no longer holds the number either way if (!r.ok) throw 0; const { ticket } = await r.json(); const frame = home(); if (frame) { frame.postMessage({ source: "salarybox", type: "relay", ticket }, location.origin); sealed(ticket, "The token has gone back to the application, and you can close this window."); setTimeout(() => window.close(), 1800); } else if (standalone) { sealed(ticket, "Copy this token and paste it into the application. It tells the company only whether your number is above or below the budget it entered.", true); } else { sealed(ticket); } } catch { showError("That did not go through, and nothing was sent to the company. Please try again."); } finally { $("go").disabled = false; } }); function sealed(ticket, text, showCopy = false) { $("form").hidden = true; $("done").hidden = false; if (text) $("doneText").textContent = text; $("copyWrap").hidden = !showCopy; if (showCopy) { $("ticket").value = ticket; $("ticket").select(); } tellParent({ type: "ticket", ticket }); } $("copy").addEventListener("click", async () => { $("ticket").select(); try { await navigator.clipboard.writeText($("ticket").value); $("copy").textContent = "Copied"; } catch { /* it's selected, so Ctrl+C works */ } }); function showError(msg) { $("err").textContent = msg; $("err").hidden = !msg; } new ResizeObserver(() => tellParent({ type: "resize", height: document.documentElement.scrollHeight })).observe(document.body); tellParent({ type: "ready" }); })();