mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 09:38:39 +00:00
refactor(cli): share browser resize output helper
This commit is contained in:
@@ -1,11 +1,8 @@
|
|||||||
import type { Command } from "commander";
|
import type { Command } from "commander";
|
||||||
import { danger } from "../../globals.js";
|
import { danger } from "../../globals.js";
|
||||||
import { defaultRuntime } from "../../runtime.js";
|
import { defaultRuntime } from "../../runtime.js";
|
||||||
import {
|
import { runBrowserResizeWithOutput } from "../browser-cli-resize.js";
|
||||||
callBrowserRequest,
|
import { callBrowserRequest, type BrowserParentOpts } from "../browser-cli-shared.js";
|
||||||
callBrowserResize,
|
|
||||||
type BrowserParentOpts,
|
|
||||||
} from "../browser-cli-shared.js";
|
|
||||||
import { requireRef, resolveBrowserActionContext } from "./shared.js";
|
import { requireRef, resolveBrowserActionContext } from "./shared.js";
|
||||||
|
|
||||||
export function registerBrowserNavigationCommands(
|
export function registerBrowserNavigationCommands(
|
||||||
@@ -52,27 +49,16 @@ export function registerBrowserNavigationCommands(
|
|||||||
.option("--target-id <id>", "CDP target id (or unique prefix)")
|
.option("--target-id <id>", "CDP target id (or unique prefix)")
|
||||||
.action(async (width: number, height: number, opts, cmd) => {
|
.action(async (width: number, height: number, opts, cmd) => {
|
||||||
const { parent, profile } = resolveBrowserActionContext(cmd, parentOpts);
|
const { parent, profile } = resolveBrowserActionContext(cmd, parentOpts);
|
||||||
if (!Number.isFinite(width) || !Number.isFinite(height)) {
|
|
||||||
defaultRuntime.error(danger("width and height must be numbers"));
|
|
||||||
defaultRuntime.exit(1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
const result = await callBrowserResize(
|
await runBrowserResizeWithOutput({
|
||||||
parent,
|
parent,
|
||||||
{
|
profile,
|
||||||
profile,
|
width,
|
||||||
width,
|
height,
|
||||||
height,
|
targetId: opts.targetId,
|
||||||
targetId: opts.targetId,
|
timeoutMs: 20000,
|
||||||
},
|
successMessage: `resized to ${width}x${height}`,
|
||||||
{ timeoutMs: 20000 },
|
});
|
||||||
);
|
|
||||||
if (parent?.json) {
|
|
||||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
defaultRuntime.log(`resized to ${width}x${height}`);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
defaultRuntime.error(danger(String(err)));
|
defaultRuntime.error(danger(String(err)));
|
||||||
defaultRuntime.exit(1);
|
defaultRuntime.exit(1);
|
||||||
|
|||||||
37
src/cli/browser-cli-resize.ts
Normal file
37
src/cli/browser-cli-resize.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { danger } from "../globals.js";
|
||||||
|
import { defaultRuntime } from "../runtime.js";
|
||||||
|
import { callBrowserResize, type BrowserParentOpts } from "./browser-cli-shared.js";
|
||||||
|
|
||||||
|
export async function runBrowserResizeWithOutput(params: {
|
||||||
|
parent: BrowserParentOpts;
|
||||||
|
profile?: string;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
targetId?: string;
|
||||||
|
timeoutMs?: number;
|
||||||
|
successMessage: string;
|
||||||
|
}): Promise<void> {
|
||||||
|
const { width, height } = params;
|
||||||
|
if (!Number.isFinite(width) || !Number.isFinite(height)) {
|
||||||
|
defaultRuntime.error(danger("width and height must be numbers"));
|
||||||
|
defaultRuntime.exit(1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await callBrowserResize(
|
||||||
|
params.parent,
|
||||||
|
{
|
||||||
|
profile: params.profile,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
targetId: params.targetId,
|
||||||
|
},
|
||||||
|
{ timeoutMs: params.timeoutMs ?? 20000 },
|
||||||
|
);
|
||||||
|
|
||||||
|
if (params.parent?.json) {
|
||||||
|
defaultRuntime.log(JSON.stringify(result, null, 2));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
defaultRuntime.log(params.successMessage);
|
||||||
|
}
|
||||||
@@ -2,11 +2,8 @@ import type { Command } from "commander";
|
|||||||
import { danger } from "../globals.js";
|
import { danger } from "../globals.js";
|
||||||
import { defaultRuntime } from "../runtime.js";
|
import { defaultRuntime } from "../runtime.js";
|
||||||
import { parseBooleanValue } from "../utils/boolean.js";
|
import { parseBooleanValue } from "../utils/boolean.js";
|
||||||
import {
|
import { runBrowserResizeWithOutput } from "./browser-cli-resize.js";
|
||||||
callBrowserRequest,
|
import { callBrowserRequest, type BrowserParentOpts } from "./browser-cli-shared.js";
|
||||||
callBrowserResize,
|
|
||||||
type BrowserParentOpts,
|
|
||||||
} from "./browser-cli-shared.js";
|
|
||||||
import { registerBrowserCookiesAndStorageCommands } from "./browser-cli-state.cookies-storage.js";
|
import { registerBrowserCookiesAndStorageCommands } from "./browser-cli-state.cookies-storage.js";
|
||||||
import { runCommandWithRuntime } from "./cli-utils.js";
|
import { runCommandWithRuntime } from "./cli-utils.js";
|
||||||
|
|
||||||
@@ -39,27 +36,16 @@ export function registerBrowserStateCommands(
|
|||||||
.action(async (width: number, height: number, opts, cmd) => {
|
.action(async (width: number, height: number, opts, cmd) => {
|
||||||
const parent = parentOpts(cmd);
|
const parent = parentOpts(cmd);
|
||||||
const profile = parent?.browserProfile;
|
const profile = parent?.browserProfile;
|
||||||
if (!Number.isFinite(width) || !Number.isFinite(height)) {
|
|
||||||
defaultRuntime.error(danger("width and height must be numbers"));
|
|
||||||
defaultRuntime.exit(1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await runBrowserCommand(async () => {
|
await runBrowserCommand(async () => {
|
||||||
const result = await callBrowserResize(
|
await runBrowserResizeWithOutput({
|
||||||
parent,
|
parent,
|
||||||
{
|
profile,
|
||||||
profile,
|
width,
|
||||||
width,
|
height,
|
||||||
height,
|
targetId: opts.targetId,
|
||||||
targetId: opts.targetId,
|
timeoutMs: 20000,
|
||||||
},
|
successMessage: `viewport set: ${width}x${height}`,
|
||||||
{ timeoutMs: 20000 },
|
});
|
||||||
);
|
|
||||||
if (parent?.json) {
|
|
||||||
defaultRuntime.log(JSON.stringify(result, null, 2));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
defaultRuntime.log(`viewport set: ${width}x${height}`);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user