diff --git a/src/init.js b/src/init.js index 1b73b5e..02d3eb8 100644 --- a/src/init.js +++ b/src/init.js @@ -68,6 +68,7 @@ var load_virtual_page = (function (path, get_params, anchor) { iframe.contentDocument.location.hash = anchor; } window.data.current_path = path; + //TODO fix history }); window.onload = function() { diff --git a/src/inject.js b/src/inject.js index b81172a..047eca6 100644 --- a/src/inject.js +++ b/src/inject.js @@ -1,4 +1,4 @@ -var embed_css = function(origin) { +var embed_css = function() { Array.from(document.querySelectorAll("link")).forEach( link => { if (link.getAttribute('rel') == 'stylesheet') { const style = document.createElement("style"); @@ -12,7 +12,7 @@ var embed_css = function(origin) { }; -var embed_js = function(origin) { +var embed_js = function() { Array.from(document.querySelectorAll("script")).forEach( oldScript => { const newScript = document.createElement("script"); Array.from(oldScript.attributes).forEach( attr => { @@ -77,15 +77,19 @@ var virtual_click = function(evnt) { return false; }; -var fix_links = function(origin) { +var fix_links = function() { Array.from(document.querySelectorAll("a")).forEach( a => { - if (is_virtual(a.getAttribute('href'))) { - a.addEventListener('click', virtual_click); - } + fix_link(a); }); }; -var fix_forms = function(origin) { +var fix_link = function(a) { + if (is_virtual(a.getAttribute('href'))) { + a.addEventListener('click', virtual_click); + } +}; + +var fix_forms = function() { Array.from(document.querySelectorAll("form")).forEach( form => { var href = form.getAttribute('action'); if (is_virtual(href) && form.getAttribute('method').toLowerCase() == 'get') { @@ -95,7 +99,7 @@ var fix_forms = function(origin) { }; -var embed_img = function(origin) { +var embed_img = function() { Array.from(document.querySelectorAll("img")).forEach( img => { if (img.hasAttribute('src')) { const src = img.getAttribute('src'); @@ -159,6 +163,15 @@ var normalize_path = function(path) { }; +var fix_document = function() { + embed_js(); // This might change the DOM, so do this first + embed_css(); + embed_img(); + fix_links(); + fix_forms(); +}; + + // Set up message listener window.addEventListener("message", (evnt) => { console.log("Received message in iframe", evnt); @@ -167,11 +180,7 @@ window.addEventListener("message", (evnt) => { console.log("Received data from parent", window.data); // dynamically fix elements on this page try { - embed_js(); // This might change the DOM, so do this first - embed_css(); - fix_links(); - fix_forms(); - embed_img(); + fix_document(); // Trigger DOMContentLoaded again, some scripts that have just // been executed expect it. window.document.dispatchEvent(new Event("DOMContentLoaded", { @@ -188,7 +197,23 @@ window.addEventListener("message", (evnt) => { }, false); -// Set parent window title +const observer = new MutationObserver((mutationList) => { + // console.log("Fix mutated elements...", mutationList); + mutationList.forEach((mutation) => { + if (mutation.type == 'childList') { + Array.from(mutation.target.querySelectorAll("a")).forEach( a => { + fix_link(a); + }); + Array.from(mutation.target.querySelectorAll("img")).forEach( img => { + // embed_img TODO + }); + } + }); +}); +observer.observe(window.document.body, {subtree: true, childList: true}); + + +// Set parent window title and trigger data transmission window.parent.postMessage({ action: "set_title", argument: window.document.querySelector('head>title').innerText diff --git a/src/monkeypatch.js b/src/monkeypatch.js index f579e82..65cef0b 100644 --- a/src/monkeypatch.js +++ b/src/monkeypatch.js @@ -17,14 +17,13 @@ var myGet = function (arg) { const originalResult = originalGet.apply(this, [arg]); // If searchtools.js of sphinx is used if ( - (arg == "q" || arg == "highlight") && - window.DOCUMENTATION_OPTIONS && - window.Scorer && window.data.get_parameters && - (! originalResult) + (window.location.search === "") && + (Array.from(this.entries()).length == 0) ) { const params = new URLSearchParams('?' + window.data.get_parameters); - const result = params.get("q"); + const result = params.get(arg); + // console.log("Return virtual get parameter:", arg, result); return result; } else { return originalResult;