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

View File

@ -197,6 +197,7 @@ window.addEventListener("message", (evnt) => {
cancelable: true
}));
} finally {
observer.observe(window.document.body, {subtree: true, childList: true});
window.parent.postMessage({
action: "show_iframe",
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

View File

@ -149,19 +149,20 @@ def embed_html_resources(html, base_dir, before, after):
import bs4
soup = bs4.BeautifulSoup(html, 'lxml')
body = soup.find('body')
if before:
if body and before:
script = soup.new_tag("script")
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.string = after
soup.find('body').append(script)
body.append(script)
# TODO embed remote resources in case we want the entire file to be
# usable in an offline environment
# usable in an offline environment
return str(soup)