Skip to content

Instantly share code, notes, and snippets.

@fishyer
Last active June 5, 2024 14:42
Show Gist options
  • Save fishyer/f045f6b101062987869c2c2e5d4f4c1e to your computer and use it in GitHub Desktop.
Save fishyer/f045f6b101062987869c2c2e5d4f4c1e to your computer and use it in GitHub Desktop.
通过无障碍服务来自动化操作Android设备,获取微信公众号文章历史列表
// 获取脚本配置
const { SHOW_CONSOLE } = hamibot.env;
// 快速模式: fast normal
auto("normal");
// 等待开启无障碍权限
auto.waitFor();
// 显示控制台
if (SHOW_CONSOLE) {
console.show();
sleep(300);
// 修改控制台位置
console.setPosition(0, 100);
// 修改控制台大小
console.setSize(device.width, device.height - 200);
}
toastLog("script is start");
// 点击按钮
// while (!click('发消息'));
let scroll_count = 0;
let article_cache = [];
let article_date = "";
while (true) {
if (scroll_count >= 50) {
break;
}
// 滑动之后,需要重新获取列表控件
let article_list = id("com.tencent.mm:id/lqa").findOne();
article_list.children().forEach(function (child) {
//需要判空 是因为可能被系统回收了
if (child != null) {
let text_list = child.find(className("TextView"));
if (text_list.length > 0) {
article_date = text_list[0].text();
if (article_cache.indexOf(article_date) == -1) {
article_cache.push(article_date);
// log(article_cache.length,article_date);
if (["已无更多订阅消息", "作者精选"].indexOf(article_date) != -1) {
// 不是文章的数据
log(article_cache.length,article_date);
} else {
if (text_list.length >= 3) {
//正常显示的文章
let article_title = text_list[1].text();
let article_read = text_list[2].text();
log(article_cache.length,article_date, article_title, article_read);
} else {
//异常情况
log(article_cache.length,text_list.length,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
for (let j = 0; j < text_list.length; j++) {
log(text_list[j].text());
}
}
}
}
}
}
});
if (article_date == "已无更多订阅消息") {
break;
}
sleep(1000);
article_list.scrollForward();
scroll_count++;
}
log("文章列表遍历完成,文章数量: " + article_cache.length);
log("scroll_count: " + scroll_count);
toastLog("script is end");
// sleep(3000);
// console.hide();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment