| 1 | |
| 2 | |
| 3 | export class BrowserVisibility { |
| 4 | constructor(cdp, targets, canHide = async () => true) { |
| 5 | this.cdp = cdp; |
| 6 | this.targets = targets; |
| 7 | this.canHide = canHide; |
| 8 | this.visible = true; |
| 9 | this.saved = new Map(); |
| 10 | this.queue = Promise.resolve(); |
| 11 | } |
| 12 | |
| 13 | set(visible) { |
| 14 | if (typeof visible !== "boolean") throw new Error("Invalid browser visibility"); |
| 15 | this.visible = visible; |
| 16 | const result = this.queue.catch(() => {}).then(() => this.apply()); |
| 17 | this.queue = result; |
| 18 | return result; |
| 19 | } |
| 20 | |
| 21 | activate(targetId) { |
| 22 | const result = this.queue.catch(() => {}).then(async () => { |
| 23 | await this.cdp("Target.activateTarget", { targetId }); |
| 24 | if (this.visible || !await this.canHide()) { |
| 25 | const { windowId, bounds } = await this.cdp("Browser.getWindowForTarget", { targetId }); |
| 26 | if (bounds.windowState === "minimized") await this.setBounds(windowId, { windowState: "normal" }); |
| 27 | await this.setBounds(windowId, { windowState: "fullscreen" }); |
| 28 | this.saved.delete(windowId); |
| 29 | } else { |
| 30 | await this.apply(); |
| 31 | } |
| 32 | }); |
| 33 | this.queue = result; |
| 34 | return result; |
| 35 | } |
| 36 | |
| 37 | async setBounds(windowId, bounds) { |
| 38 | await this.cdp("Browser.setWindowBounds", { windowId, bounds }); |
| 39 | |
| 40 | |
| 41 | for (let attempt = 0; attempt < 40; attempt++) { |
| 42 | const current = await this.cdp("Browser.getWindowBounds", { windowId }); |
| 43 | if (current.bounds.windowState === bounds.windowState) return; |
| 44 | await new Promise(resolve => setTimeout(resolve, 25)); |
| 45 | } |
| 46 | throw new Error("Browser window did not change state"); |
| 47 | } |
| 48 | |
| 49 | async apply() { |
| 50 | const visible = this.visible || !await this.canHide(); |
| 51 | const windows = new Map(); |
| 52 | for (const targetId of this.targets()) { |
| 53 | try { |
| 54 | const window = await this.cdp("Browser.getWindowForTarget", { targetId }); |
| 55 | windows.set(window.windowId, window.bounds); |
| 56 | } catch (error) { |
| 57 | if (this.targets().includes(targetId)) throw error; |
| 58 | } |
| 59 | } |
| 60 | for (const windowId of this.saved.keys()) { |
| 61 | if (!windows.has(windowId)) this.saved.delete(windowId); |
| 62 | } |
| 63 | for (const [windowId, bounds] of windows) { |
| 64 | if (visible) { |
| 65 | const saved = this.saved.get(windowId); |
| 66 | if (!saved) continue; |
| 67 | await this.setBounds(windowId, { windowState: "normal" }); |
| 68 | const restore = saved.windowState === "normal" ? saved : { windowState: saved.windowState }; |
| 69 | await this.setBounds(windowId, restore); |
| 70 | this.saved.delete(windowId); |
| 71 | } else if (bounds.windowState !== "minimized") { |
| 72 | if (!this.saved.has(windowId)) this.saved.set(windowId, bounds); |
| 73 | if (bounds.windowState !== "normal") { |
| 74 | await this.setBounds(windowId, { windowState: "normal" }); |
| 75 | } |
| 76 | await this.setBounds(windowId, { windowState: "minimized" }); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |