diff --git a/core/basepage.php b/core/basepage.php index b8ffffc9..9aa89091 100644 --- a/core/basepage.php +++ b/core/basepage.php @@ -557,6 +557,7 @@ class BasePage $body_attrs = [ "data-userclass" => $user->class->name, "data-base-href" => get_base_href(), + "data-base-link" => make_link(""), ]; print emptyHTML( diff --git a/ext/autocomplete/script.js b/ext/autocomplete/script.js index 910026dd..289643e0 100644 --- a/ext/autocomplete/script.js +++ b/ext/autocomplete/script.js @@ -43,7 +43,7 @@ function updateCompletions(element) { else { element.completer_timeout = setTimeout(() => { const wordWithoutMinus = word.replace(/^-/, ''); - fetch((document.body.getAttribute("data-base-href") ?? "") + '/api/internal/autocomplete?s=' + wordWithoutMinus).then( + fetch(shm_make_link('api/internal/autocomplete', {s: wordWithoutMinus})).then( (response) => response.json() ).then((json) => { if(element.selected_completion !== -1) { diff --git a/ext/notes/script.js b/ext/notes/script.js index 94aec446..9b4e79bf 100644 --- a/ext/notes/script.js +++ b/ext/notes/script.js @@ -150,7 +150,7 @@ function renderEditor(noteDiv, note) { save.innerText = 'Save'; save.addEventListener('click', () => { if(note.note_id == null) { - fetch('/note/create_note', { + fetch(shm_make_link('note/create_note'), { method: 'POST', headers: { 'Content-Type': 'application/json' @@ -169,7 +169,7 @@ function renderEditor(noteDiv, note) { alert(error); }); } else { - fetch('/note/update_note', { + fetch(shm_make_link('note/update_note'), { method: 'POST', headers: { 'Content-Type': 'application/json' @@ -205,7 +205,7 @@ function renderEditor(noteDiv, note) { deleteNote.innerText = 'Delete'; deleteNote.addEventListener('click', () => { // TODO: delete note from server - fetch('/note/delete_note', { + fetch(shm_make_link('note/delete_note'), { method: 'POST', headers: { 'Content-Type': 'application/json' diff --git a/ext/static_files/init.js b/ext/static_files/init.js index fe6cabf2..0a93530c 100644 --- a/ext/static_files/init.js +++ b/ext/static_files/init.js @@ -4,6 +4,13 @@ function shm_cookie_set(name, value) { function shm_cookie_get(name) { return Cookies.get(name); } +function shm_make_link(page, query) { + let base = (document.body.getAttribute("data-base-link") ?? ""); + let joiner = base.indexOf("?") === -1 ? "?" : "&"; + let url = base + page; + if(query) url += joiner + new URLSearchParams(query).toString(); + return url; +} function shm_log(section, ...message) { window.dispatchEvent(new CustomEvent("shm_log", {detail: {section, message}}));