adb-interact.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. default:
  75. process.exit(1)
  76. }