string-press.js 1.1 KB

12345678910111213141516171819
  1. /**
  2. * adb method: string-press — 按文字点击(OCR 匹配文字后点击)
  3. */
  4. async function run(action, ctx) {
  5. const { device, variableContext, api, extractVarName } = ctx
  6. const inVars = action.inVars || []
  7. const targetText = inVars.length > 0 ? (variableContext[extractVarName(inVars[0])] || inVars[0]) : action.value
  8. if (!targetText) return { success: false, error: 'string-press 操作缺少文字内容' }
  9. if (!api?.findTextAndGetCoordinate) return { success: false, error: '文字识别 API 不可用' }
  10. const matchResult = await api.findTextAndGetCoordinate(device, targetText)
  11. if (!matchResult.success) return { success: false, error: `文字识别失败: ${matchResult.error != null ? matchResult.error : 'unknown'}` }
  12. const { x, y } = matchResult.clickPosition
  13. if (!api?.sendTap) return { success: false, error: '点击 API 不可用' }
  14. const tapResult = await api.sendTap(device, x, y)
  15. if (!tapResult.success) return { success: false, error: `点击失败: ${tapResult.error != null ? tapResult.error : 'unknown'}` }
  16. return { success: true }
  17. }
  18. module.exports = { run }