-
Notifications
You must be signed in to change notification settings - Fork 481
/
tb_price_lite.js
402 lines (384 loc) · 14.7 KB
/
tb_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
/*
README:https://github.com/yichahucha/surge/tree/master
*/
const $tool = new Tool()
const consoleLog = false
const url = $request.url
const path1 = "/amdc/mobileDispatch"
const path2 = "/gw/mtop.taobao.detail.getdetail"
if (url.indexOf(path1) != -1) {
if ($tool.isResponse) {
const $base64 = new Base64()
let body = $response.body
let obj = JSON.parse($base64.decode(body))
let dns = obj.dns
if (dns && dns.length > 0) {
let i = dns.length;
while (i--) {
const element = dns[i];
let host = "trade-acs.m.taobao.com"
if (element.host == host) {
element.ips = []
if (consoleLog) console.log(JSON.stringify(element))
}
}
}
body = $base64.encode(JSON.stringify(obj))
$done({ body })
} else {
let headers = $request.headers
let body = $request.body
if (headers["User-Agent"].indexOf("%E6%89%8B%E6%9C%BA%E6%B7%98%E5%AE%9D") != -1) {
let json = Qs2Json(body)
let domain = json.domain.split(" ")
let i = domain.length;
while (i--) {
const block = "trade-acs.m.taobao.com"
const element = domain[i];
if (element == block) {
domain.splice(i, 1);
}
}
json.domain = domain.join(" ")
body = Json2Qs(json)
}
$done({ body })
}
}
if (url.indexOf(path2) != -1) {
const body = $response.body
$done({ body })
const obj = JSON.parse(body)
let item = obj.data.item
let shareUrl = `https://item.taobao.com/item.htm?id=${item.itemId}`
requestPrice(shareUrl, function (data) {
if (data) {
if (data.ok == 1 && data.single) {
const lower = lowerMsgs(data.single)
const detail = priceSummary(data)
const tip = data.PriceRemark.Tip
$tool.notify("", "", `${lower}\n${tip}${detail}`)
}
if (data.ok == 0 && data.msg.length > 0) {
$tool.notify("", "", `⚠️ ${data.msg}`)
}
}
})
}
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 requestPrice(share_url, callback) {
const 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) {
callback(JSON.parse(data));
if (consoleLog) console.log("Data:\n" + data);
} else {
callback(null, null);
if (consoleLog) console.log("Error:\n" + error);
}
})
}
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 Qs2Json(url) {
var search = url.substring(url.lastIndexOf("?") + 1);
var obj = {};
var reg = /([^?&=]+)=([^?&=]*)/g;
search.replace(reg, function (rs, $1, $2) {
var name = decodeURIComponent($1);
var val = decodeURIComponent($2);
val = String(val);
obj[name] = val;
return rs;
});
return obj;
}
function Json2Qs(json) {
var temp = [];
for (var k in json) {
temp.push(k + "=" + json[k]);
}
return temp.join("&");
}
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;
}
function Tool() {
_node = (() => {
if (typeof require == "function") {
const request = require('request')
return ({ request })
} else {
return (null)
}
})()
_isSurge = typeof $httpClient != "undefined"
_isQuanX = typeof $task != "undefined"
this.isSurge = _isSurge
this.isQuanX = _isQuanX
this.isResponse = typeof $response != "undefined"
this.notify = (title, subtitle, message) => {
if (_isQuanX) $notify(title, subtitle, message)
if (_isSurge) $notification.post(title, subtitle, message)
if (_node) console.log(JSON.stringify({ title, subtitle, message }));
}
this.write = (value, key) => {
if (_isQuanX) return $prefs.setValueForKey(value, key)
if (_isSurge) return $persistentStore.write(value, key)
}
this.read = (key) => {
if (_isQuanX) return $prefs.valueForKey(key)
if (_isSurge) return $persistentStore.read(key)
}
this.get = (options, callback) => {
if (_isQuanX) {
if (typeof options == "string") options = { url: options }
options["method"] = "GET"
$task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null))
}
if (_isSurge) $httpClient.get(options, (error, response, body) => { callback(error, _status(response), body) })
if (_node) _node.request(options, (error, response, body) => { callback(error, _status(response), body) })
}
this.post = (options, callback) => {
if (_isQuanX) {
if (typeof options == "string") options = { url: options }
options["method"] = "POST"
$task.fetch(options).then(response => { callback(null, _status(response), response.body) }, reason => callback(reason.error, null, null))
}
if (_isSurge) $httpClient.post(options, (error, response, body) => { callback(error, _status(response), body) })
if (_node) _node.request.post(options, (error, response, body) => { callback(error, _status(response), body) })
}
_status = (response) => {
if (response) {
if (response.status) {
response["statusCode"] = response.status
} else if (response.statusCode) {
response["status"] = response.statusCode
}
}
return response
}
}
function Base64() {
// private property
_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
// public method for encoding
this.encode = function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = _utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
}
return output;
}
// public method for decoding
this.decode = function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = _keyStr.indexOf(input.charAt(i++));
enc2 = _keyStr.indexOf(input.charAt(i++));
enc3 = _keyStr.indexOf(input.charAt(i++));
enc4 = _keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = _utf8_decode(output);
return output;
}
// private method for UTF-8 encoding
_utf8_encode = function (string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
// private method for UTF-8 decoding
_utf8_decode = function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}