// 配置信息 - 请根据实际情况修改
const config = {
groups: [
{
name: "测试群聊1",
id: "", // ID
message: "打!",
checkin: true
}
],
delay: 2000,
maxRetries: 3
};

let successCount = 0;
let failedGroups = [];

async function main() {
try {
showNotification('开始执行');

await launchQQ();

for (const group of config.groups) {
  try {
    await processGroup(group);
    successCount++;
  } catch (error) {
    failedGroups.push(`${group.name}: ${error.message}`);
    showNotification(`处理 "${group.name}" 失败: ${error.message}`);
  }
}

showNotification(`脚本执行完毕\n成功: ${successCount} 个群聊\n失败: ${failedGroups.length} 个群聊`);
if (failedGroups.length > 0) {
  showNotification(`失败的:\n${failedGroups.join('\n')}`);
}

} catch (error) {
showNotification(脚本执行出错: ${error.message});
}
}

async function launchQQ() {
showNotification('正在启动QQ...');
const isLaunched = await app.launch('com.tencent.mobileqq');
if (!isLaunched) {
throw new Error('启动QQ失败');
}
await sleep(config.delay);
}

async function processGroup(group) {
showNotification(正在处理群聊: ${group.name});

// 查找并进入群聊
await enterGroupChat(group);

// 如果需要打卡,执行打卡操作
if (group.checkin) {
await performCheckin();
}

// 发送指定消息
if (group.message) {
await sendMessage(group.message);
}

// 返回群列表页面
await backToGroupList();
}

async function enterGroupChat(group) {
// 点击"联系人"选项卡
const contactsTab = await findElementWithRetry(
() => id('com.tencent.mobileqq:id/tab_icon').desc('联系人'),
config.maxRetries
);
if (!contactsTab) {
throw new Error('未找到联系人选项卡');
}
await contactsTab.click();
await sleep(config.delay);

// 点击"群聊"分类
const groupChats = await findElementWithRetry(
() => text('群聊'),
config.maxRetries
);
if (!groupChats) {
throw new Error('未找到群聊分类');
}
await groupChats.click();
await sleep(config.delay);

// 查找群聊
let groupElement;
if (group.id) {
// 优先通过ID查找
groupElement = await findElementWithRetry(
() => id(group.id),
config.maxRetries
);
}

if (!groupElement) {
// 通过群聊名称查找
groupElement = await findElementWithRetry(
() => text(group.name),
config.maxRetries
);
}

if (!groupElement) {
throw new Error('未找到群聊');
}

// 进入群聊
await groupElement.click();
await sleep(config.delay);
}

async function performCheckin() {
showNotification('正在执行打卡...');

// 查找打卡按钮(可能的情况)
const checkinButton = await findElementWithRetry(
() => {
// 可能的打卡按钮文本
const texts = ['打卡', '每日打卡', '群打卡', '签到', '每日签到'];
for (const text of texts) {
const element = findViewWithText(text);
if (element) return element;
}

  // 可能的打卡按钮ID(不同QQ版本可能不同)
  const ids = ['checkin', 'sign', 'daily_checkin'];
  for (const idStr of ids) {
    const element = findViewWithId(idStr);
    if (element) return element;
  }
  
  return null;
},
config.maxRetries

);

if (!checkinButton) {
throw new Error('未找到打卡按钮');
}

// 点击打卡按钮
await checkinButton.click();
await sleep(config.delay);

// 处理可能的打卡弹窗
const confirmButton = await findElementWithRetry(
() => text('确认').clickable(true),
1 // 只尝试一次,因为不是所有打卡都有确认弹窗
);

if (confirmButton) {
await confirmButton.click();
await sleep(config.delay);
}

showNotification('打卡完成');
}

async function sendMessage(message) {
showNotification('正在发送消息...');

// 查找输入框
const inputField = await findElementWithRetry(
() => id('com.tencent.mobileqq:id/input').clickable(true),
config.maxRetries
);

if (!inputField) {
throw new Error('未找到输入框');
}

// 输入消息
await inputField.setText(message);
await sleep(config.delay);

// 查找发送按钮
const sendButton = await findElementWithRetry(
() => id('com.tencent.mobileqq:id/send').clickable(true),
config.maxRetries
);

if (!sendButton) {
throw new Error('未找到发送按钮');
}

// 点击发送按钮
await sendButton.click();
await sleep(config.delay);

showNotification('消息发送完成');
}

async function backToGroupList() {
// 点击返回按钮
const backButton = await findElementWithRetry(
() => id('com.tencent.mobileqq:id/ivTitleBtnLeft').clickable(true),
config.maxRetries
);

if (backButton) {
await backButton.click();
await sleep(config.delay);
}
}

async function findElementWithRetry(finder, maxRetries, interval = config.delay) {
for (let i = 0; i < maxRetries; i++) {
const element = finder();
if (element) {
return element;
}
await sleep(interval);
}
return null;
}

function showNotification(message) {
toast(message);
console.log(message);
}

/**

  • 睡眠函数
  • @param {number} ms - 睡眠时间(毫秒)
  • @returns {Promise} - Promise对象
    */
    function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
    }

// 启动脚本
main();
这个运行时提示语句前缺少;

已存档
HAMI_1594140933 · 8 天前

回答

Hamibot AI
1
· 8 天前

你提到的错误是由于在 JavaScript 中,某些语句前需要分号。虽然 JavaScript 有自动分号插入(ASI)机制,但在某些情况下,缺少分号可能导致语法错误。你可以按照以下方式修改你的代码:

  1. const config = { ... }; 后添加分号。
  2. let successCount = 0;let failedGroups = []; 后添加分号(如果它们前面没有分号的话)。

修改后的代码如下:

// 配置信息 - 请根据实际情况修改
const config = {
  groups: [
    {
      name: "测试群聊1",  
      id: "",             // ID
      message: "打!", 
      checkin: true       
    }
  ],
  delay: 2000,  
  maxRetries: 3  
};

let successCount = 0;
let failedGroups = [];

async function main() {
  try {
    showNotification('开始执行');
    
    await launchQQ();
    
    for (const group of config.groups) {
      try {
        await processGroup(group);
        successCount++;
      } catch (error) {
        failedGroups.push(`${group.name}: ${error.message}`);
        showNotification(`处理 "${group.name}" 失败: ${error.message}`);
      }
    }
    
    showNotification(`脚本执行完毕\n成功: ${successCount} 个群聊\n失败: ${failedGroups.length} 个群聊`);
    if (failedGroups.length > 0) {
      showNotification(`失败的:\n${failedGroups.join('\n')}`);
    }
  } catch (error) {
    showNotification(`脚本执行出错: ${error.message}`);
  }
}

// 其余代码保持不变

确保所有语句都以分号结尾,这样可以避免因 JavaScript 的自动分号插入机制导致的潜在问题。

ℹ️ 本回答由 AI 生成,内容仅供参考