2026-11-02 16:01:02 +08:00

158 lines
9.0 KiB
JavaScript
Raw 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;
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);
var finish = button(["完成"], .70);
var open = button(["打开"], .70);
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(["允许"], .50);
return allow ? action("allow", "允许", allow) : action("wait", "等待安装权限按钮", null);
}
var next = button(["继续安装"], .65);
if (next) return action("install", "继续安装", next);
next = button(["下一步", "安装"], .70);
return next ? action("install", normalizeText(next.text || next.desc), next) : null;
}
var commonInstallBusy = false;
function waitCommonInstaller(timeoutMs, completion) {
var deadline = Date.now() + timeoutMs;
var finished = false, opened = false, seenInstaller = false;
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 (seenInstaller && ++leftFrames >= 2) {
log("安装器未经确认收尾就退出,不判为安装成功");
return false;
}
} else leftFrames = 0;
var page = readCommonInstallPage();
if (inInstaller && (!page || !page.items.length)) {
var items = recognizeScreenSafe();
if (items !== null) page = {packageName: foreground.packageName, items: items};
}
var action = planCommonInstallAction(page, completion);
if (action && (action.kind === "failed" || action.kind === "blocked")) {
log("安装中断:" + action.target);
return false;
}
// 旧云手机成功页可能把“完成”漏掉,只增强已确认的成功页底部。
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;
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) {
log("安装按钮未切页,停止重复点击:" + action.target);
return false;
}
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; }
}
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;
}