From eb0e4110a84c0126f40f1e1b61205357d77d412a Mon Sep 17 00:00:00 2001 From: Adrian Vollmer Date: Mon, 10 Oct 2022 17:41:55 +0200 Subject: [PATCH] Fix anchor links --- zundler/assets/init.js | 7 +++---- zundler/assets/inject.js | 6 ++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/zundler/assets/init.js b/zundler/assets/init.js index fdd42d2..1a00e2c 100644 --- a/zundler/assets/init.js +++ b/zundler/assets/init.js @@ -59,15 +59,14 @@ var load_virtual_page = (function (path, get_params, anchor) { // return True if it worked // return False if loading indicator should be removed right away const file = window.global_context.file_tree[path]; - const data = file.data; var iframe = createIframe(); + + const data = file.data; window.global_context.get_parameters = get_params; if (file.mime_type == 'text/html') { iframe.setAttribute("srcdoc", data); - if (anchor) { - iframe.contentDocument.location.hash = anchor; - } window.global_context.current_path = path; + window.global_context.anchor = anchor; window.history.pushState({path, get_params, anchor}, '', '#'); return true; } else { diff --git a/zundler/assets/inject.js b/zundler/assets/inject.js index b6d3a50..b8ae326 100644 --- a/zundler/assets/inject.js +++ b/zundler/assets/inject.js @@ -64,6 +64,7 @@ var virtual_click = function(evnt) { } else { console.error("Invalid element", el); } + path = normalize_path(path); window.parent.postMessage({ @@ -88,6 +89,8 @@ var fix_links = function() { var fix_link = function(a) { if (is_virtual(a.getAttribute('href'))) { a.addEventListener('click', virtual_click); + } else if (a.getAttribute('href').startsWith('#')) { + a.setAttribute('href', "about:srcdoc" + a.getAttribute('href')) } }; @@ -210,6 +213,9 @@ window.addEventListener("message", (evnt) => { argument: "", }, '*'); } + if (window.global_context.anchor) { + document.location.href = "about:srcdoc#" + window.global_context.anchor; + } } }, false);