|
@@ -74,9 +74,135 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
this.execStatus = true
|
|
this.execStatus = true
|
|
|
this.notifyInfo = []
|
|
this.notifyInfo = []
|
|
|
this.log(`${this.name}, 开始执行!`)
|
|
this.log(`${this.name}, 开始执行!`)
|
|
|
|
|
+ this.initCache()
|
|
|
this.execComm()
|
|
this.execComm()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // persistence
|
|
|
|
|
+ // initialize cache
|
|
|
|
|
+ initCache() {
|
|
|
|
|
+ const pKey = this.getPersistKey();
|
|
|
|
|
+ if (this.isQuanX()) this.cache = JSON.parse($prefs.valueForKey(pKey) || "{}");
|
|
|
|
|
+ if (this.isLoon() || isSurge)
|
|
|
|
|
+ this.cache = JSON.parse($persistentStore.read(pKey) || "{}");
|
|
|
|
|
+
|
|
|
|
|
+ if (this.isNode()) {
|
|
|
|
|
+ // create a json for root cache
|
|
|
|
|
+ let fpath = "root.json";
|
|
|
|
|
+ if (!this.node.fs.existsSync(fpath)) {
|
|
|
|
|
+ this.node.fs.writeFileSync(
|
|
|
|
|
+ fpath,
|
|
|
|
|
+ JSON.stringify({}), {
|
|
|
|
|
+ flag: "wx"
|
|
|
|
|
+ },
|
|
|
|
|
+ (err) => console.log(err)
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ this.root = {};
|
|
|
|
|
+
|
|
|
|
|
+ // create a json file with the given name if not exists
|
|
|
|
|
+ fpath = `${pKey}.json`;
|
|
|
|
|
+ if (!this.node.fs.existsSync(fpath)) {
|
|
|
|
|
+ this.node.fs.writeFileSync(
|
|
|
|
|
+ fpath,
|
|
|
|
|
+ JSON.stringify({}), {
|
|
|
|
|
+ flag: "wx"
|
|
|
|
|
+ },
|
|
|
|
|
+ (err) => console.log(err)
|
|
|
|
|
+ );
|
|
|
|
|
+ this.cache = {};
|
|
|
|
|
+ } else {
|
|
|
|
|
+ this.cache = JSON.parse(
|
|
|
|
|
+ this.node.fs.readFileSync(`${pKey}.json`)
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ getPersistKey() {
|
|
|
|
|
+ return `private_${this.id}`;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // store cache
|
|
|
|
|
+ persistCache() {
|
|
|
|
|
+ const pKey = this.getPersistKey();
|
|
|
|
|
+ const data = JSON.stringify(this.cache, null, 2);
|
|
|
|
|
+ if (this.isQuanX()) $prefs.setValueForKey(data, pKey);
|
|
|
|
|
+ if (this.isLoon() || this.isSurge()) $persistentStore.write(data, pKey);
|
|
|
|
|
+ if (this.isNode()) {
|
|
|
|
|
+ this.node.fs.writeFileSync(
|
|
|
|
|
+ `${pKey}.json`,
|
|
|
|
|
+ data, {
|
|
|
|
|
+ flag: "w"
|
|
|
|
|
+ },
|
|
|
|
|
+ (err) => console.log(err)
|
|
|
|
|
+ );
|
|
|
|
|
+ this.node.fs.writeFileSync(
|
|
|
|
|
+ "root.json",
|
|
|
|
|
+ JSON.stringify(this.root, null, 2), {
|
|
|
|
|
+ flag: "w"
|
|
|
|
|
+ },
|
|
|
|
|
+ (err) => console.log(err)
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ write(data, key) {
|
|
|
|
|
+ this.log(`SET ${key}`);
|
|
|
|
|
+ if (key.indexOf("#") !== -1) {
|
|
|
|
|
+ key = key.substr(1);
|
|
|
|
|
+ if (isSurge || this.isLoon()) {
|
|
|
|
|
+ return $persistentStore.write(data, key);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (this.isQuanX()) {
|
|
|
|
|
+ return $prefs.setValueForKey(data, key);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (this.isNode()) {
|
|
|
|
|
+ this.root[key] = data;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ this.cache[key] = data;
|
|
|
|
|
+ }
|
|
|
|
|
+ this.persistCache();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ read(key) {
|
|
|
|
|
+ this.log(`READ ${key}`);
|
|
|
|
|
+ if (key.indexOf("#") !== -1) {
|
|
|
|
|
+ key = key.substr(1);
|
|
|
|
|
+ if (this.isSurge() || this.isLoon()) {
|
|
|
|
|
+ return $persistentStore.read(key);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (this.isQuanX()) {
|
|
|
|
|
+ return $prefs.valueForKey(key);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (this.isNode()) {
|
|
|
|
|
+ return this.root[key];
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return this.cache[key];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ delete(key) {
|
|
|
|
|
+ this.log(`DELETE ${key}`);
|
|
|
|
|
+ if (key.indexOf("#") !== -1) {
|
|
|
|
|
+ key = key.substr(1);
|
|
|
|
|
+ if (this.isSurge() || this.isLoon()) {
|
|
|
|
|
+ return $persistentStore.write(null, key);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (this.isQuanX()) {
|
|
|
|
|
+ return $prefs.removeValueForKey(key);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (this.isNode()) {
|
|
|
|
|
+ delete this.root[key];
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ delete this.cache[key];
|
|
|
|
|
+ }
|
|
|
|
|
+ this.persistCache();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
//当执行命令的目录不是脚本所在目录时,自动把文件路径改成指令传入的路径并返回完整文件路径
|
|
//当执行命令的目录不是脚本所在目录时,自动把文件路径改成指令传入的路径并返回完整文件路径
|
|
|
getRealPath(fileName) {
|
|
getRealPath(fileName) {
|
|
|
if (this.isNode()) {
|
|
if (this.isNode()) {
|
|
@@ -88,6 +214,26 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
return fileName
|
|
return fileName
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * http://boxjs.com/ => http://boxjs.com
|
|
|
|
|
+ * http://boxjs.com/app/jd => http://boxjs.com
|
|
|
|
|
+ */
|
|
|
|
|
+ getUrlHost(url) {
|
|
|
|
|
+ return url.slice(0, url.indexOf('/', 8))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * http://boxjs.com/ =>
|
|
|
|
|
+ * http://boxjs.com/api/getdata => /api/getdata
|
|
|
|
|
+ */
|
|
|
|
|
+ getUrlPath(url) {
|
|
|
|
|
+ // 如果以结尾, 去掉最后一个/
|
|
|
|
|
+ const end = url.lastIndexOf('/') === url.length - 1 ? -1 : undefined
|
|
|
|
|
+ // slice第二个参数传 undefined 会直接截到最后
|
|
|
|
|
+ // indexOf第二个参数用来跳过前面的 "https://"
|
|
|
|
|
+ return url.slice(url.indexOf('/', 8), end)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async execComm() {
|
|
async execComm() {
|
|
|
//支持node命令,实现发送手机测试
|
|
//支持node命令,实现发送手机测试
|
|
|
if (this.isNode()) {
|
|
if (this.isNode()) {
|
|
@@ -96,7 +242,7 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
if (this.comm[1] == "p") {
|
|
if (this.comm[1] == "p") {
|
|
|
this.isExecComm = true
|
|
this.isExecComm = true
|
|
|
//phone
|
|
//phone
|
|
|
- this.log(`开始执行指令【${this.comm[1]}】=> 发送到手机测试脚本!`)
|
|
|
|
|
|
|
+ this.log(`开始执行指令【${this.comm[1]}】=> 发送到手机测试脚本!`);
|
|
|
if (this.isEmpty(this.options) || this.isEmpty(this.options.httpApi)) {
|
|
if (this.isEmpty(this.options) || this.isEmpty(this.options.httpApi)) {
|
|
|
this.log(`未设置options,使用默认值`)
|
|
this.log(`未设置options,使用默认值`)
|
|
|
//设置默认值
|
|
//设置默认值
|
|
@@ -316,7 +462,7 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
const regex = /(?:#lk\{)(.+?)(?=\})/
|
|
const regex = /(?:#lk\{)(.+?)(?=\})/
|
|
|
let m = regex.exec(ret)
|
|
let m = regex.exec(ret)
|
|
|
if (m !== null) {
|
|
if (m !== null) {
|
|
|
- this.log(`生成BoxJs还有未配置的参数,请参考https://github.com/lowking/Scripts/blob/master/util/example/ToolKitDemo.js#L17-L18传入参数:\n`)
|
|
|
|
|
|
|
+ this.log('生成BoxJs还有未配置的参数,请参考https://github.com/lowking/Scripts/blob/master/util/example/ToolKitDemo.js#L17-L18传入参数:\n')
|
|
|
}
|
|
}
|
|
|
let loseParamSet = new Set()
|
|
let loseParamSet = new Set()
|
|
|
while ((m = regex.exec(ret)) !== null) {
|
|
while ((m = regex.exec(ret)) !== null) {
|