JavaScript 脚本规则
JavaScript 规则 @js:
是亦搜的灵魂。在任何规则中都可以使用 @js:
来插入 JavaScript 代码,实现更加复杂的功能。
规划说明
格式: @js:脚本代码
规则说明
结果规则
会成为下一条 地址规则
的 result
,成为下一条除地址规则的 lastResult
。地址规则的响应会成为其他规则的 result
。
筛选模式时,发现地址规则中栏目的附加数据 data
,会在下一步取发现列表时成为 lastResult
的值。
内置变量
变量名 | 可用规则 | 描述 |
---|---|---|
host | 全部规则 | 域名 URL |
baseUrl | 除地址外所有规则 | 当前地址规则的 URL |
result | 除搜索与发现地址外所有规则 | 结果规则的返回值 |
lastResult | 除搜索地址外所有规则 | 上一条结果规则的返回值 |
keyword | 搜索地址规则 | 搜索关键字 |
page | 地址规则 | 地址规则当前页码 |
pageSize | 地址规则 | 地址规则当前分页大小 |
cookie | 全部规则 | 请求规则 cookie 数据 |
kind | 发现地址规则, 发现列表规则 | 筛选模式栏目类别值 |
注意: lastResult
在筛选模式的发现列表规则中为栏目的扩展数据。
内置函数
亦搜内置了常用的一些功能函数和加解密相关函数,以方便规则编写。
alert (msg);
显示消息框, 示例:
@js: alert(Result);
console.log (msg);
调试规则时,显示调试信息。
参数 msg
为消息内容。
@js:
console.log("我是调试信息");
console.error("这是错误信息", 1);
async http (url, [options])
发起GET
或POST
请求,依赖于 fetch
。
参数 url
为请求地址,如果为空,自动设为 host
,如果为相对地址,会自动补全 host
。
参数 options
可选,配置HTTP选项,参考 fetch 函数
示例:
@js:
(async () => {
// GET 请求
let html = await http('https://www.baidu.com');
// POST 请求
let str = await http('http://xxxx', {
method: 'POST',
body: JSON.stringify(data),
cache: 'no-cache',
headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
}
});
// GET 请求 (返回 buffer)
let buffer = await http('https://www.baidu.com', {responseType: 'arraybuffer'});
console.log(buffer, 'buffer 是一个 number[] 原始字节数组');
// 设置重定向次数 (V3.6.0版开始支持)
let html = await http('https://www.baidu.com', null, {maxRedirects: 1});
return html;
})();
async httpRequest (url, [options])
依赖于开发语言的HTTP请求函数,发起GET
、POST
、PUT
或HEAD
请求。
参数 url
为请求地址,如果为空,自动设为 host
,如果为相对地址,会自动补全 host
。
参数 options
可选,配置HTTP选项:
method
: 指定HTTP请求方法,GET
、POST
、PUT
或HEAD
,默认使用GET
。headers
:可选,指定HTTP自定义请求头,应当时一个Map<String, Stirng>
对象。timeout
:可选,设置请求超时时间(毫秒),默认为60000
一分钟。raw
:如果指定为true
则返回的data
字段为原始字节数组,否则自动转为字符串。body
:请求的 body 数据,默认为空。maxRedirects
: 请求最大重定向次数,默认5
。设置为0
禁止重定向。大白版 V3.6.0 开始支持
返回数据:函数返回一个对象,包含如下字段:
data
:响应主体内容headers
:响应HTTP头信息statusCode
:HTTP响应状态码,如 200statusMessage
:HTTP响应状态消息
示例:
@js:
(async () => {
// GET 请求
let response = await httpRequest('https://www.baidu.com');
// POST 请求
let response = await httpRequest('http://xxxx', {
method: 'POST',
body: JSON.stringify(data),
headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
},
raw: true,
timeout: 30000
});
return response.data;
})();
提示
HTTP 请求还可以使用 http.get
, http.post
, http.request
别名方式发起。 大白版 V3.6.0
开始支持 maxRedirects
选项。
async require (url, [useCache])
载入亦搜内置或远程的 JavaScript 文件。
示例:
@js:
(async () => {
// 载入内置的 cheerio 库
await require('cheerio');
// 载入远程的 jquery 库
await require('https://cdn.staticfile.org/jquery/2.0.3/jquery.min.js');
// 载入xxx.js,不使用缓存
await require('xxx.js', false);
})();
提示
require
默认会使用本地缓存。重启 app 时会在后台更新缓存。
内置的 js 库:
名称 | 功能 | 参考资料 |
---|---|---|
cheerio | 服务器端的 jQuery 核心实现, 解析HTML页面 | 官方ReadMe |
unzip | 解压库 | - |
CryptoJS | 加解密库 | CryptoJS |
uuid ();
返回 UUID 字符串。
randomStr (len);
返回指定长度的随机字符串。
示例:
@js: randomStr(16);
localStorage
读写本地数据(功能同浏览器中的 localStorage
类似)。localStorage 在亦搜中的作用域是当前的规则。localStorage 会将数据存储为字符串,所以,如果是非字符串内容,需要自行转换。
示例:
// 写数据
localStorage.setItem('key', '123456');
// 读数据
localStorage.getItem('key');
// 删除数据
localStorage.removeItem('key');
// 清空数据
localStorage.clear();
提示
大白版 V3.4.5
开始支持 localStorage
。
xpath (text, rule);
使用 XPath 解析器解析文本,返回解析结果数组。
参数 text
为待解析的文本
参数 rule
为 XPath 路径表达式,与 @xpath:
规则用法一致
示例:
@js:
var id = xpath(html, '//class/*/@id');
var name = xpath(html, '//class/*/text()');
var url = xpath(html, '//class/a/@href');
cssParse (text, rule);
使用 CSS 解析器解析文本,返回解析结果数据。
参数 text
为待解析的文本
参数 rule
为 CSS选择器表达式,与 @css:
规则用法一致
示例:
@js:
var id = cssParse(html, '.item-id');
var name = cssParse(html, '.list a@title');
var content = cssParse(html, '.content@html');
encodeURIGb2312 (text);
字符串转为 gb2312
字符集后进行URI 编码。
参数 text
为待编码的源字符串
示例:
@js:
let s1 = encodeURIGb2312('世界abc123你好');
encodeBase64 (text, [charsert]);
字符串Base64编码。
参数 text
为待编码的源字符串
可选参数 charsert
为字符集编码,可设置为 gbk
或省略
示例:
@js:
let s1 = encodeBase64(data);
let s2 = encodeBase64(data, 'gbk');
decodeBase64 (text, [charsert]);
Base64字符串解码。
参数 text
为待解码的Base64字符串
可选参数 charsert
为字符集编码,可设置为 gbk
、raw
或省略
@js:
let s1 = decodeBase64(data);
let s2 = decodeBase64(data, 'gbk');
// raw 参数返回解密后内容的字节数组
let s3 = decodeBase64(data, 'raw');
md5 (text);
字符串MD5加密。 参数 text
为待加密的源字符串
示例:
@js:
let s = md5('你好');
AES
ASE 加密解密。 示例:
@js:
let text = '你好';
let keyStr = '0CoJUm6Qyw8W8jud';
let iv = '0102030405060708';
// 可选参数:是否返回 bytes 字节数组。
// 1:返回 bytes 字节数组,不传或非1,返回 base64 字符串
let raw = 1;
// CBC 加密
let s1 = AES.encryptCBC(text, keyStr, iv);
let s1 = AES.encryptCBC(text, keyStr, iv, raw);
// CBC 解密
let s2 = AES.decryptCBC(s1, keyStr, iv);
let s2 = AES.decryptCBC(s1, keyStr, iv, raw);
// ECB 加密
let s3 = AES.encryptECB(text, keyStr);
let s3 = AES.encryptECB(text, keyStr, raw);
// ECB 解密
let s4 = AES.decryptECB(s3, keyStr);
let s4 = AES.decryptECB(s3, keyStr, raw);
RSA
RSA 加密解密。 示例:
let text = '你好';
let publicKey = 'xxxxxx';
let password = '123456';
let md = 131;
let mode = 'pkcs1'; // 或 oaep, 默认使用 pkcs1
// 可选参数:是否返回 bytes 字节数组。
// 1:返回 bytes 字节数组,不传或非1,返回 base64 字符串
let raw = 1;
// rsa 加密
let s2 = RSA.encrypt(text, publicKey, mode, raw);
// rsa 解密
let s3 = RSA.decrypt(s2, publicKey, mode, raw);
// bi RSA 加密
let s1 = RSA.encryptBI(text, publicKey, password, md);
Crypto
其它内置加解密支持 sha1
, sha256
, sha512
等。
示例:
let text = '你好';
// md5
Crypto.md5(text);
// SHA 1 加密, 返回 `hex` 字符串
Crypto.sha1(text);
// SHA 256 加密, 返回 `hex` 字符串
Crypto.sha256(text);
// SHA 512 加密, 返回 `hex` 字符串
Crypto.sha512(text);