文件已上传:https://www.pan38.com/share.php?code=XHUZM 提取码:8888
该实现包含三个核心模块,主模块实现自动顶贴流程,配置模块支持外部配置加载,异常处理模块保障稳定运行。使用时需开启无障碍服务,建议间隔时间设置为5分钟以上以避免账号风险。调试模式可查看详细运行日志
// 基础配置参数
const CONFIG = {
targetTieba: "测试吧", // 目标贴吧名称
targetPostTitle: "测试帖", // 目标帖子标题
replyContent: ["支持楼主", "顶一下", "好帖收藏"], // 随机回复内容
interval: 300000, // 操作间隔(5分钟)
maxRetry: 3, // 最大重试次数
debugMode: true // 调试模式
};
// 主执行函数
function main() {
if (!prepareEnvironment()) return;
let retryCount = 0;
while (retryCount < CONFIG.maxRetry) {
try {
launchTieba();
if (!enterTargetTieba()) {
throw new Error("进入贴吧失败");
}
const postFound = findTargetPost();
if (!postFound) {
throw new Error("未找到目标帖子");
}
performReplyAction();
log("顶贴成功完成");
break;
} catch (e) {
retryCount++;
logError(`第${retryCount}次尝试失败: ${e.message}`);
if (retryCount >= CONFIG.maxRetry) {
toast("达到最大重试次数,停止运行");
return;
}
sleep(5000);
} finally {
closeTieba();
}
}
}
// 环境准备检测
function prepareEnvironment() {
if (!auto.service) {
toast("请先开启无障碍服务");
return false;
}
if (!device.isScreenOn()) {
device.wakeUp();
sleep(1000);
}
return true;
}
// 启动贴吧APP
function launchTieba() {
log("正在启动贴吧APP...");
const appName = "百度贴吧";
if (!launch(appName)) {
startActivity({
action: "android.intent.action.MAIN",
packageName: "com.baidu.tieba"
});
}
waitForPackage("com.baidu.tieba");
sleep(3000);
}
// 进入目标贴吧
function enterTargetTieba() {
log(正在进入${CONFIG.targetTieba}...
);
const searchBtn = id("com.baidu.tieba:id/search").findOne(5000);
if (!searchBtn) return false;
searchBtn.click();
sleep(1000);
const inputBox = className("EditText").findOne(5000);
if (!inputBox) return false;
inputBox.setText(CONFIG.targetTieba);
sleep(1000);
const searchConfirm = text("搜索").findOne(5000);
if (!searchConfirm) return false;
searchConfirm.click();
sleep(3000);
return true;
}
// 查找目标帖子
function findTargetPost() {
log(正在查找帖子[${CONFIG.targetPostTitle}]...
);
let found = false;
let scrollCount = 0;
const maxScroll = 5;
while (!found && scrollCount < maxScroll) {
const targetPost = textContains(CONFIG.targetPostTitle).findOne(3000);
if (targetPost) {
targetPost.click();
found = true;
sleep(3000);
break;
}
scrollDown();
scrollCount++;
sleep(2000);
}
return found;
}
// 执行回复操作
function performReplyAction() {
log("准备执行回复操作...");
const randomIndex = Math.floor(Math.random() * CONFIG.replyContent.length);
const replyText = CONFIG.replyContent[randomIndex];
const replyBtn = text("回复").findOne(5000);
if (!replyBtn) throw new Error("找不到回复按钮");
replyBtn.click();
sleep(1000);
const inputArea = className("EditText").findOne(5000);
if (!inputArea) throw new Error("找不到输入框");
inputArea.setText(replyText);
sleep(1000);
const sendBtn = text("发送").findOne(5000);
if (!sendBtn) throw new Error("找不到发送按钮");
sendBtn.click();
sleep(3000);
}
// 关闭贴吧APP
function closeTieba() {
log("正在关闭贴吧APP...");
back();
sleep(1000);
back();
sleep(1000);
home();
}
// 工具函数
function scrollDown() {
const height = device.height;
const width = device.width;
swipe(width / 2, height 0.8, width / 2, height 0.2, 500);
}
function log(msg) {
if (CONFIG.debugMode) {
console.log([DEBUG] ${new Date().toLocaleString()} ${msg}
);
}
}
function logError(msg) {
console.error([ERROR] ${new Date().toLocaleString()} ${msg}
);
}
// 定时执行
setInterval(main, CONFIG.interval);
main(); // 立即执行一次
// 配置文件管理
function loadConfigFromFile() {
try {
const configFile = "/sdcard/tieba_autoreply_config.json";
if (!files.exists(configFile)) {
log("未找到配置文件,使用默认配置");
return;
}
const content = files.read(configFile);
const externalConfig = JSON.parse(content);
// 合并配置
Object.assign(CONFIG, externalConfig);
log("配置文件加载成功");
} catch (e) {
logError("配置文件加载失败: " + e.message);
}
}
// 配置验证
function validateConfig() {
const requiredFields = [
'targetTieba',
'targetPostTitle',
'replyContent',
'interval'
];
for (const field of requiredFields) {
if (!CONFIG[field]) {
throw new Error(`缺少必要配置项: ${field}`);
}
}
if (CONFIG.interval < 60000) {
log("警告:间隔时间过短可能导致账号异常");
}
}
// 初始化时加载配置
loadConfigFromFile();
validateConfig();
// 全局异常捕获
events.on("exit", function() {
log("脚本停止运行");
});
events.on("pause", function() {
log("脚本被暂停");
});
// 未捕获异常处理
threads.start(function() {
while (true) {
try {
main();
sleep(CONFIG.interval);
} catch (e) {
logError("全局异常捕获: " + e.stack);
if (e.message.includes("账号异常")) {
toast("检测到账号异常,停止运行");
exit();
}
}
}
});
// 网络状态监听
events.observeNotification();
events.onNotification(function(notification) {
if (notification.getText().includes("网络")) {
log("网络状态变化: " + notification.getText());
}
});