-
Notifications
You must be signed in to change notification settings - Fork 0
/
getorderinfo.user.js
198 lines (157 loc) · 6.5 KB
/
getorderinfo.user.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
// ==UserScript==
// @name 淘宝订单导出
// @namespace http://tampermonkey.net/
// @version 0.7
// @description 淘宝网页版,已买到的宝贝页面会增加两个按。点击添加本页订单即可将订单添加到带保存的订单列表中,点击导出即可导出CSV文件。
// @author Yerik Yuan
// @include https://buyertrade.taobao*
// @require https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/lodash.js/4.17.21/lodash.min.js
// @grant none
// @license MIT
// ==/UserScript==
function addButton(element, onclickFunc, value = "按钮", width = "60px", height = "60px") {
const button = document.createElement("input");
button.type = "button";
button.value = value;
button.style.height = height;
button.style.width = width;
button.style.align = "center";
button.style.marginBottom = "10px";
button.style.marginLeft = "250px";
button.style.color = "white";
button.style.background = "#409EFF";
button.style.border = "1px solid #409EFF";
button.onclick = function () {
onclickFunc();
}
element.appendChild(button);
element.insertBefore(button, element.childNodes[0]);
}
const orderListPage = /(http|https):\/\/buyertrade\.taobao.*?\/trade/g;
if (orderListPage.exec(document.URL)) {
const orderListMain = document.getElementById("J_bought_main");
addButton(orderListMain, addCurrentPageOrdersToList, "添加本页订单", "160px");
addButton(orderListMain, exportOrders, "导出订单", "160px");
}
function toCsv(header, data, filename) {
let rows = "";
let row = header.join(",");
rows += row + "\n";
_.forEach(data, value => {
rows += _.replace(value.join(","), '#', '@') + "\n";
})
let blob = new Blob(["\ufeff" +rows],{type: 'text/csv;charset=utf-8;'});
let encodedUrl = URL.createObjectURL(blob);
let url = document.createElement("a");
url.setAttribute("href", encodedUrl);
url.setAttribute("download", filename + ".csv");
document.body.appendChild(url);
url.click();
}
let orderList = {}
let listlength = 0;
function addCurrentPageOrdersToList() {
const orders = document.getElementsByClassName("js-order-container");
for (let order of orders) {
let items = processOrder(order);
if (!items) {
continue;
}
_.forEach(items, (value, key) => {
orderList[key] = value;
})
}
if (listlength < Object.keys(orderList).length){
listlength = Object.keys(orderList).length
alert("本页订单已添加成功!");
}else{
alert("重复添加或添加失败!");
}
}
function exportOrders() {
const header = ["订单号", "下单日期", "店铺名称", "店铺链接", "商品明细", "商品链接", "所选规格", "单价", "数量", "运费", "实付款", "状态"];
toCsv(header, orderList, "订单信息")
}
function processOrder(order) {
let outputData = {};
let textContent = order.textContent;
let pattern = /(\d{4}-\d{2}-\d{2})订单号: ()/;
let isExist = pattern.exec(textContent);
if (!isExist) {
console.log('暂未发现订单!');
}else{
const date = isExist[1];
const id = order.querySelector("div[data-id]").getAttribute("data-id");
let index = 0;
var actualPay = 0;
var freight =0;
var specs = '无';
let storenamecouted = 0;
while (true) {
let count = 0;
let actualPay = "---";
let freight = "---";
let storename = '---';
let storeurl = '---';
let storeinfoQuery = order.querySelector("a[data-reactid='.0.7:$order-" + id + ".$" + id + ".0.1:0.0.1.0.1']");
if(storeinfoQuery != null && storenamecouted == 0){
storename = storeinfoQuery.textContent;
storeurl = storeinfoQuery.href;
storenamecouted =1;
}
let productQuery = order.querySelector("span[data-reactid='.0.7:$order-" + id + ".$" + id + ".0.1:1:0.$" + index + ".$0.0.1.0.0.1']");
let priceQuery = order.querySelector("span[data-reactid='.0.7:$order-" + id + ".$" + id + ".0.1:1:0.$" + index + ".$1.0.1.1']");
let countQuery = order.querySelector("p[data-reactid='.0.7:$order-" + id + ".$" + id + ".0.1:1:0.$" + index + ".$2.0.0']");
let actualPayQuery = order.querySelector("span[data-reactid='.0.7:$order-" + id + ".$" + id + ".0.1:1:0.$" + index + ".$4.0.0.2.0.1']");
/*let itemUrlQuery = order.querySelector("a[href]");*/
let itemUrlQuery = order.querySelector("a[data-reactid='.0.7:$order-" + id + ".$" + id + ".0.1:1:0.$"+ index + ".$0.0.1.0.0']");
let freightQuery = order.querySelector("span[data-reactid='.0.7:$order-" + id + ".$" + id + ".0.1:1:0.$"+ index + ".$4.0.1:$0.1']");
let specQuery = order.querySelector("p[data-reactid='.0.7:$order-" + id + ".$" + id + ".0.1:1:0.$"+ index + ".$0.0.1.1']");
if (productQuery === null) {
break;
}
if (productQuery.textContent.indexOf("保险服务") != -1){
index++;
continue;
}
if (productQuery.textContent.indexOf("增值服务") != -1){
index++;
continue;
}
if (specQuery != null){
specs = specQuery.textContent;
}
let price = priceQuery.textContent;
if (countQuery != null){
count = countQuery.textContent;
}
if (freightQuery != null){
freight = freightQuery.textContent.replace("¥","");
}
if (actualPayQuery != null) {
actualPay = actualPayQuery.textContent;
}
if (index === 0) {
let statusQuery = order.querySelector("span[data-reactid='.0.7:$order-" + id + ".$" + id + ".0.1:1:0.$" + index + ".$5.0.0.0']");
var status = statusQuery.textContent;
}
let itemUrl = itemUrlQuery.href
index++;
outputData[id + index] = [
id,
date,
storename,
storeurl,
productQuery.textContent.replace(/,/g,","),
itemUrl,
specs,
parseFloat(price),
count,
freight,
actualPay,
status,
]
}
}
return outputData;
}