Skip to content

Commit

Permalink
allow the first dispatch of 'focus', 'visibilitychange' and 'webkitvi…
Browse files Browse the repository at this point in the history
…sibilitychange' events
  • Loading branch information
brian-girko committed Nov 8, 2022
1 parent c8ca668 commit a5476f6
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/v3/data/inject/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,28 @@
}
});

const once = {
focus: true,
visibilitychange: true,
webkitvisibilitychange: true
};

document.addEventListener('visibilitychange', e => {
port.dispatchEvent(new Event('state'));
if (port.dataset.enabled === 'true' && port.dataset.visibility !== 'false') {
if (once.visibilitychange) {
once.visibilitychange = false;
return;
}
return block(e);
}
}, true);
document.addEventListener('webkitvisibilitychange', e => {
if (port.dataset.enabled === 'true' && port.dataset.visibility !== 'false') {
if (once.webkitvisibilitychange) {
once.webkitvisibilitychange = false;
return;
}
return block(e);
}
}, true);
Expand Down Expand Up @@ -74,15 +88,14 @@
const onfocus = e => {
if (port.dataset.enabled === 'true' && port.dataset.focus !== 'false') {
if (e.target === document || e.target === window) {
if (onfocus.once) {
onfocus.once = false;
if (once.focus) {
once.focus = false;
return;
}
return block(e);
}
}
};
onfocus.once = true;
document.addEventListener('focus', onfocus, true);
window.addEventListener('focus', onfocus, true);

Expand Down

0 comments on commit a5476f6

Please sign in to comment.