47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import uuidv4 from "../../func/utils/uuidv4";
|
||
import WuhuBase from "../WuhuBase";
|
||
|
||
export default class WindowActiveState extends WuhuBase {
|
||
isFocus = false;
|
||
uuid = uuidv4();
|
||
|
||
private constructor() {
|
||
super();
|
||
if (self !== top) return null;
|
||
localStorage.setItem('whuuid', this.uuid);
|
||
document.addEventListener('visibilitychange',
|
||
() => (document.visibilityState !== 'hidden') && (localStorage.setItem('whuuid', this.uuid))
|
||
);
|
||
addEventListener('focus', () => this.isFocus = true);
|
||
addEventListener('blur', () => this.isFocus = false);
|
||
}
|
||
|
||
get(): boolean {
|
||
// 当前窗口获得了焦点 优先级最高
|
||
if (this.isFocus) return true;
|
||
// 可视性
|
||
if (!document.hidden) return true;
|
||
// 全部在后台,使用唯一id判断
|
||
return this.uuid === localStorage.getItem('whuuid')
|
||
}
|
||
}
|
||
|
||
// export default function WindowActiveState() {
|
||
// if (self !== top) return null;
|
||
// const uuid = uuidv4();
|
||
// let isFocus = false;
|
||
// localStorage.setItem('whuuid', uuid);
|
||
// document.addEventListener('visibilitychange', () =>
|
||
// (document.visibilityState !== 'hidden') && (localStorage.setItem('whuuid', uuid))
|
||
// );
|
||
// addEventListener('focus', () => isFocus = true)
|
||
// addEventListener('blur', () => isFocus = false)
|
||
// return function (): boolean {
|
||
// // 当前窗口获得了焦点 优先级最高
|
||
// if (isFocus) return true;
|
||
// // 可视性
|
||
// if (!document.hidden) return true;
|
||
// // 全部在后台,使用唯一id判断
|
||
// return uuid === localStorage.getItem('whuuid')
|
||
// };
|
||
// }
|