71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
import { app, core } from "../../app.ts";
|
||
|
||
/**
|
||
* 浏览器窗口边界信息
|
||
*/
|
||
export interface Bounds {
|
||
/**
|
||
* 窗口距离屏幕左边缘的偏移量(像素)
|
||
*/
|
||
left?: number;
|
||
/**
|
||
* 窗口距离屏幕上边缘的偏移量(像素)
|
||
*/
|
||
top?: number;
|
||
/**
|
||
* 窗口宽度(像素)
|
||
*/
|
||
width?: number;
|
||
/**
|
||
* 窗口高度(像素)
|
||
*/
|
||
height?: number;
|
||
/**
|
||
* 窗口状态,默认为 normal
|
||
*/
|
||
windowState?: "normal" | "minimized" | "maximized" | "fullscreen";
|
||
}
|
||
const desc = `窗口管理,参数为窗口位置和大小信息
|
||
参数示例:
|
||
left: 窗口左上角横坐标
|
||
top: 窗口左上角纵坐标
|
||
width: 窗口宽度
|
||
height: 窗口高度
|
||
windowState: 窗口状态,可选值有 normal(正常), minimized(最小化), maximized(最大化), fullscreen(全屏)
|
||
`;
|
||
|
||
app.route({
|
||
path: 'window',
|
||
key: 'manager',
|
||
description: desc,
|
||
middleware: ['auth'],
|
||
metadata: {
|
||
tags: ['浏览器操作', '窗口管理'],
|
||
}
|
||
}).define(async (ctx) => {
|
||
const bounds = ctx.query as Bounds || {};
|
||
const { left, top, width, height, windowState } = bounds;
|
||
if (!left && !top && !width && !height && !windowState) {
|
||
bounds.windowState = 'normal'
|
||
}
|
||
const page = await core.getPage();
|
||
const cdp = await page.context().newCDPSession(page);
|
||
try {
|
||
const { windowId } = await cdp.send('Browser.getWindowForTarget');
|
||
const page2 = { left: -1920, top: 0 }
|
||
const full = { windowState: 'fullscreen' }
|
||
// 设定传入的窗口参数
|
||
await cdp.send('Browser.setWindowBounds', {
|
||
windowId,
|
||
bounds: bounds
|
||
});
|
||
ctx.body = { success: true, message: '窗口已调整' };
|
||
} catch (error) {
|
||
ctx.throw!(500, '调整窗口失败: ' + (error as Error).message);
|
||
} finally {
|
||
// 关闭session
|
||
await cdp.detach();
|
||
}
|
||
}).addTo(app);
|
||
|