首页

autojs学习笔记及资源

kkcode
2023-03-10  阅读 401

toastLog site:hexuesong.cn 检索

设备信息

console.show();
    var str = "";
    str += "屏幕宽度:" + device.width;
    str += "\n屏幕高度:" + device.height;
    str += "\nbuildId:" + device.buildId;
    str += "\n主板:" + device.board;
    str += "\n制造商:" + device.brand;
    str += "\n工业设计名称:" + device.device;
    str += "\n产品名称:" + device.product;
    str += "\n型号:" + device.model;
    str += "\nbootloader版本:" + device.bootloader;
    str += "\n硬件名称:" + device.hardware;
    str += "\n唯一标识码:" + device.fingerprint;
    str += "\n硬件序列号:"+device.serial;
    str += "\nIncremental:"+device.incremental;
    str += "\nAndroid系统版本号:"+device.release;
    str += "\nBaseOS:"+device.baseOS;
    str += "\n安全补丁程序级别:"+device.securityPatch;
    str += "\n开发代号:"+device.codename;
    str += "\n内存总量:"+device.getTotalMem();
    str += "\n当前可用内存:"+device.getAvailMem();
    str += "\nAndroidId: " + device.getAndroidId();
    str += "\nMac: " + device.getMacAddress();
    str += "\nAPI: " + device.sdkInt;
    str += "\n电量: " + device.getBattery();
    str += "\nIMEI: " + device.getIMEI();
toastLog(str);复制代码

UI控件

按钮控件

"ui";

ui.layout(
    <vertical padding="16">
        <button text="普通按钮" w="auto"/>
        <button text="带颜色按钮" style="Widget.AppCompat.Button.Colored"
        w="auto"/>
        <button text="无边框按钮" style="Widget.AppCompat.Button.Borderless"
        w="auto"/>
        <button text="无边框有颜色按钮"
        style="Widget.AppCompat.Button.Borderless.Colored" w="auto"/>
        <button text="长长的按钮" w="*"/>
        <button id="click_me" text="点我" w="auto"/>
    </vertical>
);

ui.click_me.on("click", ()=>{
    toast("我被点啦");
});

ui.click_me.on("long_click", ()=>{
    toast("我被长按啦");
});复制代码

复选框与单选框控件

"ui";
ui.layout(
    <vertical padding="16">
        <checkbox id="cb1" text="复选框"/>
        <checkbox id="cb2" checked="true" text="勾选的复选框"/>
        <radiogroup>
        <radio text="单选框1"/>
        <radio text="单选框2"/>
        <radio text="单选框3"/>
        </radiogroup>
        <radiogroup mariginTop="16">
        <radio text="单选框1"/>
        <radio text="单选框2"/>
        <radio text="勾选的单选框3" checked="true"/>
        </radiogroup>
    </vertical>
);

ui.cb1.on("check", (checked)=>{
    if(checked){
        toast("第一个框被勾选了");
    }else{
        toast("第一个框被取消勾选了");
    }
});
复制代码

进度条控件

"ui";

ui.layout(
    <vertical padding="16">
        <text text="处理中..." textColor="black" textSize="16sp"/>
        <progressbar />
        <text text="直线无限进度条" textColor="black" textSize="16sp"
        marginTop="24"/>
        <progressbar indeterminate="true"
        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"/>
        <text text="直线进度条" textColor="black" textSize="16sp" marginTop="24"/>
        <progressbar progress="30"
        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"/>
        <text text="可调节进度条" textColor="black" textSize="16sp"
        marginTop="24"/>
        <seekbar progress="20"/>
        <horizontal gravity="center" marginTop="24">
        <text id="progress_value" textColor="black" textSize="16sp"
        margin="8" text="0"/>
        <progressbar id="progress" w="*"
        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"/>
        </horizontal>
        <button id="download">开始下载</button>
    </vertical>
);

var downloadId = null;

ui.download.click(()=>{
    if(downloadId != null){
        stopDownload();
    }else{
        startDownload();
    }
});

function stopDownload(){
    ui.download.text("开始下载");
    clearInterval(downloadId);
    downloadId = null;
}

function startDownload(){
    if(ui.progress.getProgress() == 100){
        ui.progress.setProgress(0);
    }
    ui.download.text("停止下载");
    downloadId = setInterval(()=>{
        var p = ui.progress.getProgress();
        p++;
        if(p > 100){
            stopDownload();
            return;
        }
        ui.progress.setProgress(p);
        ui.progress_value.setText(p.toString());
    }, 200);
}
复制代码

卡片布局

