Skip to content

Commit

Permalink
添加控件信息查看工具
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyJiangWJ committed Apr 29, 2020
1 parent 251110b commit 670a28c
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# 简介

- 本项目是用于快速构建AutoJS自动化脚本项目的模板框架
Expand All @@ -20,6 +19,7 @@
- 支持通过ADB授权之后自动开启无障碍功能
- 封装了常用方法 `CommonFunction` 如保存运行时数据,倒计时延迟等等
- `lib/autojs-tools.dex` 中封装了更新用的一些Java方法,用于优化脚本执行性能,源码见[auto-js-tools](https://github.com/TonyJiangWJ/auto-js-tools)
- 执行 `unit/获取当前页面的布局信息.js` 可以查看当前页面中的控件文本以及id信息 方便开发脚本
- 具体使用详见各个js文件中的说明信息

## 目前基于此项目实现的脚本
Expand Down
21 changes: 20 additions & 1 deletion lib/prototype/FloatyUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2019-12-02 19:05:01
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-04-27 00:07:09
* @Last Modified time: 2020-04-29 14:55:51
* @Description: 悬浮窗工具,单独提出来作为单例使用
*/

Expand Down Expand Up @@ -77,6 +77,9 @@ FloatyUtil.prototype.setFloatyInfo = function (position, text, option) {
if (option.textSize) {
_this.floatyWindow.content.setTextSize(option.textSize)
}
if (typeof option.touchable !== 'undefined') {
_this.floatyWindow.setTouchable(option.touchable)
}
_this.floatyLock.unlock()
})
}
Expand All @@ -101,4 +104,20 @@ FloatyUtil.prototype.setFloatyTextColor = function (colorStr) {
}
}

FloatyUtil.prototype.setFloatyText = function (text, option) {
this.setFloatyInfo(null, text, option)
}

FloatyUtil.prototype.setFloatyPosition = function (x, y, option) {
this.setFloatyInfo({ x: x, y: y }, null, option)
}

FloatyUtil.prototype.setTextSize = function (textSize) {
this.setFloatyInfo(null, null, { textSize: textSize })
}

FloatyUtil.prototype.setTouchable = function (touchable) {
this.setFloatyInfo(null, null, { touchable: touchable })
}

module.exports = new FloatyUtil()
3 changes: 2 additions & 1 deletion sync_libs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: TonyJiangWJ
# @Date: 2020-04-27 09:15:51
# @Last Modified by: TonyJiangWJ
# @Last Modified time: 2020-04-27 23:57:32
# @Last Modified time: 2020-04-29 21:22:27
# @Description:
###
#!/bin/bash
Expand Down Expand Up @@ -46,6 +46,7 @@ target_files=(
"extends/LockScreen-demo.js"
"extends/LockScreen.js"
"test/TestLockScreen.js"
"unit/获取当前页面的布局信息.js"
)
# 定义target_files下标,mac下的bash无法使用dict 暂时这么写
running_queue_dispatcher=0
Expand Down
38 changes: 31 additions & 7 deletions sync_libs_to_device.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: TonyJiangWJ
# @Date: 2020-04-27 10:23:36
# @Last Modified by: TonyJiangWJ
# @Last Modified time: 2020-04-27 15:00:13
# @Last Modified time: 2020-04-29 15:24:41
# @Description: 同步脚本到设备
###
#!/bin/bash
Expand Down Expand Up @@ -46,23 +46,47 @@ sync_file_to_device() {
}

# 目标项目文件夹
target_dirs=( "energy_store" "蚂蚁庄园" "Alipay-Credits" "京东签到" "AutoScriptBase" )
target_dirs=(
"energy_store"
"蚂蚁庄园"
"Alipay-Credits"
"京东签到"
"AutoScriptBase"
)
# 可同步文件
target_files=(
"lib/prototype/RunningQueueDispatcher.js"
"lib/prototype/FloatyUtil.js"
"unit/获取当前页面的布局信息.js"
)
# 定义target_files下标,mac下的bash无法使用dict 暂时这么写
running_queue_dispatcher=0
floaty_util=1

