| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #!/usr/bin/env node
- const { execSync } = require('child_process')
- const path = require('path')
- // 从配置文件读取 ADB 路径
- const config = require(path.join(__dirname, '..', '..', 'configs', 'config.js'))
- const projectRoot = path.resolve(__dirname, '..', '..')
- const adbPath = config.adbPath?.path
- ? path.resolve(projectRoot, config.adbPath.path)
- : path.join(projectRoot, 'lib', 'scrcpy-adb', 'adb.exe')
- const action = process.argv[2]
- if (!action) {
- process.exit(1)
- }
- switch (action) {
- case 'tap': {
- const coordX = process.argv[3]
- const coordY = process.argv[4]
- const deviceId = process.argv[5] || ''
-
- if (!coordX || !coordY) {
- process.exit(1)
- }
-
- const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
- const tapCommand = `"${adbPath}" ${deviceFlag}shell input tap ${coordX} ${coordY}`
- execSync(tapCommand, { encoding: 'utf-8', timeout: 1000 })
- process.exit(0)
- }
-
- case 'swipe': {
- const direction = process.argv[3]
- const distance = parseInt(process.argv[4])
- const duration = parseInt(process.argv[5])
- const deviceId = process.argv[6] || ''
-
- if (!direction || !distance || !duration) {
- process.exit(1)
- }
-
- const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
- const sizeCommand = `"${adbPath}" ${deviceFlag}shell wm size`
- const sizeOutput = execSync(sizeCommand, { encoding: 'utf-8', timeout: 1000 })
- const sizeMatch = sizeOutput.match(/(\d+)x(\d+)/)
-
- if (!sizeMatch) {
- process.exit(1)
- }
-
- const screenWidth = parseInt(sizeMatch[1])
- const screenHeight = parseInt(sizeMatch[2])
- const centerX = Math.floor(screenWidth / 2)
- const centerY = Math.floor(screenHeight / 2)
-
- let startX = centerX
- let startY = centerY
- let endX = centerX
- let endY = centerY
-
- switch (direction) {
- case 'up-down':
- startY = centerY - Math.floor(distance / 2)
- endY = centerY + Math.floor(distance / 2)
- break
- case 'down-up':
- startY = centerY + Math.floor(distance / 2)
- endY = centerY - Math.floor(distance / 2)
- break
- case 'left-right':
- startX = centerX - Math.floor(distance / 2)
- endX = centerX + Math.floor(distance / 2)
- break
- case 'right-left':
- startX = centerX + Math.floor(distance / 2)
- endX = centerX - Math.floor(distance / 2)
- break
- default:
- process.exit(1)
- }
-
- const swipeCommand = `"${adbPath}" ${deviceFlag}shell input swipe ${startX} ${startY} ${endX} ${endY} ${duration}`
- execSync(swipeCommand, { encoding: 'utf-8', timeout: 2000 })
- process.exit(0)
- }
- case 'swipe-coords': {
- const x1 = process.argv[3]
- const y1 = process.argv[4]
- const x2 = process.argv[5]
- const y2 = process.argv[6]
- const duration = process.argv[7] || 300
- const deviceId = process.argv[8] || ''
- if (!x1 || !y1 || !x2 || !y2) process.exit(1)
- const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
- const cmd = `"${adbPath}" ${deviceFlag}shell input swipe ${x1} ${y1} ${x2} ${y2} ${duration}`
- execSync(cmd, { encoding: 'utf-8', timeout: 2000 })
- process.exit(0)
- }
- case 'keyevent': {
- const keyCode = process.argv[3]
- const deviceId = process.argv[4] || ''
- if (!keyCode) process.exit(1)
- const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
- execSync(`"${adbPath}" ${deviceFlag}shell input keyevent ${keyCode}`, { encoding: 'utf-8', timeout: 1000 })
- process.exit(0)
- }
- case 'text': {
- const text = process.argv[3]
- const deviceId = process.argv[4] || ''
- if (text === undefined) process.exit(1)
- const deviceFlag = deviceId && deviceId.includes(':') ? `-s ${deviceId} ` : ''
- const escaped = String(text).replace(/"/g, '\\"').replace(/\$/g, '\\$').replace(/`/g, '\\`')
- execSync(`"${adbPath}" ${deviceFlag}shell input text "${escaped}"`, { encoding: 'utf-8', timeout: 5000 })
- process.exit(0)
- }
- default:
- process.exit(1)
- }
|