"ui";
ui.layout(
  <vertical>
    <appbar>
      <toolbar id="toolbar" title="卡片布局" />
    </appbar>
    <card w="*" h="70" margin="10 5" cardCornerRadius="2dp"
      cardElevation="1dp" gravity="center_vertical">
      <vertical padding="18 8" h="auto">
        <text text="写操作系统作业" textColor="#222222" textSize="16sp" />
        <text text="明天第1~2节" textColor="#999999" textSize="14sp" />
      </vertical>
      <View bg="#f44336" h="*" w="10" />
    </card>
    <card w="*" h="70" margin="10 5" cardCornerRadius="2dp"
      cardElevation="1dp" gravity="center_vertical">
      <vertical padding="18 8" h="auto">
        <text text="修复ui模式的Bug" textColor="#222222" textSize="16sp" />
        <text text="无限期" textColor="#999999" textSize="14sp" />
      </vertical>
      <View bg="#ff5722" h="*" w="10" />
    </card>
    <card w="*" h="70" margin="10 5" cardCornerRadius="2dp"
      cardElevation="1dp" gravity="center_vertical">
      <vertical padding="18 8" h="auto">
        <text text="发布Auto.js 5.0.0正式版" textColor="#222222"
          textSize="16sp" />
        <text text="2019年1月" textColor="#999999" textSize="14sp" />
      </vertical>
      <View bg="#4caf50" h="*" w="10" />
    </card>
    <card w="*" h="70" margin="10 5" cardCornerRadius="2dp"
      cardElevation="1dp" gravity="center_vertical">
      <vertical padding="18 8" h="auto">
        <text text="完成毕业设计和论文" textColor="#222222"
          textSize="16sp" />
        <text text="2019年4月" textColor="#999999" textSize="14sp" />
      </vertical>
      <View bg="#2196f3" h="*" w="10" />
    </card>
  </vertical>
);
复制代码

列表控件

"ui";
ui.layout(
  <frame>
    <list id="list">
      <vertical>
        <text id="name" textSize="16sp" textColor="#000000" text="姓名:
{{name}}"/>
        <text id="age" textSize="16sp" textColor="#000000" text="年龄:
{{age}}岁"/>
        <button id="deleteItem" text="删除" />
      </vertical>
    </list>
  </frame>
);
var items = [
  {name: "小明", age: 18}, {name: "小红", age: 30},
  {name: "小东", age: 19}, {name: "小强", age: 31},
  {name: "小满", age: 20}, {name: "小一", age: 32},
  {name: "小和", age: 21}, {name: "小二", age: 1},
  {name: "小贤", age: 22}, {name: "小三", age: 2},
  {name: "小伟", age: 23}, {name: "小四", age: 3},
  {name: "小黄", age: 24}, {name: "小五", age: 4},
  {name: "小健", age: 25}, {name: "小六", age: 5},
  {name: "小啦", age: 26}, {name: "小七", age: 6},
  {name: "小哈", age: 27}, {name: "小八", age: 7},
  {name: "小啊", age: 28}, {name: "小九", age: 8},
  {name: "小啪", age: 29}, {name: "小十", age: 9}
];
ui.list.setDataSource(items);
ui.list.on("item_click", function (item, i, itemView, listView) {
  toast("被点击的人名字为: " + item.name + ",年龄为: " + item.age);
});
ui.list.on("item_bind", function (itemView, itemHolder) {
  itemView.deleteItem.on("click", function () {
    let item = itemHolder.item;
    toast("被删除的人名字为: " + item.name + ",年龄为: " + item.age);
    items.splice(itemHolder.position, 1);
  });
})复制代码

图片控件

"ui";
ui.layout(
  <scroll>
    <vertical bg="#707070" padding="16">
      <text text="网络图片" textColor="black" textSize="16sp" marginTop="16" />
      <img src="http://www.autojs.org/assets/uploads/profile/3-
profileavatar.png"
        w="100" h="100" />
      <text text="带边框的图片" textColor="black" textSize="16sp"
        marginTop="16" />
      <img src="http://www.autojs.org/assets/uploads/profile/1-
profileavatar.jpeg"
        w="100" h="100" borderWidth="2dp" borderColor="#202020" />
      <text text="圆形图片" textColor="black" textSize="16sp" marginTop="16" />
      <img src="http://www.autojs.org/assets/uploads/profile/1-
profileavatar.jpeg"
        w="100" h="100" circle="true" />
      <text text="带边框的圆形图片" textColor="black" textSize="16sp"
        marginTop="16" />
      <img src="http://www.autojs.org/assets/uploads/profile/1-
profileavatar.jpeg"
        w="100" h="100" circle="true" borderWidth="2dp"
        borderColor="#202020" />
      <text text="圆角图片" textColor="black" textSize="16sp" marginTop="16" />
      <img id="rounded_img"
        src="http://www.autojs.org/assets/uploads/profile/1-profileavatar.jpeg"
        w="100" h="100" radius="20dp" scaleType="fitXY" />
      <button id="change_img" text="更改图片" />
    </vertical>
  </scroll>
);
ui.change_img.on("click", () => {
  ui.rounded_img.setSource("http://www.autojs.org/assets/uploads/profile/1-profilecover.jpeg");
});
复制代码

