Add JSONP support on other endpoints

Refactored the code to define and process endpoints rather than doing
it imperatively, which avoided a lot of duplicated code.
This commit is contained in:
Cadence Ember
2025-11-17 15:18:54 +13:00
parent 7b2f96eb03
commit b7fe180790
12 changed files with 518 additions and 282 deletions

View File

@@ -37,23 +37,29 @@ window.proxy = new Proxy(jsonpData, {
// *** Data upload and download
async function cont() {
if (jsonpData.wikipage?.error) return error(jsonpData.wikipage)
if (jsonpData.siteinfo?.error) return error(jsonpData.siteinfo)
if (!(jsonpData.wikipage && jsonpData.siteinfo)) return
// Check for errors
for (const v of Object.values(jsonpData)) {
if (v.error) return error(v)
}
// Check for completion
const dependencies = [...document.querySelectorAll("[data-jsonp-var]")].map(e => e.getAttribute("data-jsonp-var"))
for (const d of dependencies) {
if (!(d in jsonpData)) return
}
const xhr = new XMLHttpRequest();
const uploadFraction = 0.7
const pkg = {
url: "/api/render/wiki",
url: location.href,
init: {
method: "POST",
body: JSON.stringify({
data: jsonpData.wikipage,
siteinfo: jsonpData.siteinfo,
wikiname: BWData.wikiname,
path: BWData.path
path: BWData.path,
...jsonpData
})
}
}
@@ -127,7 +133,7 @@ function error(data) {
if (typeof data === "string") {
render(html`<p><strong>BreezeWiki ran into an error on this page.</strong></p><p>Try reloading the page.</p><p>If this keeps happening, you could <a href="mailto:~cadence/breezewiki-discuss@lists.sr.ht">send a public bug report</a>. Please include the following information:</p><pre>URL: ${window.location.href}${"\n"}${data}</pre>`, eContent)
} else if (data.error.code === "missingtitle") {
render(html`<p><strong>This page doesn't exist on Fandom.</strong></p><p><small><a href="/${BWData.wikiname}/wiki/Main_Page">Return to the homepage?</a></small></p>`, eContent)
render(html`<p><strong>This page doesn't exist on Fandom.</strong></p><p><small><a href="/${BWData.wikiname}/wiki/Main_Page">Return to the wiki's main page</a></small></p>`, eContent)
} else {
render(html`<p>BreezeWiki wasn't able to load this page.</p><p><strong>${data.error.code}: ${data.error.info}</strong></p>`, eContent)
}