28 lines
945 B
TypeScript
28 lines
945 B
TypeScript
import uuidv4 from "../../func/utils/uuidv4";
|
||
import WuhuBase from "../WuhuBase";
|
||
|
||
export default class WindowActiveState extends WuhuBase {
|
||
className = 'WindowActiveState';
|
||
isFocus = false;
|
||
uuid = uuidv4();
|
||
|
||
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')
|
||
}
|
||
} |