Files
galaxy/test/functional/webhooks/xkcd/script.js
T
Alireza Heidari 4416f383a4 Port xkcd webhook to vanilla JS with a server-side helper
Rewrite the xkcd webhook script without Backbone, underscore or jQuery.
Add an __init__.py helper that fetches a random comic from the xkcd JSON
API server-side; the previous client-side JSONP call to
http://dynamic.xkcd.com is blocked as mixed content on HTTPS pages and
the API sends no CORS headers. DOM element ids are unchanged so
styles.css still applies.
2026-07-26 17:01:26 +02:00

42 lines
1.7 KiB
JavaScript

// Injected by the webhook framework (see appendScriptStyle in client/src/utils/utils.ts).
// Runs in the global page scope wrapped in an IIFE, so it must be self-contained
// vanilla JS -- Backbone, underscore and jQuery are no longer available globals.
// The comic is fetched through the server-side helper (__init__.py) because the
// xkcd JSON API sends no CORS headers and is only served over HTTPS.
const root = typeof Galaxy !== "undefined" && Galaxy.root ? Galaxy.root : "/";
const container = document.getElementById("xkcd");
if (container) {
container.innerHTML =
'<div id="xkcd-header">' +
'<div id="xkcd-name">xkcd</div>' +
'<button id="xkcd-random" type="button">Random</button>' +
"</div>" +
'<div id="xkcd-img"></div>';
const imgContainer = document.getElementById("xkcd-img");
async function loadRandomComic() {
imgContainer.innerHTML = '<div id="xkcd-loader"></div>';
const url = `${root}api/webhooks/xkcd/data`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.success) {
const img = document.createElement("img");
img.src = data.comic.img;
img.alt = data.comic.alt;
img.title = data.comic.title;
imgContainer.replaceChildren(img);
} else {
console.error(`[xkcd webhook] "${url}":\n${data.error}`);
}
} catch (e) {
console.error(`[xkcd webhook] request to "${url}" failed`, e);
}
}
document.getElementById("xkcd-random").addEventListener("click", loadRandomComic);
loadRandomComic();
}