// 公共截图后端:一个投影会话;失败熔断,禁止反复截图拖住后续流程。 function captureCurrentScreen() { if (captureBackendState.fatalError) throw captureBackendState.fatalError; if (captureBackendState.mode === "shell") return captureWithShellFallback(); var attempts = 3; for (let attempt = 1; attempt <= attempts; attempt += 1) { try { var image = captureScreenOnce(); if (image) { return image; } log("captureScreen() 返回空图片(第 " + attempt + "/" + attempts + " 次)"); } catch (error) { log("captureScreen 失败(第 " + attempt + "/" + attempts + " 次),原因=" + String(error && error.message || error) + ",堆栈=" + errorText(error)); } if (attempt < attempts) { forceScreenRefresh(); } } if (CONFIG.shellCaptureFallback && CONFIG.shellCaptureFallback.enabled) { log("投影截图连续失败,开始验证手机本地命令截图兜底"); return captureWithShellFallback(); } var error = new Error("截图连续失败,请检查锁屏或系统录屏权限;不继续盲点其他流程"); error.captureBackendFatal = true; captureBackendState.fatalError = error; throw error; } function approveWorkflowCapture(state) { var deadline = Date.now() + 30000, verified = false; while (state.pending && Date.now() < deadline) { if (workflowStartupPackage() === "com.android.systemui") { var title = textMatches(/.*AutoJs6.*(?:录制|投放|投射|截取|共享|分享|screen|capture).*/i) .packageName("com.android.systemui").findOnce(); if (title) verified = true; if (verified) { var whole = textMatches(/^(?:整个屏幕|Entire screen|Full screen)$/i).packageName("com.android.systemui").findOnce(); var single = textMatches(/^(?:单个应用|单个应用程序|A single app|Single app)$/i).packageName("com.android.systemui").findOnce(); if (single && !whole) clickWorkflowStartupNode(single); else if (whole && !title) clickWorkflowStartupNode(whole); else if (title && !single) { var allow = textMatches(/^(?:开始|立即开始|允许|Start now|Start|Allow)$/i).packageName("com.android.systemui").findOnce(); if (allow) clickWorkflowStartupNode(allow); } } } else verified = false; sleep(200); } } function requestCapturePermission() { // Android 新版要求申请时 AutoJs6 在前台;授权后业务入口会重新拉起应用宝。 if (typeof context !== "undefined") { var host = String(context.getPackageName()); if (!app.launchPackage(host)) throw new Error("无法打开 AutoJs6 申请截图权限"); waitWorkflowStartupPackage(host, 8000); sleep(400); } var state = {pending:true}; var worker = typeof threads !== "undefined" ? threads.start(function () { approveWorkflowCapture(state); }) : null; try { if (typeof requestScreenCapture === "function") return requestScreenCapture(false); if (typeof images !== "undefined" && typeof images.requestScreenCapture === "function") return images.requestScreenCapture(false); throw new Error("没有可用的截图授权接口"); } finally { state.pending = false; if (worker) { worker.interrupt(); worker.join(1500); if (worker.isAlive()) { var error = new Error("截图授权线程未退出,停止页面操作"); error.unsafeWorker = true; throw error; } } } }