From 7ce6634c51a38cf7d43c4a259cb59cfaa69e45a7 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 7 Dec 2020 17:11:32 +0100 Subject: [PATCH] Ensure that the `pdf.sandbox.js` scriptElement is also removed from the DOM (PR 12695 follow-up) I completely missed this previously, but we obviously should remove the scriptElement as well to *really* clean-up everything properly. Given that there's multiple existing usages of `loadScript` in the code-base, the safest/quickest solution seemed to be to have call-sites opt-in to remove the scriptElement using a new parameter. --- src/display/display_utils.js | 10 ++++++++-- web/genericcom.js | 5 ++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 302dd0244..0c3fd6158 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -530,14 +530,20 @@ function isValidFetchUrl(url, baseUrl) { /** * @param {string} src + * @param {boolean} [removeScriptElement] * @returns {Promise} */ -function loadScript(src) { +function loadScript(src, removeScriptElement = false) { return new Promise((resolve, reject) => { const script = document.createElement("script"); script.src = src; - script.onload = resolve; + script.onload = function (evt) { + if (removeScriptElement) { + script.remove(); + } + resolve(evt); + }; script.onerror = function () { reject(new Error(`Cannot load script at: ${script.src}`)); }; diff --git a/web/genericcom.js b/web/genericcom.js index 2b83f0dfe..0f914218d 100644 --- a/web/genericcom.js +++ b/web/genericcom.js @@ -41,7 +41,10 @@ class GenericPreferences extends BasePreferences { class GenericScripting { constructor() { - this._ready = loadScript(AppOptions.get("sandboxBundleSrc")).then(() => { + this._ready = loadScript( + AppOptions.get("sandboxBundleSrc"), + /* removeScriptElement = */ true + ).then(() => { return window.pdfjsSandbox.QuickJSSandbox(); }); }