use iFrameId const

This commit is contained in:
Adrian Vollmer 2022-10-03 15:05:03 +02:00
parent b78d3728eb
commit 9bb1e6f3ae
3 changed files with 15 additions and 12 deletions

View File

@ -1,3 +1,5 @@
const iFrameId = 'main';
var _ArrayBufferToBase64 = function (array_buffer) { var _ArrayBufferToBase64 = function (array_buffer) {
var binary = ''; var binary = '';
var bytes = new Uint8Array(array_buffer); var bytes = new Uint8Array(array_buffer);
@ -21,13 +23,13 @@ var _base64ToArrayBuffer = function (base64) {
}; };
var createIframe = function() { var createIframe = function() {
var iframe = document.getElementById('main'); var iframe = document.getElementById(iFrameId);
if (iframe) { iframe.remove() }; if (iframe) { iframe.remove() };
iframe = document.createElement("iframe"); iframe = document.createElement("iframe");
window.document.body.prepend(iframe); window.document.body.prepend(iframe);
iframe.setAttribute('src', '#'); iframe.setAttribute('src', '#');
iframe.setAttribute('name', 'main'); iframe.setAttribute('name', iFrameId);
iframe.setAttribute('id', 'main'); iframe.setAttribute('id', iFrameId);
iframe.style.display = 'none'; iframe.style.display = 'none';
return iframe; return iframe;
} }
@ -60,14 +62,14 @@ window.onload = function() {
// iframe has finished loading and sent us its title // iframe has finished loading and sent us its title
// parent sets the title and responds with the data object // parent sets the title and responds with the data object
window.document.title = evnt.data.argument; window.document.title = evnt.data.argument;
var iframe = document.getElementById('main'); var iframe = document.getElementById(iFrameId);
iframe.contentWindow.postMessage({ iframe.contentWindow.postMessage({
action: "set_data", action: "set_data",
argument: window.data, argument: window.data,
}, "*"); }, "*");
} else if (evnt.data.action == 'virtual_click') { } else if (evnt.data.action == 'virtual_click') {
// user has clicked on a link in the iframe // user has clicked on a link in the iframe
var iframe = document.getElementById('main'); var iframe = document.getElementById(iFrameId);
iframe.remove() iframe.remove()
var loading = document.getElementById('loading-indicator'); var loading = document.getElementById('loading-indicator');
loading.style.display = ''; loading.style.display = '';
@ -79,7 +81,7 @@ window.onload = function() {
} else if (evnt.data.action == 'show_iframe') { } else if (evnt.data.action == 'show_iframe') {
// iframe finished fixing the document and is ready to be shown; // iframe finished fixing the document and is ready to be shown;
// hide loading indicator // hide loading indicator
var iframe = document.getElementById('main'); var iframe = document.getElementById(iFrameId);
iframe.style.display = ''; iframe.style.display = '';
var loading = document.getElementById('loading-indicator'); var loading = document.getElementById('loading-indicator');
loading.style.display = 'none'; loading.style.display = 'none';

View File

@ -197,6 +197,7 @@ window.addEventListener("message", (evnt) => {
cancelable: true cancelable: true
})); }));
} finally { } finally {
observer.observe(window.document.body, {subtree: true, childList: true});
window.parent.postMessage({ window.parent.postMessage({
action: "show_iframe", action: "show_iframe",
argument: "", argument: "",
@ -222,7 +223,6 @@ const observer = new MutationObserver((mutationList) => {
} }
}); });
}); });
observer.observe(window.document.body, {subtree: true, childList: true});
// Set parent window title and trigger data transmission // Set parent window title and trigger data transmission

View File

@ -149,16 +149,17 @@ def embed_html_resources(html, base_dir, before, after):
import bs4 import bs4
soup = bs4.BeautifulSoup(html, 'lxml') soup = bs4.BeautifulSoup(html, 'lxml')
body = soup.find('body')
if before: if body and before:
script = soup.new_tag("script") script = soup.new_tag("script")
script.string = before script.string = before
soup.find('body').insert(0, script) body.insert(0, script)
if after: if body and after:
script = soup.new_tag("script") script = soup.new_tag("script")
script.string = after script.string = after
soup.find('body').append(script) body.append(script)
# TODO embed remote resources in case we want the entire file to be # TODO embed remote resources in case we want the entire file to be
# usable in an offline environment # usable in an offline environment