From 6999490a605fb356e09ebed44f4f13c43d1f4397 Mon Sep 17 00:00:00 2001 From: Michael Campagnaro Date: Wed, 10 Mar 2021 17:27:34 -0500 Subject: [PATCH] Handle null document body --- AutoplayStopper/content/content.js | 32 ++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/AutoplayStopper/content/content.js b/AutoplayStopper/content/content.js index 2a32cbc..fbf91a2 100644 --- a/AutoplayStopper/content/content.js +++ b/AutoplayStopper/content/content.js @@ -95,15 +95,19 @@ const ContentScript = new function() { } } }); - - if (window.document.body) + + if (window.document.body) observer.observe((href.search("^https?:") == -1) ? window.document : window.document.body , { subtree: true, childList: true}); - else + else if (window.document.documentElement) { docObserver.observe(window.document.documentElement, { childList: true}); - - // workaround for chrome no events on blank iframe ... - setTimeout( function() { + } + else { + return; + } + + // workaround for chrome no events on blank iframe ... + setTimeout( function() { window.addEventListener("mousedown", onContextMenu, true); ["mousedown","click"].forEach((a) => window.addEventListener(a, function(e) { if (e.isTrusted && e.button == 0) { @@ -146,10 +150,14 @@ const ContentScript = new function() { function handleDocument() { - var nodes = handleNodesDeep(window.document.body || window.document.documentElement); - var frames = (window.document.body || window.document.documentElement).querySelectorAll(frameSelector); - if (frames.length) handleFrames(frames); - return nodes; + var body = (window.document.body || window.document.documentElement); + if (body) { + var nodes = handleNodesDeep(body); + var frames = body.querySelectorAll(frameSelector); + if (frames.length) handleFrames(frames); + return nodes; + } + return null; }; function handleNodes(nodes, delayed) @@ -185,9 +193,11 @@ const ContentScript = new function() { }; }); }; - + function handleNodesDeep(node, match) { + if (!node) return null; + var nodes = match && (node.matches(selector) || node.shadowRoot) ? [node] : []; var count = nodes.push(...node.querySelectorAll(selector)); for (var el of nodes.filter((a) => a.shadowRoot)) {