hmz 8de61173e0 修复应用宝一条龙任务恢复、常亮及公共浏览安装流程
完善惊喜任务逐项执行和存储授权,兼容微信浏览入口及应用推荐下载落点。增加重复运行保护、投影恢复和任务完成复核,并保留原有安装适配。

验证:59项离线回归通过,生成文件一致性、语法及差异空白检查通过。
2026-11-18 16:05:57 +08:00

205 lines
12 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 公共安装器:vivo/OPPO 原生控件优先;惊喜任务保留“完成”收尾策略。
function readCommonInstallPage() {
try {
var root = auto.rootInActiveWindow;
if (!root) return null;
if (typeof root.refresh === "function") root.refresh();
var packageName = String(root.getPackageName() || "");
if (packageName !== CONFIG.yibaoPackage && !isInstallerForeground({ packageName: packageName })) return null;
var items = [];
var count = 0;
function visit(node, depth) {
if (!node || depth > 40 || ++count > 2000) throw new Error("安装节点树超出读取上限");
if (!node.isVisibleToUser()) return;
var rect = new android.graphics.Rect();
node.getBoundsInScreen(rect);
var text = String(node.getText() || "");
var desc = String(node.getContentDescription() || "");
if ((text || desc) && rect.right > rect.left && rect.bottom > rect.top) {
items.push({ text: text, desc: desc, resourceId: String(node.getViewIdResourceName() || ""),
enabled: Boolean(node.isEnabled()),
bounds: { left: rect.left, top: rect.top, right: rect.right, bottom: rect.bottom },
centerX: (rect.left + rect.right) / 2, centerY: (rect.top + rect.bottom) / 2 });
}
for (var i = 0; i < node.getChildCount(); i += 1) visit(node.getChild(i), depth + 1);
}
visit(root, 0);
return { packageName: packageName, items: items };
} catch (error) {
log("安装原生控件读取失败,本帧不操作: " + errorText(error));
return null;
}
}
function planCommonInstallAction(page, completion) {
if (!page) return null;
var inInstaller = isInstallerForeground(page);
if (!inInstaller && page.packageName !== CONFIG.yibaoPackage) return null;
var items = page.items || [];
var corpus = normalizeText(items.map(function (item) { return item.text || item.desc || ""; }).join(" "));
function button(targets, bottomRatio, resourceId) {
var matches = items.filter(function (item) {
return item.enabled !== false && item.bounds && item.bounds.top >= 0 &&
item.bounds.bottom <= device.height && item.bounds.left >= 0 && item.bounds.right <= device.width &&
item.centerY >= device.height * bottomRatio &&
(!resourceId || item.resourceId === resourceId) &&
(targets.indexOf(normalizeText(item.text)) >= 0 || targets.indexOf(normalizeText(item.desc)) >= 0);
});
matches.sort(function (a, b) { return b.centerY - a.centerY; });
return matches.length ? matches[0] : null;
}
function action(kind, target, item) { return { kind: kind, target: target, item: item }; }
if (!inInstaller) {
// 只操作已实测的应用宝详情页底部按钮,不把任务卡片/推广里的“安装”当作按钮。
var detailInstall = button(["安装", "立即安装"], .80, CONFIG.yibaoPackage + ":id/wt");
if (detailInstall) return action("install", "安装", detailInstall);
var detailOpen = button(["打开", "立即打开"], .80, CONFIG.yibaoPackage + ":id/wt");
if (detailOpen) return action("open", "打开", detailOpen);
// 广告内的网页详情页不一定有 wt ID,须先同时确认多项应用详情标签。
var detailHits = CONFIG.extraTask.detailTexts.filter(function (label) {
return corpus.indexOf(normalizeText(label)) >= 0;
}).length;
if (detailHits < 2) return null;
var webButton = button(["安装", "立即安装", "打开", "立即打开", "立即下载"], .80);
if (!webButton) return null;
var label = normalizeText(webButton.text || webButton.desc);
return action(label.indexOf("打开") >= 0 ? "open" : "install", label, webButton);
}
if (/安装失败|未安装此应用|应用未安装|安装包损坏|解析.*错误/.test(corpus)) return action("failed", "安装失败", null);
if (/输入.*密码|验证身份|指纹验证|人脸验证/.test(corpus)) return action("blocked", "需要手动身份验证", null);
if (/正在安装|安装中|正在验证|正在准备|正在检测|正在扫描/.test(corpus)) return action("wait", "安装或检测中", null);
// OPPO PackageInstaller 的确认/完成弹窗居中,按钮约在屏幕58%处。
// 已限定系统安装器包及精确按钮文字,不能继续套用整屏安装页的底部70%条件。
var finish = button(["完成"], .35);
var open = button(["打开"], .35);
if (/安装完成|安装成功|已成功安装/.test(corpus) || finish) {
// 用户指定安装结束选“打开”;没有打开时兼容原来的“完成”路径。
if (completion === "finish") return finish ? action("finish", "完成", finish) : action("wait", "等待完成按钮", null);
if (open) return action("open", "打开", open);
return finish ? action("finish", "完成", finish) : action("wait", "等待安装结束按钮", null);
}
if (/应用宝.*(?:请求使用安装权限|正在发起安装)/.test(corpus)) {
var allow = button(["允许"], .35);
return allow ? action("allow", "允许", allow) : action("wait", "等待安装权限按钮", null);
}
var next = button(["继续安装"], .35);
if (next) return action("install", "继续安装", next);
next = button(["下一步", "安装"], .35);
return next ? action("install", normalizeText(next.text || next.desc), next) : null;
}
var commonInstallBusy = false;
function readCommonInstallPositiveRegion(page) {
var cancel = page && page.items.filter(function (item) {
return item.resourceId === "android:id/button2" && normalizeText(item.text) === "取消" &&
item.bounds && item.bounds.right < device.width * .95;
})[0];
if (!cancel) return [];
// OPPO 不向服务暴露 button1,整屏OCR又把“取消 安装”合成一个框。
// 依据真实取消按钮bounds裁剪右侧同一行,仅识别明确的正向文字,不推测点击位置。
var b = cancel.bounds;
var region = {x:b.right / device.width, y:b.top / device.height,
width:(device.width - b.right) / device.width, height:(b.bottom - b.top) / device.height, scale:3};
var previous = CONFIG.adButtonCrop;
try {
CONFIG.adButtonCrop = region;
return recognizeAdButtonRegion(["安装", "安裝", "继续安装", "下一步", "完成", "打开"], region).map(function (item) {
item.text = String(item.text).replace(/裝/g, "装");
return item;
});
} finally { CONFIG.adButtonCrop = previous; }
}
function waitCommonInstaller(timeoutMs, completion) {
var deadline = Date.now() + timeoutMs;
var finished = false, opened = false, seenInstaller = false, successSeen = false, manualKey = "";
var pending = "", frames = 0, leftFrames = 0, counts = {};
var previousBusy = commonInstallBusy;
commonInstallBusy = true;
try {
while (Date.now() < deadline) {
var foreground = getForegroundApp();
var inInstaller = isInstallerForeground(foreground);
if (inInstaller) seenInstaller = true;
if (!inInstaller && foreground) {
if (finished) return backToYibao(30000);
if (opened && isThirdPartyForeground(foreground)) return backToYibao(30000) ? "opened" : false;
if (successSeen) return backToYibao(30000);
if (seenInstaller && ++leftFrames >= 2) {
log("安装器未经确认收尾就退出,不判为安装成功");
return false;
}
} else leftFrames = 0;
var page = readCommonInstallPage();
var action = planCommonInstallAction(page, completion);
// OPPO 会只向 AutoJs6 暴露“取消”,安装按钮在同一弹窗中却漏出无障碍树。
// 部分节点存在也必须补OCR;不能只在整页为空时才启用兜底。
if (inInstaller && (!page || !page.items.length || !action)) {
var items = recognizeScreenSafe();
if (items !== null) page = {packageName: foreground.packageName, items: (page ? page.items : []).concat(items)};
action = planCommonInstallAction(page, completion);
if (!action && page) {
page.items = page.items.concat(readCommonInstallPositiveRegion(page));
action = planCommonInstallAction(page, completion);
}
}
if (action && (action.kind === "failed" || action.kind === "blocked")) {
log("安装中断:" + action.target);
return false;
}
if (action && (action.kind === "finish" || action.kind === "open")) successSeen = true;
// 旧云手机成功页可能把“完成”漏掉,只增强已确认的成功页底部。
if (completion === "finish" && action && action.target === "等待完成按钮") {
var crop = recognizeAdButtonRegion(["完成"], {x:0,y:.80,width:1,height:.20,scale:2});
action = planCommonInstallAction({packageName: page.packageName, items: crop}, completion);
}
if (!action || !action.item) { pending = ""; frames = 0; sleep(1000); continue; }
var key = page.packageName + ":" + action.kind + ":" + action.target;
if (manualKey === key) { sleep(1000); continue; }
var position = key + ":" + Math.round(action.item.centerX / 24) + "," + Math.round(action.item.centerY / 24);
frames = pending === position ? frames + 1 : 1;
pending = position;
if (frames >= CONFIG.reliability.confirmFrames) {
counts[key] = (counts[key] || 0) + 1;
if (counts[key] > CONFIG.reliability.clickVerifyRetries) {
manualKey = key;
deadline = Date.now() + 300000;
var message = "系统未响应自动点击,请手动点击“" + action.target + "”;脚本保持亮屏等待5分钟,确认后自动继续";
log(message); toast(message);
setStep("等待手动确认安装器:“" + action.target + "”");
if (typeof yibaoWorkflowState !== "undefined" && yibaoWorkflowState) {
yibaoWorkflowState.manualInstall = {at:new Date().toISOString(),target:action.target,status:"waiting"};
persistYibaoWorkflowState();
}
continue;
}
log("公共安装点击“" + action.target + "” bounds=" + JSON.stringify(action.item.bounds));
clickOcrMatch({item: action.item, target: action.target}, 0);
if (action.kind === "finish") finished = true;
if (action.kind === "open") opened = true;
pending = ""; frames = 0;
}
sleep(1000);
}
log("公共安装流程超时,保留页面,不把安装成功当成任务完成");
return false;
} finally {
commonInstallBusy = previousBusy;
if (typeof yibaoWorkflowState !== "undefined" && yibaoWorkflowState && yibaoWorkflowState.manualInstall) {
yibaoWorkflowState.manualInstall.status = successSeen || finished || opened ? "confirmed" : "unconfirmed";
persistYibaoWorkflowState();
}
}
}
function recoverCommonInstaller() {
if (commonInstallBusy || !isInstallerForeground(getForegroundApp())) return false;
log("优先处理前台安装器,暂停任务扫描和滑动");
var completion = typeof activeYibaoFlow !== "undefined" && activeYibaoFlow ? activeYibaoFlow.completion : "finish";
if (!waitCommonInstaller(CONFIG.extraTask.installTimeoutMs, completion)) {
var error = new Error("公共安装尚未完成,停止后续页面操作");
error.taskInterrupted = true;
throw error;
}
return true;
}