开发日志251204,05
date
Dec 4, 2025
slug
开发日志251204,05
status
Published
tags
计算机
AI
summary
完成了关键词判别 评论功能修复 优化main函数执行方法 可传入多个设备编号同时执行 重构等待运行结果函数waitForDeviceResult() 设置超时时间
type
Post
关键词判别
/**
* 关键词命中检测函数(完美支持你的使用场景)
* 输入:对象 {author: "...", desc: "...", title: "...", ...}
* 输出:只要任意一个字段的值里包含 keywords 中的任意一个词 → 返回 true
*/
function hasKeyword(input, keywords) {
// 防御性检查
if (!input || typeof input !== "object" || input === null) {
return false;
}
if (!keywords || !Array.isArray(keywords) || keywords.length === 0) {
return false;
}
// 遍历对象的所有 value(author、desc、title……)
for (let value of Object.values(input)) {
// 只处理字符串类型的字段
if (typeof value === "string") {
// 遍历每一个敏感词,只要有一个命中就立刻返回 true
for (let word of keywords) {
if (value.includes(word)) {
// 可选:打开下面这行可以看到具体命中了哪个词(调试超好用)
// log(`命中关键词:【${word}】 ← 来自字段值:${value}`);
return true;
}
}
}
}
// 所有字段都没命中
return false;
}优化了代码结构 将大部分通用函数移到了
common.js脚本范例
sample.js ://等待无障碍权限
auto.waitFor();
//定义全局量
const PKG = 'com.ss.android.ugc.aweme'; // 抖音包名
const deviceId = files.read('/sdcard/SN.txt').trim();
let success = 'fail';//声明初始化执行结果
let returns = {};//声明返回结果
//请求common.js通用脚本
require('/sdcard/脚本/common.js');
// 主函数
function main() {
try {
//记录开始时间
const startTime = Date.now();
//初始化屏幕尺寸
setScreenMetrics(width, height);
//打印common文件版本
logInfo.i(`common.js文件版本:${commonVersion}`);
logInfo.i(`初始化屏幕尺寸:${width}*${height}`);
console.show();
logInfo.i('开始运行...');
//==========开始运行
success = 'success';
//==========结束运行
//操作完成
const endTime = Date.now();
const duration = endTime - startTime;
logInfo.s(`脚本执行完毕 耗时${duration}`);
} catch (error) {
logInfo.e(error);
success = 'fail';
}
//隐藏控制台
console.hide();
//通用返回日志
output();
}
// 运行主函数
main();评论功能修复
- 可能出现找错评论框导致输入错位置的bug
commentEditText = className('EditText').focused().findOne();
^^^^^^^^^^添加了
.focused() 方法过滤掉无焦点的文本框 解决- 可能出现评论成功检测错误的问题
应该是函数写错了 犯了个低级错误 getCommentList(a)改为a=getCommentList();
main() 函数调整
- 优化优化main函数执行方法 可传入多个设备编号同时执行
//确定执行设备列表
let processDeviceList = [];
// 未传入参数 或 传入空数组 -> 处理所有设备
if (!deviceNo || !Array.isArray(deviceNo) || deviceNo.length === 0) {
processDeviceList = phoneList;
logInfo.i(`未指定设备,默认执行全部 ${phoneList.length} 台设备`);
}
// 传入了设备号数组 -> 筛选出指定的设备
else {
// 筛选出编号在 deviceNos 数组中的设备
processDeviceList = phoneList.filter(device => deviceNo.includes(device.no));
logInfo.i(`准备执行指定设备,共 ${processDeviceList.length} 台`);
}
// 为每个设备匹配配置并启动异步处理
for (const phone of processDeviceList) {- 重构等待运行结果函数waitForDeviceResult() 设置超时时间
// 通过轮询Node.js API实现异步等待设备运行结果 三分钟超时
async function waitForDeviceResult(deviceId, timeoutMs = 180000) {
const startTime = Date.now();
while (true) {
// 超时保护:超过时间直接放弃
if (Date.now() - startTime > timeoutMs) {
logInfo.w(`设备 ${deviceId} 等待结果超时(${timeoutMs / 1000}秒),任务跳过`);
return { log: ["timeout"], result: false, returns: "timeout" }; // 自定义超时标记
}
try {
const res = await fetch(`/api/getresult?deviceId=${deviceId}`, {
signal: AbortSignal.timeout(5000) // 单个请求最多等5秒(防DNS卡死)
});
if (res.ok) {
const data = await res.json();
if (data) {
return data; // 正常收到结果
}
} else {
logInfo.e(`获取设备 ${deviceId} 结果失败: ${res.status}`);
}
} catch (err) {
// 网络错误、手机断网、fetch 被中断,都到这里
if (err.name === 'TimeoutError') {
// logInfo.d(`设备 ${deviceId} 请求超时,继续轮询...`);
} else {
logInfo.w(`设备 ${deviceId} 网络异常: ${err.message}`);
}
}
// 没拿到结果 → 休息1秒再问(避免打满CPU)
await sleep(1000);
}
}