Skip to content

Commit 0fddfb0

Browse files
committed
Improve web ensureCanvasOnTop logic
1 parent a4ca5be commit 0fddfb0

1 file changed

Lines changed: 38 additions & 7 deletions

File tree

webdriver/resources/web-extension/js/testar.canvas.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ function addCanvasTestar() {
1414
// Create canvas, get reference to context
1515
testar_canvas = document.createElement('canvas');
1616
testar_canvas.id = 'testar_canvas';
17-
document.body.appendChild(testar_canvas);
17+
testar_canvas.style.position = 'fixed';
18+
testar_canvas.style.margin = '0px';
19+
testar_canvas.style.padding = '0px';
20+
testar_canvas.style.border = '0px';
21+
testar_canvas.style.pointerEvents = 'none';
22+
testar_canvas.style.zIndex = '2147483647';
1823
testarCtx = testar_canvas.getContext('2d');
1924

2025
// Set canvas to complete viewport
@@ -23,6 +28,12 @@ function addCanvasTestar() {
2328
// Make sure canvas keeps size of viewport on resize or scroll
2429
window.addEventListener('resize', resizeCanvasTestar, true);
2530
window.addEventListener('scroll', resizeCanvasTestar, true);
31+
new MutationObserver(ensureCanvasOnTop).observe(document.documentElement, {
32+
childList: true,
33+
subtree: true,
34+
attributes: true,
35+
attributeFilter: ['open', 'popover']
36+
});
2637

2738
ensureCanvasOnTop();
2839
return typeof testar_canvas;
@@ -33,12 +44,32 @@ function addCanvasTestar() {
3344
* will try to get their element the highest z-index
3445
*/
3546
function ensureCanvasOnTop() {
36-
var lengths = Array.from(document.querySelectorAll('body *'))
37-
.map(a => parseFloat(window.getComputedStyle(a).zIndex))
38-
.filter(a => !isNaN(a));
39-
var maxIndex = Math.max.apply(null, lengths);
40-
if (testar_canvas.style.zIndex < maxIndex) {
41-
testar_canvas.style.zIndex = maxIndex + 1;
47+
if (typeof testar_canvas !== 'object') {
48+
return;
49+
}
50+
51+
var canvasHost = document.body;
52+
53+
try {
54+
var popoverHost = document.querySelector(':popover-open');
55+
if (popoverHost) {
56+
canvasHost = popoverHost;
57+
}
58+
} catch (error) {
59+
}
60+
61+
if (canvasHost === document.body) {
62+
try {
63+
var modalHost = document.querySelector(':modal');
64+
if (modalHost) {
65+
canvasHost = modalHost;
66+
}
67+
} catch (error) {
68+
}
69+
}
70+
71+
if (testar_canvas.parentNode !== canvasHost || canvasHost.lastElementChild !== testar_canvas) {
72+
canvasHost.appendChild(testar_canvas);
4273
}
4374
}
4475

0 commit comments

Comments
 (0)