shawenguan 2 ani în urmă
părinte
comite
3247e35d45

+ 3 - 0
Scripts/gandart/gandart.rewirte.quanx.conf

@@ -0,0 +1,3 @@
+hostname = api2.gandart.com
+
+^https:\/\/(api|api2).gandart.com url script-analyze-echo-response https://git.jojo21.cf/shawenguan/Quantumult-X/raw/master/Scripts/gandart/gandartHelper.js

Fișier diff suprimat deoarece este prea mare
+ 1 - 1
Scripts/gandart/gandartHelper.js


+ 148 - 2
Scripts/util/ToolKit.js

@@ -74,9 +74,135 @@ function ToolKit(scriptName, scriptId, options) {
             this.execStatus = true
             this.notifyInfo = []
             this.log(`${this.name}, 开始执行!`)
+            this.initCache()
             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) {
             if (this.isNode()) {
@@ -88,6 +214,26 @@ function ToolKit(scriptName, scriptId, options) {
             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() {
             //支持node命令,实现发送手机测试
             if (this.isNode()) {
@@ -96,7 +242,7 @@ function ToolKit(scriptName, scriptId, options) {
                 if (this.comm[1] == "p") {
                     this.isExecComm = true
                     //phone
-                    this.log(`开始执行指令【${this.comm[1]}】=> 发送到手机测试脚本!`)
+                    this.log(`开始执行指令【${this.comm[1]}】=> 发送到手机测试脚本!`);
                     if (this.isEmpty(this.options) || this.isEmpty(this.options.httpApi)) {
                         this.log(`未设置options,使用默认值`)
                         //设置默认值
@@ -316,7 +462,7 @@ function ToolKit(scriptName, scriptId, options) {
                         const regex = /(?:#lk\{)(.+?)(?=\})/
                         let m = regex.exec(ret)
                         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()
                         while ((m = regex.exec(ret)) !== null) {

Fișier diff suprimat deoarece este prea mare
+ 0 - 0
Scripts/util/ToolKit.min.js


Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff