forked from enkiger/Rewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjd_price_lite.js
568 lines (541 loc) · 21.5 KB
/
jd_price_lite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*************************************
项目功能:京东比价-弹窗版
使用声明:仅供学习与交流,请勿转载与贩卖!⚠️⚠️⚠️
**************************************
# Loon/Surge
[Script]
http-response ^https?://api\.m\.jd\.com/(client\.action|api)\?functionId=(wareBusiness|serverConfig|basicConfig|lite_wareBusiness|pingou_item) requires-body=1,script-path=https://raw.githubusercontent.com/chxm1023/Rewrite/main/jd_price_lite.js, tag=京东比价-弹窗
# QuantumultX
^https?://api\.m\.jd\.com/(client\.action|api)\?functionId=(wareBusiness|serverConfig|basicConfig|lite_wareBusiness|pingou_item) url script-response-body https://raw.githubusercontent.com/chxm1023/Rewrite/main/jd_price_lite.js
[MITM]
hostname = api.m.jd.com
Boxjs订阅:https://raw.githubusercontent.com/githubdulong/Script/master/boxjs.json
*************************************/
const path1 = "serverConfig";
const path2 = "wareBusiness";
const path2h = "wareBusiness.style";
const path3 = "basicConfig";
const path4 = "pingou_item";
const consolelog = false;
const url = $request.url;
const body = $response.body;
const $tool = tool();
let cookie = $tool.getCache("jfCookie") || "";
let cscheme = $tool.getCache("jfChooseScheme") || "jd"; // "jx","js","jk" 京东、京喜、京东极速版、京东健康
let jsapp = [true, "true"].includes($tool.getCache("jfusejsapp")) || false; // 对极速版是否也跳转 app,注意即使跳转 app,也需要先加入购物车返回再结账,否则无法使用极速版优惠券
let browser = $tool.getCache("chooseBrowser") || "Safari";
let jfAutoScheme = $tool.getCache("jfAutoScheme"); // 本地不使用 BoxJs 可自行更改 let jfAutoScheme = true / false
let jfConvert = $tool.getCache("jfUseConvert");
let chooseScheme;
let chooseBrowser;
switch (cscheme) {
case "jd":
chooseScheme = "openjd";
break;
case "jx":
chooseScheme = "openapp.jdpingou";
break;
case "jk":
chooseScheme = "openApp.jdHealth";
break;
case "js":
if (jsapp) {
chooseScheme = "openjdlite";
break;
}
case "browser":
chooseScheme = "browser";
break;
}
switch (browser) {
case "Safari":
chooseBrowser = "";
break;
case "Alook":
chooseBrowser = "Alook://";
break;
case "Foxok":
chooseBrowser = "Foxok://";
break;
case "Edge":
chooseBrowser = "Edge://";
break;
case "Chrome":
chooseBrowser = "Chrome://";
break;
case "Focus":
chooseBrowser = "Focus://";
break;
default:
chooseBrowser = $tool.getCache("jfDIYScheme");
}
const autoChoose = jfAutoScheme == undefined ? true : [true, "true"].includes(jfAutoScheme);
const useConvert = jfAutoScheme == undefined ? true : [true, "true"].includes(jfConvert);
let autoScheme;
let cookiesArr = [
$tool.getCache("CookieJD"),
$tool.getCache("CookieJD2"),
...cookieParse($tool.getCache("CookiesJD") || "[]").map((item) => item.cookie),
].filter((item) => !!item);
function cookieParse(str) {
if (typeof str == "string") {
try {
return JSON.parse(str);
} catch (e) {
console.log(e);
$.msg($.name, "", "请勿随意在BoxJs输入框修改内容\n建议通过脚本去获取cookie");
return [];
}
}
}
cookie = cookie ? cookie : cookiesArr[0];
if (url.indexOf(path1) != -1) {
let obj = JSON.parse(body);
delete obj.serverConfig.httpdns;
delete obj.serverConfig.dnsvip;
delete obj.serverConfig.dnsvip_v6;
$done({ body: JSON.stringify(obj) });
}
if (url.indexOf(path3) != -1) {
let obj = JSON.parse(body);
let JDHttpToolKit = obj.data.JDHttpToolKit;
let jCommandConfig = obj.data.jCommandConfig;
if (JDHttpToolKit) {
delete obj.data.JDHttpToolKit.httpdns;
delete obj.data.JDHttpToolKit.dnsvipV6;
}
if (jCommandConfig) {
delete obj.data.jCommandConfig.httpdnsConfig;
}
$done({ body: JSON.stringify(obj) });
}
if (url.indexOf(path2) != -1 || url.indexOf(path4) != -1) {
if (!$tool.isQuanX) {
$done({ body });
}
let obj = JSON.parse(body);
const floors = obj.floors;
const commodity_info = floors[floors.length - 1];
const others = obj.others;
const domain = obj.domain;
const shareUrl =
url.indexOf(path4) != -1
? domain.h5Url
: url.indexOf(path2h) != -1
? others.property.shareUrl
: commodity_info.data.property.shareUrl;
autoScheme = url.indexOf(path2h) != -1 ? "openApp.jdHealth" : "openjd";
//const scheme = !autoChoose ? chooseScheme : url.indexOf(path4) != -1 ? "openapp.jdpingou" : url.indexOf(path2h) != -1 ? "openApp.jdHealth" : url.indexOf("lite_"+path3) != -1 ? "openjdlite" : "openjd";
let getHistory = request_history_price(shareUrl);
let convertURL = "";
let jxconvertURL = "";
if (useConvert) {
convertURL = convert(shareUrl);
jxconvertURL = url.indexOf(path4) != -1 ? convert(shareUrl, true) : undefined;
}
Promise.all([getHistory, convertURL, jxconvertURL])
.then((detail) => {
let msg = "";
if (detail[1] == "useJXOrigin") detail[1] = detail[2];
if (detail[0].lower_tip) {
msg += detail[0].lower_tip;
let convertmsg = detail[1].convertURL ? detail[1].msg : detail[1];
msg += convertmsg ? "\n" + convertmsg : "";
msg += "\n" + detail[0].historydetail;
} else {
let convertmsg = detail[1].convertURL ? detail[1].msg : detail[1];
msg += convertmsg ? convertmsg + "\n" : "";
msg += detail[0];
}
let oprnUrl = detail[1].convertURL ? detail[1].convertURL : "";
$tool.notify("", "", msg, oprnUrl);
})
.finally(() => {
$done({ body });
});
}
function lowerMsgs(data) {
const lower = data.lowerPriceyh;
const lowerDate = dateFormat(data.lowerDateyh);
const lowerMsg = "♻️ 历史最低到手价:¥" + String(lower) + ` (${lowerDate}) `;
return lowerMsg;
}
function priceSummary(data) {
let summary = "";
let listPriceDetail = data.PriceRemark.ListPriceDetail.slice(0, 4);
let list = listPriceDetail.concat(historySummary(data.single));
list.forEach((item, index) => {
if (item.Name == "双11价格") {
item.Name = "双十一价格";
} else if (item.Name == "618价格") {
item.Name = "六一八价格";
}
let price = String(parseInt(item.Price.substr(1)));
summary += `\n${item.Name} ${isNaN(price) ? "-" : "¥" + price} ${item.Date} ${
item.Difference
}`;
});
return summary;
}
function historySummary(single) {
const rexMatch = /\[.*?\]/g;
const rexExec = /\[(.*),(.*),"(.*)".*\]/;
let currentPrice, lowest30, lowest90, lowest180, lowest360;
let list = single.jiagequshiyh.match(rexMatch);
list = list.reverse().slice(0, 360);
list.forEach((item, index) => {
if (item.length > 0) {
const result = rexExec.exec(item);
const dateUTC = new Date(eval(result[1]));
const date = dateUTC.format("yyyy-MM-dd");
let price = parseFloat(result[2]);
if (index == 0) {
currentPrice = price;
lowest30 = {
Name: "三十天最低",
Price: `¥${String(price)}`,
Date: date,
Difference: difference(currentPrice, price),
price,
};
lowest90 = {
Name: "九十天最低",
Price: `¥${String(price)}`,
Date: date,
Difference: difference(currentPrice, price),
price,
};
lowest180 = {
Name: "一百八最低",
Price: `¥${String(price)}`,
Date: date,
Difference: difference(currentPrice, price),
price,
};
lowest360 = {
Name: "三百六最低",
Price: `¥${String(price)}`,
Date: date,
Difference: difference(currentPrice, price),
price,
};
}
if (index < 30 && price < lowest30.price) {
lowest30.price = price;
lowest30.Price = `¥${String(price)}`;
lowest30.Date = date;
lowest30.Difference = difference(currentPrice, price);
}
if (index < 90 && price < lowest90.price) {
lowest90.price = price;
lowest90.Price = `¥${String(price)}`;
lowest90.Date = date;
lowest90.Difference = difference(currentPrice, price);
}
if (index < 180 && price < lowest180.price) {
lowest180.price = price;
lowest180.Price = `¥${String(price)}`;
lowest180.Date = date;
lowest180.Difference = difference(currentPrice, price);
}
if (index < 360 && price < lowest360.price) {
lowest360.price = price;
lowest360.Price = `¥${String(price)}`;
lowest360.Date = date;
lowest360.Difference = difference(currentPrice, price);
}
}
});
return [lowest30, lowest90, lowest180];
}
function difference(currentPrice, price) {
let difference = sub(currentPrice, price);
if (difference == 0) {
return "-";
} else {
return `${difference > 0 ? "↑" : "↓"}${String(difference)}`;
}
}
function sub(arg1, arg2) {
return add(arg1, -Number(arg2), arguments[2]);
}
function add(arg1, arg2) {
(arg1 = arg1.toString()), (arg2 = arg2.toString());
var arg1Arr = arg1.split("."),
arg2Arr = arg2.split("."),
d1 = arg1Arr.length == 2 ? arg1Arr[1] : "",
d2 = arg2Arr.length == 2 ? arg2Arr[1] : "";
var maxLen = Math.max(d1.length, d2.length);
var m = Math.pow(10, maxLen);
var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
var d = arguments[2];
return typeof d === "number" ? Number(result.toFixed(d)) : result;
}
function request_history_price(share_url) {
return new Promise((resolve) => {
let options = {
url: "https://apapia-history.manmanbuy.com/ChromeWidgetServices/WidgetServices.ashx",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"User-Agent":
"Mozilla/5.0 (iPhone; CPU iPhone OS 13_1_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 - mmbWebBrowse - ios",
},
body: "methodName=getHistoryTrend&p_url=" + encodeURIComponent(share_url),
};
$tool.post(options, function (error, response, data) {
if (!error) {
data = JSON.parse(data);
if (consolelog) console.log("Data:\n" + data);
if (data.ok == 1 && data.single) {
const lower = lowerMsgs(data.single);
const detail = priceSummary(data);
const tip = data.PriceRemark.Tip;
let r = {};
r.lower_tip = `${lower} ${tip}`;
r.historydetail = `${detail.substr(1)}`;
resolve(r);
}
if (data.ok == 0 && data.msg.length > 0) {
let e = `⚠️ ${data.msg}`;
resolve(e);
}
} else {
if (consolelog) console.log("JD History Error:\n" + error);
resolve();
}
});
});
}
function convert(url, isOriginJXURL) {
return new Promise((resolve) => {
if (!cookie) {
$tool.setCache("false", "jfUseConvert");
resolve("");
} else {
let id;
if (
url.includes("item.m.jd") ||
url.includes("item.jd") ||
(!autoChoose && chooseScheme == "openApp.jdHealth")
) {
id = url.match(/(\d+)\.html/)[1];
url = `https:\/\/item.m.jd.com\/product\/${id}.html`;
} else if (url.includes("m.jingxi") || url.includes("wq.jd.com")) {
id = url.match(/sku=(\d+)/)[1];
url = `https:\/\/wq.jd.com\/item\/view?sku=${id}`;
if (isOriginJXURL || (!autoChoose && chooseScheme == "openjdlite"))
url = `https:\/\/m.jingxi.com\/item\/jxview?sku=${id}`;
autoScheme = "openapp.jdpingou";
} else if (url.includes("kpl.m.jd")) {
id = url.match(/wareId=(\d+)/)[1];
url = `https:\/\/kpl.m.jd.com\/product?wareId=${id}`;
autoScheme = jsapp ? "openjdlite" : "browser";
} else {
url = url;
//url = url.replace(/\//g, "\\/");
}
let body = {
funName: "getSuperClickUrl",
param: {
materialInfo: url,
ext1: "200|100_3|",
},
};
let t = +new Date();
let options = {
url: `https://api.m.jd.com/api?functionId=ConvertSuperLink&appid=u&_=${t}&body=${encodeURI(
JSON.stringify(body)
)}&loginType=2`,
headers: {
"Accept-Encoding": "gzip,compress,br,deflate",
Connection: "keep-alive",
Cookie: cookie,
Host: "api.m.jd.com",
Referer: "https://servicewechat.com/wxf463e50cd384beda/114/page-frame.html",
"User-Agent":
"Mozilla/5.0 (iPhone; CPU iPhone OS 13_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.4(0x18000429) NetType/WIFI Language/zh_CN",
"content-type": "application/json",
},
};
$tool.get(options, function (err, resp, data) {
if (!err) {
data = JSON.parse(data);
console.log("JD Convert Data:\n" + data);
if (data.code === 200) {
let r = {};
let scheme = autoChoose ? autoScheme : chooseScheme;
if (data.data.promotionUrl) {
r.msg = `💰返点比率:${data.data.wlCommissionShare} % 预计返利:¥ ${data.data.wlCommission}`;
r.convertURL =
scheme == "browser"
? chooseBrowser + data.data.promotionUrl
: `${scheme}://virtual?params=%7B%22category%22:%22jump%22,%22des%22:%22m%22,%22sourceValue%22:%22babel-act%22,%22sourceType%22:%22babel%22,%22url%22:%22${data.data.promotionUrl}%22%7D`;
} else {
r.msg = `该商品暂无详细返利信息,${data.data.formatContext.trim()}`;
r.convertURL =
scheme == "browser"
? chooseBrowser + data.data.originalContext
: `${scheme}://virtual?params=%7B%22category%22:%22jump%22,%22des%22:%22m%22,%22sourceValue%22:%22babel-act%22,%22sourceType%22:%22babel%22,%22url%22:%22${data.data.originalContext}%22%7D`;
}
resolve(r);
} else if (data.code === 105) {
if (autoScheme == "openapp.jdpingou") resolve("useJXOrigin");
else resolve("");
} else if (data.code === 430) {
$tool.setCache("false", "jfUseConvert");
resolve("");
} else {
resolve(JSON.stringify(data));
}
} else {
console.log("JD Convert Error:\n" + err);
resolve();
}
});
}
});
}
function dateFormat(cellval) {
const date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
const month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
const currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return date.getFullYear() + "-" + month + "-" + currentDate;
}
function tool() {
const isLoon = typeof $loon != "undefined";
const isSurge = typeof $httpClient != "undefined";
const isQuanX = typeof $task != "undefined";
const node = (() => {
if (typeof require == "function") {
const request = require("request");
return { request };
} else {
return null;
}
})();
const notify = (title, subtitle, content, open_url) => {
if (isSurge && !isLoon) {
let opts = {};
if (open_url) opts["url"] = open_url;
if (JSON.stringify(opts) == "{}") {
$notification.post(title, subtitle, content);
} else {
$notification.post(title, subtitle, content, opts);
}
}
if (isQuanX) {
let opts = {};
if (open_url) opts["open-url"] = open_url;
if (JSON.stringify(opts) == "{}") {
$notify(title, subtitle, content);
} else {
$notify(title, subtitle, content, opts);
}
}
if (isLoon) {
let opts = {};
if (open_url) opts["openUrl"] = open_url;
if (JSON.stringify(opts) == "{}") {
$notification.post(title, subtitle, content);
} else {
$notification.post(title, subtitle, content, opts);
}
}
if (node) {
let content_Node = content + (open_url == undefined ? "" : `\n\n跳转链接:${open_url}`);
console.log(`${title}\n${subtitle}\n${content_Node}\n\n`);
}
};
const setCache = (value, key) => {
if (isQuanX) return $prefs.setValueForKey(value, key);
if (isSurge) return $persistentStore.write(value, key);
};
const getCache = (key) => {
if (isQuanX) return $prefs.valueForKey(key);
if (isSurge) return $persistentStore.read(key);
};
const adapterStatus = (response) => {
if (response.status) {
response["statusCode"] = response.status;
} else if (response.statusCode) {
response["status"] = response.statusCode;
}
return response;
};
const get = (options, callback) => {
if (isQuanX) {
if (typeof options == "string") options = { url: options };
options["method"] = "GET";
$task.fetch(options).then(
(response) => {
callback(null, adapterStatus(response), response.body);
},
(reason) => callback(reason.error, null, null)
);
}
if (isSurge)
$httpClient.get(options, (error, response, body) => {
callback(error, adapterStatus(response), body);
});
if (node) {
node.request(options, (error, response, body) => {
callback(error, adapterStatus(response), body);
});
}
};
const post = (options, callback) => {
if (isQuanX) {
if (typeof options == "string") options = { url: options };
options["method"] = "POST";
$task.fetch(options).then(
(response) => {
callback(null, adapterStatus(response), response.body);
},
(reason) => callback(reason.error, null, null)
);
}
if (isSurge) {
$httpClient.post(options, (error, response, body) => {
callback(error, adapterStatus(response), body);
});
}
if (node) {
node.request.post(options, (error, response, body) => {
callback(error, adapterStatus(response), body);
});
}
};
return { isQuanX, isSurge, notify, setCache, getCache, get, post };
}
Array.prototype.insert = function (index, item) {
this.splice(index, 0, item);
};
Date.prototype.format = function (fmt) {
var o = {
"y+": this.getFullYear(),
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds(),
};
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
if (k == "y+") {
fmt = fmt.replace(RegExp.$1, ("" + o[k]).substr(4 - RegExp.$1.length));
} else if (k == "S+") {
var lens = RegExp.$1.length;
lens = lens == 1 ? 3 : lens;
fmt = fmt.replace(RegExp.$1, ("00" + o[k]).substr(("" + o[k]).length - 1, lens));
} else {
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
);
}
}
}
return fmt;
};