Properly monkeypatch get parameters

This commit is contained in:
Adrian Vollmer 2022-09-25 17:45:04 +02:00
parent 2489e8ab31
commit 991a853e8b
3 changed files with 44 additions and 19 deletions

View File

@ -68,6 +68,7 @@ var load_virtual_page = (function (path, get_params, anchor) {
iframe.contentDocument.location.hash = anchor; iframe.contentDocument.location.hash = anchor;
} }
window.data.current_path = path; window.data.current_path = path;
//TODO fix history
}); });
window.onload = function() { window.onload = function() {

View File

@ -1,4 +1,4 @@
var embed_css = function(origin) { var embed_css = function() {
Array.from(document.querySelectorAll("link")).forEach( link => { Array.from(document.querySelectorAll("link")).forEach( link => {
if (link.getAttribute('rel') == 'stylesheet') { if (link.getAttribute('rel') == 'stylesheet') {
const style = document.createElement("style"); 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 => { Array.from(document.querySelectorAll("script")).forEach( oldScript => {
const newScript = document.createElement("script"); const newScript = document.createElement("script");
Array.from(oldScript.attributes).forEach( attr => { Array.from(oldScript.attributes).forEach( attr => {
@ -77,15 +77,19 @@ var virtual_click = function(evnt) {
return false; return false;
}; };
var fix_links = function(origin) { var fix_links = function() {
Array.from(document.querySelectorAll("a")).forEach( a => { Array.from(document.querySelectorAll("a")).forEach( a => {
if (is_virtual(a.getAttribute('href'))) { fix_link(a);
a.addEventListener('click', virtual_click);
}
}); });
}; };
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 => { Array.from(document.querySelectorAll("form")).forEach( form => {
var href = form.getAttribute('action'); var href = form.getAttribute('action');
if (is_virtual(href) && form.getAttribute('method').toLowerCase() == 'get') { 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 => { Array.from(document.querySelectorAll("img")).forEach( img => {
if (img.hasAttribute('src')) { if (img.hasAttribute('src')) {
const src = img.getAttribute('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 // Set up message listener
window.addEventListener("message", (evnt) => { window.addEventListener("message", (evnt) => {
console.log("Received message in iframe", evnt); console.log("Received message in iframe", evnt);
@ -167,11 +180,7 @@ window.addEventListener("message", (evnt) => {
console.log("Received data from parent", window.data); console.log("Received data from parent", window.data);
// dynamically fix elements on this page // dynamically fix elements on this page
try { try {
embed_js(); // This might change the DOM, so do this first fix_document();
embed_css();
fix_links();
fix_forms();
embed_img();
// Trigger DOMContentLoaded again, some scripts that have just // Trigger DOMContentLoaded again, some scripts that have just
// been executed expect it. // been executed expect it.
window.document.dispatchEvent(new Event("DOMContentLoaded", { window.document.dispatchEvent(new Event("DOMContentLoaded", {
@ -188,7 +197,23 @@ window.addEventListener("message", (evnt) => {
}, false); }, 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({ window.parent.postMessage({
action: "set_title", action: "set_title",
argument: window.document.querySelector('head>title').innerText argument: window.document.querySelector('head>title').innerText

View File

@ -17,14 +17,13 @@ var myGet = function (arg) {
const originalResult = originalGet.apply(this, [arg]); const originalResult = originalGet.apply(this, [arg]);
// If searchtools.js of sphinx is used // If searchtools.js of sphinx is used
if ( if (
(arg == "q" || arg == "highlight") &&
window.DOCUMENTATION_OPTIONS &&
window.Scorer &&
window.data.get_parameters && window.data.get_parameters &&
(! originalResult) (window.location.search === "") &&
(Array.from(this.entries()).length == 0)
) { ) {
const params = new URLSearchParams('?' + window.data.get_parameters); 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; return result;
} else { } else {
return originalResult; return originalResult;