文本控件

"ui";
ui.layout(
  <vertical padding="16">
    <text textSize="40sp">大字</text>
    <text textSize="12sp">小字</text>
    <text textStyle="bold" textColor="black">加粗</text>
    <text textStyle="italic">斜体</text>
    <text textColor="#00ff00">原谅色</text>
    <text margin="8">Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移
      动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大
      陆地区较多人使用“安卓”或“安致”。Android操作系统最初由Andy Rubin开发,主要支持手机。2005年8
      月由Google收购注资。2007年11月,Google与84家硬件制造商、软件开发商及电信营运商组建开放手机联
      盟共同研发改良Android系统。</text>
    <text maxLines="1" ellipsize="end" margin="8">Android是一种基于Linux的自由
      及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领
      导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。Android操作系统最初由Andy
      Rubin开发,主要支持手机。2005年8月由Google收购注资。2007年11月,Google与84家硬件制造商、软
      件开发商及电信营运商组建开放手机联盟共同研发改良Android系统。</text>
    <text maxLines="2" ellipsize="end" margin="8">Android是一种基于Linux的自由
      及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领
      导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。Android操作系统最初由Andy
      Rubin开发,主要支持手机。2005年8月由Google收购注资。2007年11月,Google与84家硬件制造商、软
      件开发商及电信营运商组建开放手机联盟共同研发改良Android系统。</text>
    <text w="*" gravity="center" textSize="20sp">居中</text>
    <text autoLink="all">自动超链接网址www.baidu.com, 邮箱 123@qq.com等</text>
  </vertical>
);
复制代码

下拉菜单

"ui";
ui.layout(
  <vertical padding="16">
    <horizontal>
      <text textSize="16sp">下拉菜单</text>
      <spinner id="sp1" entries="选项1|选项2|选项3" />
    </horizontal>
    <horizontal>
      <text textSize="16sp">对话框菜单</text>
      <spinner id="sp2" entries="选项4|选项5|选项6" spinnerMode="dialog" />
    </horizontal>
    <button id="ok">确定</button>
    <button id="select3">选择选项3</button>
  </vertical>
);
ui.ok.on("click", () => {
  var i = ui.sp1.getSelectedItemPosition();
  var j = ui.sp2.getSelectedItemPosition();
  toast("您的选择是选项" + (i + 1) + "和选项" + (j + 4));
});
ui.select3.on("click", () => {
  ui.sp1.setSelection(2);
});复制代码

自定义控件-带颜色按钮

"ui";
var ColoredButton = (function () {
  //继承ui.Widget
  util.extend(ColoredButton, ui.Widget);
  function ColoredButton() {
    //调用父类构造函数
    ui.Widget.call(this);
    //自定义属性color,定义按钮颜色
    this.defineAttr("color", (view, name, defaultGetter) => {
      return this._color;
    }, (view, name, value, defaultSetter) => {
      this._color = value;
      view.attr("backgroundTint", value);
    });
    //自定义属性onClick,定义被点击时执行的代码
    this.defineAttr("onClick", (view, name, defaultGetter) => {
      return this._onClick;
    }, (view, name, value, defaultSetter) => {
      this._onClick = value;
    });
  }
  ColoredButton.prototype.render = function () {
    return (
      <button textSize="16sp" style="Widget.AppCompat.Button.Colored"
        w="auto" />
    );
  }
  ColoredButton.prototype.onViewCreated = function (view) {
    view.on("click", () => {
      if (this._onClick) {
        eval(this._onClick);
      }
    });
  }
  ui.registerWidget("colored-button", ColoredButton);
  return ColoredButton;
})();
ui.layout(
  <vertical>
    <colored-button text="第一个按钮" color="#ff5722" />
    <colored-button text="第二个按钮" onClick="hello()" />
  </vertical>
);
function hello() {
  alert("Hello ~");
}复制代码

HTTP简介

file

图色处理

颜色获取和检测

