string-press-parser.js 1.2 KB

1234567891011121314151617181920212223
  1. /** 语句:string-press 文字识别并点击 */
  2. const types = ['string-press']
  3. function parse(action, parseContext) {
  4. return Object.assign({}, action, { type: 'string-press' })
  5. }
  6. async function execute(action, ctx) {
  7. const { device, api, resolveValue, variableContext } = ctx
  8. const inVars = action.inVars || []
  9. let targetText = inVars.length > 0 ? (variableContext[ctx.extractVarName(inVars[0])] || action.value) : (resolveValue(action.value, variableContext) || action.value)
  10. if (!targetText) return { success: false, error: 'string-press 操作缺少文字内容' }
  11. if (!api.findTextAndGetCoordinate) return { success: false, error: '文字识别 API 不可用' }
  12. const matchResult = await api.findTextAndGetCoordinate(device, targetText)
  13. if (!matchResult.success) return { success: false, error: `文字识别失败: ${matchResult.error}` }
  14. const { x, y } = matchResult.clickPosition
  15. if (!api.sendTap) return { success: false, error: '点击 API 不可用' }
  16. const tapResult = await api.sendTap(device, x, y)
  17. if (!tapResult.success) return { success: false, error: `点击失败: ${tapResult.error}` }
  18. return { success: true }
  19. }
  20. module.exports = { types, parse, execute }