diff options
author | Daniel Schadt <kingdread@gmx.de> | 2024-04-06 22:11:55 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2024-04-06 22:11:55 +0200 |
commit | a4fe67d968f002a8a08ef93eb5e8772dd6cf2116 (patch) | |
tree | f1c573310fcfd2a33c0816d15eab0a076c299617 /asset-sources/fietsboek.ts | |
parent | 12b2d960279d1e6be9b7352b34f04e1cfc72b8b3 (diff) | |
download | fietsboek-a4fe67d968f002a8a08ef93eb5e8772dd6cf2116.tar.gz fietsboek-a4fe67d968f002a8a08ef93eb5e8772dd6cf2116.tar.bz2 fietsboek-a4fe67d968f002a8a08ef93eb5e8772dd6cf2116.zip |
fix URL resolution for AJAX calls
This also fixes a few spots from previous additions.
Diffstat (limited to 'asset-sources/fietsboek.ts')
-rw-r--r-- | asset-sources/fietsboek.ts | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/asset-sources/fietsboek.ts b/asset-sources/fietsboek.ts index e2d7d1f..d70890f 100644 --- a/asset-sources/fietsboek.ts +++ b/asset-sources/fietsboek.ts @@ -1,4 +1,5 @@ declare const FRIENDS_URL: string; +declare const BASE_URL: string; declare const LOCALE: string; /** @@ -39,6 +40,19 @@ function getCookie(name: string): string | undefined { ?.split("=")[1]; } + +/** + * Builds a URL with the correct application URL as base. + * + * Do not add the leading slash, otherwise the resolution will be wrong! + * + * @param path - Path to append to the base. + * @return The correct URL in regards to the application URL. + */ +function makeUrl(name: string): URL { + return new URL(name, BASE_URL); +} + /** * Installs a listener to the given DOM objects. * @@ -392,7 +406,7 @@ addHandler(".summary-toggler", "click", toggleSummary); */ addHandler("#archiveDownloadButton", "click", () => { const checked = document.querySelectorAll(".archive-checkbox:checked"); - const url = new URL("/track/archive", window.location.href); + const url = makeUrl("track/archive"); checked.forEach((c) => { url.searchParams.append("track_id[]", (c as HTMLInputElement).value); }); @@ -455,7 +469,7 @@ function toggleTrackFavourite(event: MouseEvent) { if (trackId === null) { return; } - const url = new URL("/me/toggle-favourite", window.location.href); + const url = makeUrl("me/toggle-favourite"); const formData = new URLSearchParams(); formData.append("track-id", trackId); formData.append("csrf_token", getCookie("csrf_token") ?? ""); @@ -493,7 +507,7 @@ function getLocalizedMonthNames(): string[] { function loadProfileStats() { const monthNames = getLocalizedMonthNames(); - const url = new URL("/me/summary.json", window.location.href); + const url = makeUrl("me/summary.json"); fetch(url) .then(response => response.json()) .then((response: YearSummary) => { |