Files
moloapp/example/assets/agent_sandbox.js
T

40 lines
1.5 KiB
JavaScript
Raw Normal View History

// Agent sandbox page: each form posts its fields as flat JSON to its
// data-route and prints the reply. No state, no timers.
(function () {
"use strict";
var out = document.getElementById("sb-out");
var err = document.getElementById("sb-error");
var cfg = document.getElementById("sb-config");
fetch("/agent/sandbox/status").then(function (r) { return r.json(); }).then(function (s) {
cfg.textContent = s.configured
? "Google credential present. Calls are capped."
: "No Google credential: calls answer 503 until GOOGLE_ACCESS_TOKEN is set.";
}).catch(function () { cfg.textContent = "Status unavailable."; });
Array.prototype.forEach.call(document.querySelectorAll(".sb-form"), function (form) {
form.addEventListener("submit", function (ev) {
ev.preventDefault();
var body = {};
Array.prototype.forEach.call(form.elements, function (el) {
if (el.name && el.value !== "") body[el.name] = el.value;
});
err.textContent = "";
out.textContent = "...";
fetch(form.getAttribute("data-route"), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
}).then(function (r) {
return r.text().then(function (t) { return { ok: r.ok, text: t }; });
}).then(function (res) {
out.textContent = res.text;
if (!res.ok) err.textContent = "Request failed.";
}).catch(function (e) {
out.textContent = "";
err.textContent = String(e);
});
});
});
})();