Handle null document body

This commit is contained in:
Michael Campagnaro 2021-03-10 17:27:34 -05:00
parent 69eb447c28
commit 6999490a60

View File

@ -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 observer.observe((href.search("^https?:") == -1) ? window.document : window.document.body
, { subtree: true, childList: true}); , { subtree: true, childList: true});
else else if (window.document.documentElement) {
docObserver.observe(window.document.documentElement, { childList: true}); docObserver.observe(window.document.documentElement, { childList: true});
}
// workaround for chrome no events on blank iframe ... else {
setTimeout( function() { return;
}
// workaround for chrome no events on blank iframe ...
setTimeout( function() {
window.addEventListener("mousedown", onContextMenu, true); window.addEventListener("mousedown", onContextMenu, true);
["mousedown","click"].forEach((a) => window.addEventListener(a, function(e) { ["mousedown","click"].forEach((a) => window.addEventListener(a, function(e) {
if (e.isTrusted && e.button == 0) { if (e.isTrusted && e.button == 0) {
@ -146,10 +150,14 @@ const ContentScript = new function() {
function handleDocument() function handleDocument()
{ {
var nodes = handleNodesDeep(window.document.body || window.document.documentElement); var body = (window.document.body || window.document.documentElement);
var frames = (window.document.body || window.document.documentElement).querySelectorAll(frameSelector); if (body) {
if (frames.length) handleFrames(frames); var nodes = handleNodesDeep(body);
return nodes; var frames = body.querySelectorAll(frameSelector);
if (frames.length) handleFrames(frames);
return nodes;
}
return null;
}; };
function handleNodes(nodes, delayed) function handleNodes(nodes, delayed)
@ -185,9 +193,11 @@ const ContentScript = new function() {
}; };
}); });
}; };
function handleNodesDeep(node, match) function handleNodesDeep(node, match)
{ {
if (!node) return null;
var nodes = match && (node.matches(selector) || node.shadowRoot) ? [node] : []; var nodes = match && (node.matches(selector) || node.shadowRoot) ? [node] : [];
var count = nodes.push(...node.querySelectorAll(selector)); var count = nodes.push(...node.querySelectorAll(selector));
for (var el of nodes.filter((a) => a.shadowRoot)) { for (var el of nodes.filter((a) => a.shadowRoot)) {