* 创建 zbrowser 客户端实例 * * 加载 client.js 并注入 run() 方法(client.js 本身不包含 run)。 * run() 将操作队列通过 ipcInvoke 发送到主进程的 ZBrowserExecutor。 * * @returns {object} ZBrowserClient 实例(带 run 方法)
()
| 256 | * @returns {object} ZBrowserClient 实例(带 run 方法) |
| 257 | */ |
| 258 | function createZBrowserClient() { |
| 259 | const { ZBrowserClient } = require('./zbrowser/client') |
| 260 | const client = new ZBrowserClient() |
| 261 | client.run = async function (ubrowserIdOrOptions, options) { |
| 262 | if (this._queue.length === 0) throw new Error('no actions run') |
| 263 | // 复制队列并清空(允许客户端实例复用) |
| 264 | const queue = [...this._queue] |
| 265 | this._queue = [] |
| 266 | |
| 267 | // 解析参数:run() / run(options) / run(ubrowserId) / run(ubrowserId, options) |
| 268 | let ubrowserId |
| 269 | let runOptions = {} |
| 270 | if (typeof ubrowserIdOrOptions === 'number') { |
| 271 | ubrowserId = ubrowserIdOrOptions |
| 272 | if (typeof options === 'object' && options !== null) runOptions = options |
| 273 | } else if (typeof ubrowserIdOrOptions === 'object' && ubrowserIdOrOptions !== null) { |
| 274 | runOptions = ubrowserIdOrOptions |
| 275 | } |
| 276 | // 如果未指定 ubrowserId 但实例上有保留的 _windowId,自动复用 |
| 277 | if (ubrowserId === undefined && this._windowId) { |
| 278 | ubrowserId = this._windowId |
| 279 | } |
| 280 | |
| 281 | const result = await ipcInvoke('runZBrowser', { ubrowserId, options: runOptions, queue }) |
| 282 | if (result.error) { |
| 283 | const err = new Error(result.message) |
| 284 | err.data = result.data |
| 285 | throw err |
| 286 | } |
| 287 | // 如果窗口保留(可见窗口加入空闲池),记录 windowId 以便后续 run() 复用 |
| 288 | if (result.windowId) { |
| 289 | this._windowId = result.windowId |
| 290 | } |
| 291 | // uTools 兼容:返回数组,最后一个元素是窗口信息对象 |
| 292 | const data = result.data || [] |
| 293 | if (result.windowInfo) { |
| 294 | data.push(result.windowInfo) |
| 295 | } |
| 296 | return data |
| 297 | } |
| 298 | return client |
| 299 | } |
| 300 | |
| 301 | window.ztools = { |
| 302 | getAppName: () => 'ZTools', |