From 820bd4d5d6fe33df597b5a227eca262ee2101654 Mon Sep 17 00:00:00 2001 From: Fredrik Lindseth Date: Tue, 28 Apr 2026 19:07:42 +0200 Subject: [PATCH] Drop broken CORS proxy, use MediaWiki API and direct fetch The Cloudflare Worker proxy at production.osmno-cors-proxy.mathiash98.workers.dev forwards a gzipped upstream body without setting Content-Encoding: gzip, so browsers fail with ERR_CONTENT_DECODING_FAILED on every progress page (see #3). Drop the proxy entirely. Both data sources already serve CORS: - wiki.openstreetmap.org exposes the MediaWiki action API with origin=* (Access-Control-Allow-Origin: *). Use action=parse&prop=text and pull the rendered HTML from parse.text["*"]. - obtitus.github.io is GitHub Pages, which sends Access-Control-Allow-Origin: * for static files. Fetch directly. This removes a single point of failure hosted on a personal Cloudflare account and shortens the request path. --- main.js | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/main.js b/main.js index 4ac00d8..c3412b8 100644 --- a/main.js +++ b/main.js @@ -537,29 +537,37 @@ function getAvg(nums) { } /** + * Fetch a progress page and parse its first into JSON. * - * @param {string} url + * For wiki.openstreetmap.org we hit the MediaWiki action API with origin=* + * for CORS. For other hosts we fetch the page directly — works as long as + * the host sends Access-Control-Allow-Origin (GitHub Pages does). + * + * @param {string} hostname * @param {string} path * @returns */ -async function convertWikiToJson(url, path) { - const resp = await fetch( - `https://production.osmno-cors-proxy.mathiash98.workers.dev/${path}?url=${url}` - ); - - if (resp.ok) { - // parse html table and get percentage for each kommune - const progressDocumentText = await resp.text(); - const progressHtml = new DOMParser().parseFromString( - progressDocumentText, - "text/html" - ); - const progressTable = progressHtml.querySelector("table"); - const tableAsJson = parseHTMLTableElem(progressTable); - return tableAsJson; +async function convertWikiToJson(hostname, path) { + let html; + if (hostname === "wiki.openstreetmap.org") { + const page = path.replace(/^wiki\//, ""); + const apiUrl = `https://${hostname}/w/api.php?action=parse&page=${encodeURIComponent( + page + )}&prop=text&format=json&origin=*`; + const resp = await fetch(apiUrl); + if (!resp.ok) throw new Error(resp.statusText); + const json = await resp.json(); + if (json.error) throw new Error(json.error.info); + html = json.parse.text["*"]; } else { - throw new Error(resp.statusText); + const resp = await fetch(`https://${hostname}/${path}`); + if (!resp.ok) throw new Error(resp.statusText); + html = await resp.text(); } + + const progressHtml = new DOMParser().parseFromString(html, "text/html"); + const progressTable = progressHtml.querySelector("table"); + return parseHTMLTableElem(progressTable); } /**