完善惊喜任务逐项执行和存储授权,兼容微信浏览入口及应用推荐下载落点。增加重复运行保护、投影恢复和任务完成复核,并保留原有安装适配。 验证:59项离线回归通过,生成文件一致性、语法及差异空白检查通过。
164 lines
8.0 KiB
JavaScript
164 lines
8.0 KiB
JavaScript
// 公共截图后端:一个投影会话;失败熔断,禁止反复截图拖住后续流程。
|
||
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('<frame w="2" h="2" bg="#40000000"/>');
|
||
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()<deadline) {
|
||
var foreground=getForegroundApp();
|
||
if(foreground && foreground.packageName===target){sleep(600);return true;}
|
||
sleep(300);
|
||
}
|
||
}catch(error){
|
||
if(error && (error.taskInterrupted || error.unsafeWorker) || /InterruptedException|ScriptInterruptedException/.test(String(error)))throw error;
|
||
log("重建截图会话失败:"+errorText(error));
|
||
}
|
||
return false;
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
}
|