Skip to content

Commit

Permalink
possible fix for #26 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-girko committed Nov 15, 2022
1 parent d1468a7 commit 84142b9
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions v2/data/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ script.textContent = `{
e.stopImmediatePropagation();
};
const once = {
focus: true,
visibilitychange: true,
webkitvisibilitychange: true
};
/* visibility */
Object.defineProperty(document, 'visibilityState', {
get() {
Expand All @@ -32,10 +38,24 @@ script.textContent = `{
document.addEventListener('visibilitychange', e => {
script.dispatchEvent(new Event('state'));
if (script.dataset.visibility !== 'false') {
if (once.visibilitychange) {
once.visibilitychange = false;
return;
}
return block(e);
}
}, true);
document.addEventListener('webkitvisibilitychange', e => script.dataset.visibility !== 'false' && block(e), true);
document.addEventListener('webkitvisibilitychange', e => {
if (script.dataset.visibility !== 'false') {
if (once.webkitvisibilitychange) {
once.webkitvisibilitychange = false;
return;
}
return block(e);
}
}, true);
window.addEventListener('pagehide', e => script.dataset.visibility !== 'false' && block(e), true);
/* hidden */
Expand All @@ -51,8 +71,7 @@ script.textContent = `{
});
/* focus */
document.addEventListener('hasFocus', e => script.dataset.focus !== 'false' && block(e), true);
document.__proto__.hasFocus = new Proxy(document.__proto__.hasFocus, {
Document.prototype.hasFocus = new Proxy(Document.prototype.hasFocus, {
apply(target, self, args) {
if (script.dataset.focus !== 'false') {
return true;
Expand All @@ -61,6 +80,22 @@ script.textContent = `{
}
});
const onfocus = e => {
console.log(1);
if (script.dataset.focus !== 'false') {
if (e.target === document || e.target === window) {
if (once.focus && document.readyState === 'complete' && e.target === window) {
once.focus = false;
return;
}
return block(e);
}
}
};
// document.addEventListener('focus', onfocus, true);
window.addEventListener('focus', onfocus, true);
/* blur */
const onblur = e => {
if (script.dataset.blur !== 'false') {
Expand Down Expand Up @@ -107,7 +142,6 @@ script.textContent = `{
return Reflect.apply(target, self, args);
}
});
}`;
document.documentElement.appendChild(script);
script.remove();
Expand Down

0 comments on commit 84142b9

Please sign in to comment.