// 公共截图后端:一个投影会话;失败熔断,禁止反复截图拖住后续流程。 function startYibaoCapturePulse() { // 静止广告不产生新帧时,1px手势不会重绘。用不接收触摸的小窗口刷新合成帧。 if (typeof floaty === "undefined" || typeof threads === "undefined" || !floaty.hasPermission()) return null; var pulse = {window: null, worker: null, done: false}; try { pulse.window = floaty.rawWindow(''); pulse.window.setTouchable(false); pulse.worker = threads.start(function () { var alternate = false; while (!pulse.done) { pulse.window.setPosition(alternate ? 0 : 2, Math.round(device.height * .5)); alternate = !alternate; sleep(120); } }); sleep(180); return pulse; } catch (error) { stopYibaoCapturePulse(pulse); log("静止页刷新不可用:" + errorText(error)); return null; } } function stopYibaoCapturePulse(pulse) { if (!pulse) return; pulse.done = true; try { if (pulse.worker) { pulse.worker.interrupt(); pulse.worker.join(1000); } } finally { if (pulse.window) pulse.window.close(); } } function yibaoCaptureGeometry(width,height,screenWidth,screenHeight) { var scale=Math.min(width/screenWidth,height/screenHeight); return {left:Math.round((width-screenWidth*scale)/2),top:Math.round((height-screenHeight*scale)/2), width:Math.round(screenWidth*scale),height:Math.round(screenHeight*scale)}; } function alignYibaoCaptureFrame(image) { if(!image || typeof image.getWidth!=="function" || typeof image.getHeight!=="function")return image; var width=Number(image.getWidth()),height=Number(image.getHeight()); if(width===device.width && height===device.height)return image; var area=yibaoCaptureGeometry(width,height,device.width,device.height),crop=null; try { var key=width+"x"+height+"->"+device.width+"x"+device.height; if(captureBackendState.frameGeometry!==key){ log("投影画布与屏幕不一致,裁剪居中留白并对齐OCR坐标:"+key+" crop="+JSON.stringify(area)); captureBackendState.frameGeometry=key; } crop=images.clip(image,area.left,area.top,area.width,area.height); return images.resize(crop,[device.width,device.height]); }finally{ if(crop && crop!==image && crop.recycle)crop.recycle(); if(image.recycle)image.recycle(); } } function captureCurrentScreen() { if (captureBackendState.fatalError) throw captureBackendState.fatalError; if (captureBackendState.mode === "shell") return captureWithShellFallback(); var attempts = 3; var pulse = null; try { for (let attempt = 1; attempt <= attempts; attempt += 1) { try { var image = captureScreenOnce(); if (image) { return alignYibaoCaptureFrame(image); } log("captureScreen() 返回空图片(第 " + attempt + "/" + attempts + " 次)"); } catch (error) { log("captureScreen 失败(第 " + attempt + "/" + attempts + " 次),原因=" + String(error && error.message || error) + ",堆栈=" + errorText(error)); } if (attempt < attempts) { if (!pulse) pulse = startYibaoCapturePulse(); // 不再用中央1px手势刷新:静态广告会将它识别成点击并拉起第三方应用。 sleep(300); } } } finally { stopYibaoCapturePulse(pulse); } if (!captureBackendState.recoveryUsed && renewYibaoCaptureSession()) return captureCurrentScreen(); if (CONFIG.shellCaptureFallback && CONFIG.shellCaptureFallback.enabled) { log("投影截图连续失败,开始验证手机本地命令截图兜底"); return captureWithShellFallback(); } var error = new Error("截图连续失败,请检查锁屏或系统录屏权限;不继续盲点其他流程"); error.captureBackendFatal = true; captureBackendState.fatalError = error; throw error; } function renewYibaoCaptureSession() { captureBackendState.recoveryUsed=true; // 每轮最多重建一次,不能无限弹授权窗口。 try { if(typeof context==="undefined" || typeof android==="undefined")return false; var keyguard=context.getSystemService(android.content.Context.KEYGUARD_SERVICE); if(keyguard && keyguard.isKeyguardLocked()) { log("截图失败时手机处于锁屏,需手动解锁,不尝试操作锁屏后的页面"); return false; } var previous=getForegroundApp(),target=previous && previous.packageName; if(!target || target==="unknown")target=CONFIG.yibaoPackage; log("截图连续失败,重建一次整个屏幕投影授权,然后回到原页面:"+target); if(typeof images!=="undefined" && typeof images.stopScreenCapture==="function")images.stopScreenCapture(); if(!requestCapturePermission())return false; if(!app.launchPackage(target))return false; var deadline=Date.now()+8000; while(Date.now()