if (!requestScreenCapture()) {
  toast("请求截图失败");
  exit
}
sleep(2000);
var x = 760;
var y = 180;
//获取在点(x, y)处的颜色
var c = images.pixel(captureScreen(), x, y);
//显示该颜色
var msg = "";
msg += "在位置(" + x + ", " + y + ")处的颜色为" + colors.toString(c);
msg += "\nR = " + colors.red(c) + ", G = " + colors.green(c) + ", B = " +
  colors.blue(c);
//检测在点(x, y)处是否有颜色#73bdb6 (模糊比较)
var isDetected = images.detectsColor(captureScreen(), "#73bdb6", x, y);
msg += "\n该位置是否匹配到颜色#73bdb6: " + isDetected;
alert(msg);
复制代码

精确找色

if (!requestScreenCapture()) {
  toast("请求截图失败");
  stop();
}
var img = captureScreen();
toastLog("开始找色");
//0x1d75b3为编辑器默认主题蓝色字体(if, var等关键字)的颜色
//找到颜色与0x1d75b3完全相等的颜色
var point = findColorEquals(img, 0x006699);
if (point) {
  toastLog("x = " + point.x + ", y = " + point.y);
} else {
  toastLog("没有找到");
}复制代码

模糊找色

if (!requestScreenCapture()) {
  toast("请求截图失败");
  exit();
}
var img = captureScreen();
//0x9966ff为编辑器紫色字体的颜色
toastLog("开始找色");
var point = findColor(img, 0x9966ff);
if (point) {
  toastLog("x = " + point.x + ", y = " + point.y);
} else {
  toastLog("没有找到");
}复制代码

区域找色1

if (!requestScreenCapture()) {
  toast("请求截图失败");
  exit();
}
var img = captureScreen();
toastLog("开始找色");
//指定在位置(100, 220)宽高为400*400的区域找色。
//#75438a是编辑器默认主题的棕红色字体(数字)颜色,位置大约在第5行的"2000",坐标大约为(283,
465)
var point = findColorInRegion(img, "#75438a", 90, 220, 900, 1000);
if (point) {
  toastLog("x = " + point.x + ", y = " + point.y);
} else {
  toastLog("没有找到");
}复制代码

区域找色2

if (!requestScreenCapture()) {
  toast("请求截图失败");
  exit();
}
var img = captureScreen();
//0xffffff为白色
toastLog("开始找色");
//指定在位置(90, 220)宽高为900*1000的区域找色。
//0xff00cc是编辑器的深粉红色字体(字符串)颜色
var point = findColor(img, "#ff00cc", {
  region: [90, 220, 900, 1000],
  threads: 8
});
if (point) {
  toastLog("x = " + point.x + ", y = " + point.y);
} else {
  toastLog("没有找到");
}复制代码

findOne和findOnce和find的区别

1、findOne()

根据当前的选择器所确定的筛选条件,对屏幕上的控件进行搜索,直到屏幕上出现满足条件的一个控件为止, 并返回该控件。如果找不到控件,当屏幕内容发生变化时会重新寻找,直至找到。

需要注意的是,如果屏幕上一直没有出现所描述的控件,则该函数会阻塞,直至所描述的控件出现为止。因此 此函数不会返回null。

如果想要只在屏幕上搜索一次而不是一直搜索,请使用findOnce()。 另外,如果屏幕上有多个满足条件的控件,findOne()采用深度优先搜索(DFS),会返回该搜索算法找到的第 一个控件。

寻找控件建议不要用findOne()、waitFor()、exists()等一些会阻塞的方法,不然稍微出现一点其他事 件就会阻塞,导致脚本莫名其妙的卡死,使用findOne(timeout)。

2、findOnce()

根据当前的选择器所确定的筛选条件,对屏幕上的控件进行搜索,如果找到符合条件的控件则返回该控件;否 则返回null。

3、find()

查找所有控件。

学习笔记

Autojs脚本开发课程笔记

文档资源链接

  1. Auto.js4.1.0文档
  2. AutoxJs 文档
  3. 飞云脚本
  4. Autojs 4.1.1实战教程
  5. autojs基础入门教程__视频
  6. autojs做一个脚本商店
  7. 【AutoJs】[王者]一键换装源码 - 哔哩哔哩
  8. Auto.js基础入门教程--哔哩哔哩视频
  9. https://github.com/kkevsekk1/AutoX/releases | Releases · kkevsekk1/AutoX
  10. https://github.com/kkevsekk1/webpack-autojs | kkevsekk1/webpack-autojs
本文为作者原创文章,转载无需和我联系,但请注明转载链接。 【前端黑猫】