From c4ea9321dcffbda9004461a7b0027cf5c893f3c0 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 2 May 2020 13:00:46 -0400 Subject: [PATCH] Validate document nodes before use (#493) * Validate document.body before use in loadScripts This also fixes an issue where reject wasn't being passed to loadScriptSentinel. * Validate document nodes before use in _getSiteColor * Validate document.body before use in _getViewport * Validate document.body before use in setContentScale * Validate document.body before use in docImposterCreate --- ext/fg/js/document.js | 5 ++++- ext/fg/js/float.js | 4 +++- ext/fg/js/popup.js | 12 +++++++++--- ext/mixed/js/dynamic-loader.js | 12 ++++++++---- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 3b4cc28f..6103c7c5 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -28,6 +28,9 @@ function docSetImposterStyle(style, propertyName, value) { } function docImposterCreate(element, isTextarea) { + const body = document.body; + if (body === null) { return [null, null]; } + const elementStyle = window.getComputedStyle(element); const elementRect = element.getBoundingClientRect(); const documentRect = document.documentElement.getBoundingClientRect(); @@ -78,7 +81,7 @@ function docImposterCreate(element, isTextarea) { } container.appendChild(imposter); - document.body.appendChild(container); + body.appendChild(container); // Adjust size const imposterRect = imposter.getBoundingClientRect(); diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 294093cd..77e8edd8 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -162,7 +162,9 @@ class DisplayFloat extends Display { } setContentScale(scale) { - document.body.style.fontSize = `${scale}em`; + const body = document.body; + if (body === null) { return; } + body.style.fontSize = `${scale}em`; } async getDocumentTitle() { diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 2b33b714..f5cb6f77 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -389,8 +389,13 @@ class Popup { _getSiteColor() { const color = [255, 255, 255]; - Popup._addColor(color, Popup._getColorInfo(window.getComputedStyle(document.documentElement).backgroundColor)); - Popup._addColor(color, Popup._getColorInfo(window.getComputedStyle(document.body).backgroundColor)); + const {documentElement, body} = document; + if (documentElement !== null) { + Popup._addColor(color, Popup._getColorInfo(window.getComputedStyle(documentElement).backgroundColor)); + } + if (body !== null) { + Popup._addColor(color, Popup._getColorInfo(window.getComputedStyle(body).backgroundColor)); + } const dark = (color[0] < 128 && color[1] < 128 && color[2] < 128); return dark ? 'dark' : 'light'; } @@ -575,10 +580,11 @@ class Popup { } } + const body = document.body; return { left: 0, top: 0, - right: document.body.clientWidth, + right: (body !== null ? body.clientWidth : 0), bottom: window.innerHeight }; } diff --git a/ext/mixed/js/dynamic-loader.js b/ext/mixed/js/dynamic-loader.js index 29672d36..51b6821b 100644 --- a/ext/mixed/js/dynamic-loader.js +++ b/ext/mixed/js/dynamic-loader.js @@ -31,8 +31,13 @@ const dynamicLoader = (() => { } function loadScripts(urls) { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const parent = document.body; + if (parent === null) { + reject(new Error('Missing body')); + return; + } + for (const url of urls) { const node = parent.querySelector(`script[src='${escapeCSSAttribute(url)}']`); if (node !== null) { continue; } @@ -43,12 +48,11 @@ const dynamicLoader = (() => { parent.appendChild(script); } - loadScriptSentinel(resolve); + loadScriptSentinel(parent, resolve, reject); }); } - function loadScriptSentinel(resolve, reject) { - const parent = document.body; + function loadScriptSentinel(parent, resolve, reject) { const script = document.createElement('script'); const sentinelEventName = 'dynamicLoaderSentinel';