From be7630648d5dd9668199e06f21bdc8213abd5638 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 13 Apr 2021 13:32:50 +0200 Subject: [PATCH] Add setInnerHTML as a Replacement for $.html --- qt/aqt/data/web/js/reviewer.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/qt/aqt/data/web/js/reviewer.ts b/qt/aqt/data/web/js/reviewer.ts index e41144780..fe9935e87 100644 --- a/qt/aqt/data/web/js/reviewer.ts +++ b/qt/aqt/data/web/js/reviewer.ts @@ -26,8 +26,34 @@ function _queueAction(action: Callback): void { _updatingQueue = _updatingQueue.then(action); } +function setInnerHTML(element: Element, html: string): void { + for (const oldVideo of element.getElementsByTagName("video")) { + oldVideo.pause(); + + while (oldVideo.firstChild) { + oldVideo.removeChild(oldVideo.firstChild); + } + + oldVideo.load(); + } + + element.innerHTML = html; + + for (const oldScript of element.getElementsByTagName("script")) { + const newScript = document.createElement("script"); + + for (const attribute of oldScript.attributes) { + newScript.setAttribute(attribute.name, attribute.value); + } + + newScript.appendChild(document.createTextNode(oldScript.innerHTML)); + oldScript.parentNode.replaceChild(newScript, oldScript); + } +} + async function _updateQA( html: string, + _unusused: unknown, onupdate: Callback, onshown: Callback ): Promise { @@ -49,7 +75,7 @@ async function _updateQA( // update card try { - qa.innerHTML = html; + setInnerHTML(qa, html); } catch (error) { renderError("HTML")(error); } @@ -78,6 +104,7 @@ function _showQuestion(q: string, bodyclass: string): void { _queueAction(() => _updateQA( q, + null, function () { // return to top of window window.scrollTo(0, 0); @@ -99,6 +126,7 @@ function _showAnswer(a: string, bodyclass: string): void { _queueAction(() => _updateQA( a, + null, function () { if (bodyclass) { // when previewing