|
|
@@ -75,9 +75,53 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
this.notifyInfo = []
|
|
|
this.log(`${this.name}, 开始执行!`)
|
|
|
this.initCache()
|
|
|
+ this.checkRecordRequestBody()
|
|
|
this.execComm()
|
|
|
}
|
|
|
|
|
|
+ checkRecordRequestBody() {
|
|
|
+ if (!this.isRequest()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const reqBody = $request.body;
|
|
|
+ if (!reqBody) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const path = $request.path;
|
|
|
+ const cacheKey = this.id + "#" + path.replace("/", "_");
|
|
|
+ if (this.isQuanX()) $prefs.setValueForKey(reqBody, cacheKey);
|
|
|
+ if (this.isLoon() || this.isSurge()) $persistentStore.write(reqBody, cacheKey);
|
|
|
+ if (this.isNode()) {
|
|
|
+ this.node.fs.writeFileSync(
|
|
|
+ `${cacheKey}.json`,
|
|
|
+ reqBody,
|
|
|
+ {
|
|
|
+ flag: "w"
|
|
|
+ },
|
|
|
+ (err) => console.log(err)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ getRequestBody() {
|
|
|
+ const path = $request.path;
|
|
|
+ const cacheKey = this.id + "#" + path.replace("/", "_");
|
|
|
+ if (this.isSurge() || this.isLoon()) {
|
|
|
+ return $persistentStore.read(cacheKey);
|
|
|
+ }
|
|
|
+ if (this.isQuanX()) {
|
|
|
+ return $prefs.valueForKey(cacheKey);
|
|
|
+ }
|
|
|
+ if (this.isNode()) {
|
|
|
+ const fpath = `${cacheKey}.json`;
|
|
|
+ if (!this.node.fs.existsSync(fpath)) {
|
|
|
+ return JSON.parse(
|
|
|
+ this.node.fs.readFileSync(fpath)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// persistence
|
|
|
// initialize cache
|
|
|
initCache() {
|
|
|
@@ -92,9 +136,10 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
if (!this.node.fs.existsSync(fpath)) {
|
|
|
this.node.fs.writeFileSync(
|
|
|
fpath,
|
|
|
- JSON.stringify({}), {
|
|
|
- flag: "wx"
|
|
|
- },
|
|
|
+ JSON.stringify({}),
|
|
|
+ {
|
|
|
+ flag: "wx"
|
|
|
+ },
|
|
|
(err) => console.log(err)
|
|
|
);
|
|
|
}
|
|
|
@@ -105,9 +150,10 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
if (!this.node.fs.existsSync(fpath)) {
|
|
|
this.node.fs.writeFileSync(
|
|
|
fpath,
|
|
|
- JSON.stringify({}), {
|
|
|
- flag: "wx"
|
|
|
- },
|
|
|
+ JSON.stringify({}),
|
|
|
+ {
|
|
|
+ flag: "wx"
|
|
|
+ },
|
|
|
(err) => console.log(err)
|
|
|
);
|
|
|
this.cache = {};
|
|
|
@@ -120,7 +166,7 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
}
|
|
|
|
|
|
getPersistKey() {
|
|
|
- return `private_${this.id}`;
|
|
|
+ return `${this.id}#privateCache`;
|
|
|
}
|
|
|
|
|
|
// store cache
|
|
|
@@ -132,16 +178,18 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
if (this.isNode()) {
|
|
|
this.node.fs.writeFileSync(
|
|
|
`${pKey}.json`,
|
|
|
- data, {
|
|
|
- flag: "w"
|
|
|
- },
|
|
|
+ data,
|
|
|
+ {
|
|
|
+ flag: "w"
|
|
|
+ },
|
|
|
(err) => console.log(err)
|
|
|
);
|
|
|
this.node.fs.writeFileSync(
|
|
|
"root.json",
|
|
|
- JSON.stringify(this.root, null, 2), {
|
|
|
- flag: "w"
|
|
|
- },
|
|
|
+ JSON.stringify(this.root, null, 2),
|
|
|
+ {
|
|
|
+ flag: "w"
|
|
|
+ },
|
|
|
(err) => console.log(err)
|
|
|
);
|
|
|
}
|
|
|
@@ -810,7 +858,9 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
}
|
|
|
|
|
|
getResponseBody() {
|
|
|
- return $response.body
|
|
|
+ if ($response) {
|
|
|
+ return $response.body
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
isGetCookie(reg) {
|
|
|
@@ -944,25 +994,32 @@ function ToolKit(scriptName, scriptId, options) {
|
|
|
return format
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- *
|
|
|
- * @param {Object} options
|
|
|
- * @returns {String} 将 Object 对象 转换成 queryStr: key=val&name=senku
|
|
|
- */
|
|
|
- queryStr(options) {
|
|
|
- let queryString = ''
|
|
|
-
|
|
|
- for (const key in options) {
|
|
|
- let value = options[key]
|
|
|
+ objToQueryStr(obj, encode) {
|
|
|
+ let str = ''
|
|
|
+ for (const key in obj) {
|
|
|
+ let value = obj[key]
|
|
|
if (value != null && value !== '') {
|
|
|
if (typeof value === 'object') {
|
|
|
value = JSON.stringify(value)
|
|
|
+ } else if (encode) {
|
|
|
+ value = encodeURIComponent(value)
|
|
|
}
|
|
|
- queryString += `${key}=${value}&`
|
|
|
+ str += `${key}=${value}&`
|
|
|
}
|
|
|
}
|
|
|
- queryString = queryString.substring(0, queryString.length - 1)
|
|
|
- return queryString
|
|
|
+ str = str.substring(0, str.length - 1)
|
|
|
+ return str
|
|
|
+ }
|
|
|
+
|
|
|
+ parseQueryStr(str) {
|
|
|
+ let obj = {};
|
|
|
+ let str1 = str.split("?");
|
|
|
+ let str2 = str1[1].split("&"); //["a=1", "b=2", "c=3"]
|
|
|
+ for (let i = 0; i < str2.length; i++) {
|
|
|
+ let str3 = str2[i].split("=");
|
|
|
+ obj[str3[0]] = str3[1]
|
|
|
+ }
|
|
|
+ return obj
|
|
|
}
|
|
|
})(scriptName, scriptId, options)
|
|
|
}
|