From efce40355df8f9fc1250ac947acee243fca5dc1f Mon Sep 17 00:00:00 2001 From: mtmail Date: Wed, 3 Mar 2021 14:52:29 +0100 Subject: [PATCH] make sure webpages still work on file:// protocol (#94) --- src/lib/api_utils.js | 24 +++++++++++++++++------- src/lib/stores.js | 8 +++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/lib/api_utils.js b/src/lib/api_utils.js index a44a30e..9dff877 100644 --- a/src/lib/api_utils.js +++ b/src/lib/api_utils.js @@ -33,17 +33,27 @@ export async function fetch_from_api(endpoint_name, params, callback) { var fetch_content_cache = {}; export async function fetch_content_into_element(url, dom_element) { + if (!window.location.protocol.match(/^http/)) { + dom_element.innerHTML = `Cannot display data from ${url} here. ` + + 'Browser security prevents loading content from file:// URLs.'; + return; + } + if (fetch_content_cache[url]) { dom_element.innerHTML = fetch_content_cache[url]; return; } - await fetch(url) - .then(response => response.text()) - .then(html => { - html = html.replace('Nominatim_API_Endpoint', Nominatim_Config.Nominatim_API_Endpoint); - dom_element.innerHTML = html; - fetch_content_cache[url] = html; - }); + try { + await fetch(url) + .then(response => response.text()) + .then(html => { + html = html.replace('Nominatim_API_Endpoint', Nominatim_Config.Nominatim_API_Endpoint); + dom_element.innerHTML = html; + fetch_content_cache[url] = html; + }); + } catch (error) { + dom_element.innerHTML = `Error fetching content from ${url} (${error})`; + } } function generate_nominatim_api_url(endpoint_name, params) { diff --git a/src/lib/stores.js b/src/lib/stores.js index 7ada776..00b4195 100644 --- a/src/lib/stores.js +++ b/src/lib/stores.js @@ -34,7 +34,13 @@ export function refresh_page(pagename, params) { if (param_str) { param_str = '?' + param_str; } - window.history.pushState([], '', pagename + '.html' + param_str); + let new_url = pagename + '.html' + param_str; + + if (window.location.protocol.match(/^http/)) { + window.history.pushState([], '', new_url); + } else { + window.location.href = new_url; + } } page.set({ tab: pagename, params: params }); -- 2.39.5