sync_to_all() {
sync_target_js_to_all() {
target_js_idx=$1
target_js=${target_files[$target_js_idx]}
for target_dir in ${target_dirs[@]}; do
sync_file_to_device $target_js $target_dir
done
}

sync_to_all $running_queue_dispatcher
sync_all_js_to_all() {
for target_js in ${target_files[@]}; do
for target_dir in ${target_dirs[@]}; do
sync_file_to_device $target_js $target_dir
done
done
}

sync_all_to_target_dir() {
target_dir_idx=$1
for target_js in ${target_files[@]}; do
target_dir=${target_dirs[$target_dir_idx]}
sync_file_to_device $target_js $target_dir
done
}

# 复制指定文件到所有目标路径
# sync_target_js_to_all 0
# 复制所有文件到所有目标路径
sync_all_to_target_dir 4
# 复制指定文件到指定目录
#sync_file_to_device ${target_files[0]} ${target_dirs[0]}
194 changes: 194 additions & 0 deletions unit/获取当前页面的布局信息.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* @Author: TonyJiangWJ
* @Date: 2020-04-29 14:44:49
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-04-29 21:50:33
* @Description:
*/
let singletonRequire = require('../lib/SingletonRequirer.js')(runtime, this)
let widgetUtils = singletonRequire('WidgetUtils')
let logUtils = singletonRequire('LogUtils')
let floatyInstance = singletonRequire('FloatyUtil')
let commonFunctions = singletonRequire('CommonFunction')
let { config } = require('../config.js')(runtime, this)

if (!floatyInstance.init()) {
toast('创建悬浮窗失败')
exit()
}

// 适配老代码
if (!floatyInstance.hasOwnProperty('setFloatyInfo')) {
floatyInstance.setFloatyText = function (text) {
this.setFloatyInfo(null, text, null)
}

floatyInstance.setPosition = function (x, y) {
this.setFloatyInfo({ x: x, y: y }, null, null)
}
}


floatyInstance.setFloatyInfo({ x: parseInt(config.device_width / 2.7), y: parseInt(config.device_height / 2) }, '即将开始分析', { textSize: 20 })
sleep(1000)
let limit = 3
while (limit > 0) {
floatyInstance.setFloatyText('倒计时' + limit-- + '秒')
sleep(1000)
}
floatyInstance.setFloatyText('正在分析中...')


let uiObjectInfoList = null
let start = new Date().getTime()

let any = widgetUtils.widgetGetOne(/.+/)
if (any) {
let root = getRootContainer(any)
let boundsInfo = root.bounds()
let content = root.text() || root.desc()
let id = root.id()
logUtils.logInfo([
'rootInfo id:{} content: {} bounds:[{}, {}, {}, {}]',
id, content,
boundsInfo.left, boundsInfo.top, boundsInfo.width(), boundsInfo.height()
])
let resultList = iterateAll(root, 1)
uiObjectInfoList = flatMap(flatArrayList, resultList)

floatyInstance.setPosition(parseInt(config.device_width / 5), parseInt(config.device_height / 2))
floatyInstance.setFloatyText('分析完成,请查看日志页面')
} else {
floatyInstance.setPosition(parseInt(config.device_width / 5), parseInt(config.device_height / 2))
floatyInstance.setFloatyText('无法获取任何控件信息')
}
sleep(1000)
floatyInstance.close()
if (uiObjectInfoList) {
let timeCost = new Date().getTime() - start
let total = uiObjectInfoList.length
let logInfoList = uiObjectInfoList.filter(v => v.hasUsableInfo()).map(v => v.toString())
let content = removeMinPrefix(logInfoList).join('\n')
logUtils.debugInfo(content)
dialogs.build({
title: '布局分析结果',
content: commonFunctions.formatString("总分析耗时:{}ms 总控件数:{}\n{}", timeCost, total, content),
negative: '关闭',
negativeColor: 'red',
cancelable: false
})
.on('negative', () => {
exit()
})
.show()
}


function getRootContainer (target) {
if (target === null) {
logUtils.errorInfo("target为null 无法获取root节点", true)
}
if (target.parent() !== null) {
return getRootContainer(target.parent())
} else {
return target
}
}


function iterateAll (root, depth) {
depth = depth || 1
let uiObjectInfo = new UiObjectInfo(root, depth)
logUtils.logInfo(uiObjectInfo.toString())
if (root.getChildCount() > 0) {
return [uiObjectInfo].concat(root.children().map(child => iterateAll(child, depth + 1)))
} else {
return uiObjectInfo
}
}

function UiObjectInfo (uiObject, depth) {
this.content = uiObject.text() || uiObject.desc() || ''
this.isDesc = typeof uiObject.desc() !== 'undefined' && uiObject.desc() !== ''
this.id = uiObject.id()
this.boundsInfo = uiObject.bounds()
this.depth = depth


this.toString = function () {
return commonFunctions.formatString(
'{}{}{}',
new Array(this.depth).join('-'),
this.isEmpty(this.id) ? '' : ' id:[' + this.id + ']',
this.isEmpty(this.content) ? '' :
commonFunctions.formatString(
' [{}]content: [{}] bounds:[{}, {}, {}, {}]',
(this.isDesc ? 'desc' : 'text'), this.content,
this.boundsInfo.left, this.boundsInfo.top,
this.boundsInfo.width(), this.boundsInfo.height()
)

)
}

this.isEmpty = function (v) {
return typeof v === 'undefined' || v === null || v === ''
}

this.hasUsableInfo = function () {
return !(this.isEmpty(this.content) && this.isEmpty(this.id))
}
}

function flatMap (f, list) {
return list.map(f).reduce((x, y) => x.concat(y), [])
}

function Queue (size) {
this.size = size || 10
this.pushTime = 0
this.queue = []

this.enqueue = function (val) {
if (this.queue.length >= this.size) {
this.queue.shift()
}
this.pushTime++
this.queue.push(val)
}

this.dequeue = function () {
return this.queue.shift()
}

this.peek = function () {
return this.queue[0]
}
}


function flatArrayList (a) {
if (a instanceof UiObjectInfo) {
return a
} else {
return flatMap(flatArrayList, a)
}
}

function removeMinPrefix (list) {
let min = 10000000
let minQueue = new Queue(3)
const regex = /^(-+)[^-]+$/
let result = list.map(l => {
if (regex.test(l)) {
let prefixLength = regex.exec(l)[1].length
if (prefixLength < min) {
minQueue.enqueue(prefixLength)
min = prefixLength
}
}
return l
}).map(l => l.substring(minQueue.peek()))
console.log(JSON.stringify(minQueue))
return result
}

0 comments on commit 670a28c

Please sign in to comment.