(selector: string, options?: WaitForOptions)
| 215 | |
| 216 | // 等待元素出现 |
| 217 | async waitFor(selector: string, options?: WaitForOptions): Promise<WaitForResult> { |
| 218 | const tabId = await this.resolveTabId(options?.tabId); |
| 219 | const timeout = options?.timeout ?? 10000; |
| 220 | const interval = 500; |
| 221 | const startTime = Date.now(); |
| 222 | |
| 223 | while (Date.now() - startTime < timeout) { |
| 224 | const results = await chrome.scripting.executeScript({ |
| 225 | target: { tabId }, |
| 226 | func: checkElement, |
| 227 | args: [selector], |
| 228 | world: "ISOLATED", |
| 229 | }); |
| 230 | |
| 231 | if (results?.[0]?.result) { |
| 232 | return results[0].result as WaitForResult; |
| 233 | } |
| 234 | |
| 235 | await new Promise((resolve) => setTimeout(resolve, interval)); |
| 236 | } |
| 237 | |
| 238 | return { found: false }; |
| 239 | } |
| 240 | |
| 241 | // 在页面中执行 JavaScript 代码(动态代码必须跑 MAIN world,ISOLATED 会被扩展 CSP 拦截 new Function) |
| 242 | async executeScript(code: string, options?: ExecuteScriptOptions): Promise<{ result: unknown; tabId: number }> { |
no test coverage detected