Massprice/static/js/app.js
Barely Removable bec399dd11
All checks were successful
Deploy / deploy (push) Successful in 38s
Replace credential auto-login with a Chrome-remote-debugging session capture flow
Live testing against the real site found MassPrice's login is behind
hCaptcha, which silently blocks fully automated username/password
login outright (the form never submits) — no selector fix could work
around this. Also fixed the broken _is_logged_out() heuristic: an
unauthenticated visit to /Rule/List redirects to the public marketing
homepage, not a /login URL, so run_rule/list_rules were silently
scraping the wrong page instead of erroring.

New flow: a Session page lets the user start a headless-but-CDP-exposed
Chromium instance, log in themselves via chrome://inspect (solving the
captcha as a real click), then capture that browser's cookies into the
same auth_state.json scheduled runs already use. Required a socat proxy
in the container image, since Chromium's debug port binds loopback-only
regardless of --remote-debugging-address (confirmed by live testing),
unreachable through Docker's port mapping without it. All Playwright
objects for this now live on one dedicated thread (automation.py's
_debug_worker), since the sync API isn't safe to call across the
arbitrary threads gunicorn's gthread workers hand out per request.

Dashboard now shows a banner when the latest run failed specifically
due to a missing/expired session, rather than just looking like an
empty rule list.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H6TgffxTvYhqxykAbVQg2v
2026-08-31 19:55:08 -07:00

139 lines
4.5 KiB
JavaScript

document.addEventListener("DOMContentLoaded", () => {
const refreshBtn = document.getElementById("refresh-rules");
if (refreshBtn) {
refreshBtn.addEventListener("click", async () => {
refreshBtn.disabled = true;
const original = refreshBtn.textContent;
refreshBtn.textContent = "Refreshing…";
try {
const res = await fetch("/api/rules/refresh", { method: "POST" });
if (!res.ok) {
const data = await res.json().catch(() => ({}));
alert("Refresh failed: " + (data.error || res.status));
return;
}
window.location.reload();
} finally {
refreshBtn.disabled = false;
refreshBtn.textContent = original;
}
});
}
document.querySelectorAll(".rule-card").forEach(initRuleCard);
initSessionControls();
});
function initSessionControls() {
const startBtn = document.getElementById("session-start");
const captureBtn = document.getElementById("session-capture");
const stopBtn = document.getElementById("session-stop");
if (!startBtn) return; // not on the Session page
async function call(url, btn) {
btn.disabled = true;
const original = btn.textContent;
btn.textContent = "Working…";
try {
const res = await fetch(url, { method: "POST" });
const data = await res.json().catch(() => ({}));
if (!res.ok) {
alert(data.error || "Request failed");
btn.disabled = false;
btn.textContent = original;
return;
}
window.location.reload();
} catch (err) {
btn.disabled = false;
btn.textContent = original;
}
}
startBtn.addEventListener("click", () => call("/api/session/start", startBtn));
captureBtn.addEventListener("click", () => call("/api/session/capture", captureBtn));
stopBtn.addEventListener("click", () => call("/api/session/stop", stopBtn));
}
function initRuleCard(card) {
const ruleId = card.dataset.ruleId;
card.querySelector(".rule-enabled").addEventListener("change", (e) => {
saveSchedule(card, { enabledOverride: e.target.checked });
});
card.querySelectorAll(".day-toggle").forEach((btn) => {
btn.addEventListener("click", () => btn.classList.toggle("active"));
});
card.querySelector(".add-schedule").addEventListener("click", () => {
const time = card.querySelector(".new-time").value;
const days = [...card.querySelectorAll(".day-toggle.active")].map((b) => b.dataset.day);
if (!time || days.length === 0) {
alert("Pick a time and at least one day.");
return;
}
addChip(card, time, days);
card.querySelectorAll(".day-toggle.active").forEach((b) => b.classList.remove("active"));
saveSchedule(card);
});
card.querySelectorAll(".chip-remove").forEach((btn) => bindRemove(btn));
card.querySelector(".run-now").addEventListener("click", async (e) => {
const btn = e.currentTarget;
const original = btn.textContent;
btn.disabled = true;
btn.textContent = "Queuing…";
try {
const res = await fetch(`/api/rules/${ruleId}/run`, { method: "POST" });
btn.textContent = res.ok ? "Queued" : "Failed";
} catch (err) {
btn.textContent = "Failed";
} finally {
setTimeout(() => {
btn.textContent = original;
btn.disabled = false;
}, 2000);
}
});
}
function bindRemove(btn) {
btn.addEventListener("click", () => {
const card = btn.closest(".rule-card");
btn.closest(".schedule-chip").remove();
saveSchedule(card);
});
}
function addChip(card, time, days) {
const list = card.querySelector(".schedule-list");
const li = document.createElement("li");
li.className = "schedule-chip";
li.innerHTML =
`<span class="chip-time">${time}</span>` +
`<span class="chip-days">${days.join(" ")}</span>` +
`<button type="button" class="chip-remove" title="Remove">&times;</button>`;
list.appendChild(li);
bindRemove(li.querySelector(".chip-remove"));
}
async function saveSchedule(card, opts = {}) {
const ruleId = card.dataset.ruleId;
const enabled =
opts.enabledOverride !== undefined ? opts.enabledOverride : card.querySelector(".rule-enabled").checked;
const schedules = [...card.querySelectorAll(".schedule-chip")].map((chip) => ({
time: chip.querySelector(".chip-time").textContent.trim(),
days: chip
.querySelector(".chip-days")
.textContent.trim()
.split(/\s+/)
.filter(Boolean),
}));
await fetch(`/api/rules/${ruleId}/schedule`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ enabled, schedules }),
});
}