* 在目标页面中执行 JS 函数 * * 函数会被序列化为字符串,在目标页面上下文中执行。 * 支持异步函数(返回 Promise),结果会自动 unwrap。 * * @param {Function} fn - 要执行的函数 * @param {...*} args - 传递给函数的参数(必须是可 JSON 序列化的) * @returns {ZBrowserClient} this * * 示例: * .evaluate(() => document.title) * .evaluate((selector) =>
(fn, ...args)
| 100 | * .evaluate((selector) => document.querySelector(selector)?.textContent, '.title') |
| 101 | */ |
| 102 | evaluate(fn, ...args) { |
| 103 | if (typeof fn !== 'function') { |
| 104 | throw new Error('evaluate: first argument should be a function') |
| 105 | } |
| 106 | this._queue.push({ |
| 107 | method: 'javascript', |
| 108 | args: [jsCodeTemplate(fn, args)] |
| 109 | }) |
| 110 | return this |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * 等待操作 |