From 9bb1e6f3aeab7ed0b9f4f4bb41e07a792d167cb1 Mon Sep 17 00:00:00 2001 From: Adrian Vollmer Date: Mon, 3 Oct 2022 15:05:03 +0200 Subject: [PATCH] use iFrameId const --- src/assets/init.js | 14 ++++++++------ src/assets/inject.js | 2 +- src/embed.py | 11 ++++++----- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/assets/init.js b/src/assets/init.js index 5c83680..af19e7a 100644 --- a/src/assets/init.js +++ b/src/assets/init.js @@ -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'; diff --git a/src/assets/inject.js b/src/assets/inject.js index a7ce1d6..7834c4d 100644 --- a/src/assets/inject.js +++ b/src/assets/inject.js @@ -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 diff --git a/src/embed.py b/src/embed.py index a4be10b..a5a8290 100644 --- a/src/embed.py +++ b/src/embed.py @@ -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)