adb-interact.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env node
  2. const { execSync } = require('child_process')
  3. const path = require('path')
  4. // 从配置文件读取 ADB 路径
  5. const config = require(path.join(__dirname, '..', '..', 'configs', 'config.js'))
  6. const projectRoot = path.resolve(__dirname, '..', '..')
  7. const adbPath = config.adbPath?.path
  8. ? path.resolve(projectRoot, config.adbPath.path)
  9. : path.join(projectRoot, 'lib', 'scrcpy-adb', 'adb.exe')
  10. const action = process.argv[2]
  11. if (!action) {
  12. process.exit(1)
  13. }
  14. switch (action) {
  15. case 'tap': {
  16. const coordX = process.argv[3]
  17. const coordY = process.argv[4]
  18. const deviceId = process.argv[5] || ''
  19. if (!coordX || !coordY) {
  20. process.exit(1)
  21. }
  22. const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
  23. const tapCommand = `"${adbPath}" ${deviceFlag}shell input tap ${coordX} ${coordY}`
  24. execSync(tapCommand, { encoding: 'utf-8', timeout: 1000 })
  25. process.exit(0)
  26. }
  27. case 'swipe': {
  28. const direction = process.argv[3]
  29. const distance = parseInt(process.argv[4])
  30. const duration = parseInt(process.argv[5])
  31. const deviceId = process.argv[6] || ''
  32. if (!direction || !distance || !duration) {
  33. process.exit(1)
  34. }
  35. const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
  36. const sizeCommand = `"${adbPath}" ${deviceFlag}shell wm size`
  37. const sizeOutput = execSync(sizeCommand, { encoding: 'utf-8', timeout: 1000 })
  38. const sizeMatch = sizeOutput.match(/(\d+)x(\d+)/)
  39. if (!sizeMatch) {
  40. process.exit(1)
  41. }
  42. const screenWidth = parseInt(sizeMatch[1])
  43. const screenHeight = parseInt(sizeMatch[2])
  44. const centerX = Math.floor(screenWidth / 2)
  45. const centerY = Math.floor(screenHeight / 2)
  46. let startX = centerX
  47. let startY = centerY
  48. let endX = centerX
  49. let endY = centerY
  50. switch (direction) {
  51. case 'up-down':
  52. startY = centerY - Math.floor(distance / 2)
  53. endY = centerY + Math.floor(distance / 2)
  54. break
  55. case 'down-up':
  56. startY = centerY + Math.floor(distance / 2)
  57. endY = centerY - Math.floor(distance / 2)
  58. break
  59. case 'left-right':
  60. startX = centerX - Math.floor(distance / 2)
  61. endX = centerX + Math.floor(distance / 2)
  62. break
  63. case 'right-left':
  64. startX = centerX + Math.floor(distance / 2)
  65. endX = centerX - Math.floor(distance / 2)
  66. break
  67. default:
  68. process.exit(1)
  69. }
  70. const swipeCommand = `"${adbPath}" ${deviceFlag}shell input swipe ${startX} ${startY} ${endX} ${endY} ${duration}`
  71. execSync(swipeCommand, { encoding: 'utf-8', timeout: 2000 })
  72. process.exit(0)
  73. }
  74. case 'swipe-coords': {
  75. const x1 = process.argv[3]
  76. const y1 = process.argv[4]
  77. const x2 = process.argv[5]
  78. const y2 = process.argv[6]
  79. const duration = process.argv[7] || 300
  80. const deviceId = process.argv[8] || ''
  81. if (!x1 || !y1 || !x2 || !y2) process.exit(1)
  82. const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
  83. const cmd = `"${adbPath}" ${deviceFlag}shell input swipe ${x1} ${y1} ${x2} ${y2} ${duration}`
  84. execSync(cmd, { encoding: 'utf-8', timeout: 2000 })
  85. process.exit(0)
  86. }
  87. case 'keyevent': {
  88. const keyCode = process.argv[3]
  89. const deviceId = process.argv[4] || ''
  90. if (!keyCode) process.exit(1)
  91. const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
  92. execSync(`"${adbPath}" ${deviceFlag}shell input keyevent ${keyCode}`, { encoding: 'utf-8', timeout: 1000 })
  93. process.exit(0)
  94. }
  95. case 'text': {
  96. const text = process.argv[3]
  97. const deviceId = process.argv[4] || ''
  98. if (text === undefined) process.exit(1)
  99. const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
  100. const escaped = String(text).replace(/"/g, '\\"').replace(/\$/g, '\\$').replace(/`/g, '\\`')
  101. execSync(`"${adbPath}" ${deviceFlag}shell input text "${escaped}"`, { encoding: 'utf-8', timeout: 5000 })
  102. process.exit(0)
  103. }
  104. default:
  105. process.exit(1)
  106. }