-
Notifications
You must be signed in to change notification settings - Fork 0
/
Steam求购单导出导入.js
251 lines (197 loc) · 8.55 KB
/
Steam求购单导出导入.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
// ==UserScript==
// @name Steam求购单导出工具
// @namespace http://tampermonkey.net/
// @version 2024-08-05
// @description Steam求购单导出工具,使用时需要关闭会操作求购单dom的工具,如:Steam价格转换
// @author You
// @match https://steamcommunity.com/market/*
// @match https://www.baidu.com/
// @icon https://www.google.com/s2/favicons?sz=64&domain=steamcommunity.com
// @grant GM_notification
// @grant GM.getResourceText
// @grant GM.xmlHttpRequest
// @grant GM.openInTab
// @grant GM.addValueChangeListener
// @grant GM.getTab
// @resource myTxt1 file:///D:/data1.csv
// @resource myTxt file:///D:/data.csv
// ==/UserScript==
(function() {
'use strict';
function convertToUnicode(str) {
let unicodeStr = '';
for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i);
unicodeStr += "\\u" + charCode.toString(16).padStart(4, '0');
}
return unicodeStr;
}
async function initBuyMarket(){
if( localStorage.getItem('o')){
let item = JSON.parse( localStorage.getItem('o'));
Market_ShowBuyOrderPopup( 730, item[0],item[1] );
await new Promise(resolve => setTimeout(resolve, 3 *1000));
// 在新标签页的 DOM 上进行操作
document.getElementById('market_buy_commodity_input_price').value=item[2];
document.getElementById('market_buy_commodity_input_quantity').value=item[3];
document.getElementById('market_buyorder_dialog_accept_ssa').click();
document.getElementById("market_buyorder_dialog_purchase").click()//提交
// await new Promise(resolve => setTimeout(resolve, 5 *1000));
function closePage(){
if(document.getElementById('market_buy_commodity_status').innerText ||document.getElementById('market_buyorder_dialog_error_text').innerText){
localStorage.removeItem('o');
localStorage.setItem('k','go');
}
}
setTimeout(closePage,500)
}
}
initBuyMarket();
if(! document.querySelectorAll('.market_home_listing_table')[1].querySelector('.my_market_header')){
return false;
}
// 在页面上添加一个按钮
var button = document.createElement("button");
button.id = "exportButton";
button.textContent = "导出";
button.style.padding = "5px 10px";
button.style.margin = " 0 5px 0 0 ";
button.style.backgroundColor = "#4CAF50";
button.style.color = "white";
button.style.border = "none";
button.style.borderRadius = "4px";
button.style.cursor = "pointer";
document.querySelectorAll('.market_home_listing_table')[1].querySelector('.my_market_header').appendChild(button);
var button2 = document.createElement("button");
button2.id = "exportButto2n";
button2.textContent = "导入";
button2.style.padding = "5px 10px";
button2.style.margin = " 0 5px 0 0 ";
button2.style.backgroundColor = "#4CAF50";
button2.style.color = "white";
button2.style.border = "none";
button2.style.borderRadius = "4px";
button2.style.cursor = "pointer";
document.querySelectorAll('.market_home_listing_table')[1].querySelector('.my_market_header').appendChild(button2);
var button3 = document.createElement("button");
button3.id = "exportButton3";
button3.textContent = "清空缓存";
button3.style.padding = "5px 10px";
button3.style.backgroundColor = "#4CAF50";
button3.style.color = "white";
button3.style.border = "none";
button3.style.borderRadius = "4px";
button3.style.cursor = "pointer";
document.querySelectorAll('.market_home_listing_table')[1].querySelector('.my_market_header').appendChild(button3);
// 绑定按钮点击事件
document.getElementById("exportButton").addEventListener("click", exportTableToCSV);
document.getElementById("exportButto2n").addEventListener("click", uploadfile);
document.getElementById("exportButton3").addEventListener("click", clearCarce);
function clearCarce(){
localStorage.removeItem('o');
localStorage.removeItem('k');
}
function exportTableToCSV(){
let mybuyorder = document.querySelectorAll('.market_home_listing_table')[1]
let allRows = mybuyorder.querySelectorAll('.market_recent_listing_row')
let data = [];
for(var i = 0 ; i<allRows.length; i++){
let name = allRows[i].querySelector('.market_listing_item_name_link').href.replace('https://steamcommunity.com/market/listings/730/','');
let zh_name = allRows[i].querySelector('.market_listing_item_name_link').innerText;
let my_price = allRows[i].querySelector('.market_listing_my_price').innerText;
let num = allRows[i].querySelector('.market_listing_buyorder_qty').innerText;
data.push([decodeURI(name),zh_name,my_price,num])
}
// 将数据转换为CSV格式
var csvContent = "data:text/csv;charset=utf-8,";
data.forEach(function(rowArray) {
var row = rowArray.join(",");
csvContent += row + "\n";
});
// 创建一个下载链接并触发下载
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "table_data.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
GM_notification({
text: "导出成功,请在下载文件中查看",
title: "提示",
timeout: 3000, // 消失时间, 单位毫秒
onClick: function() {
console.log("用户点击了提示");
}
});
}
function convertCSVtoArray(csvData) {
var lines = csvData.trim().split("\n");
var result = [];
for (var i = 0; i < lines.length; i++) {
var cells = lines[i].split(",");
result.push(cells);
}
return result;
}
function uploadfile(){
// 读取 CSV 文件
GM.getResourceText("myTxt1")
.then(async csvData => {
let arr = convertCSVtoArray(csvData);
console.log("CSV data:", arr);
let curIndex = 0;
let tableInstent = null;
function openNTab(){
let item = arr[curIndex];
console.log('当前饰品:',item)
let newTabUrl = 'https://steamcommunity.com/market/listings/730/'+encodeURI(item[0])
localStorage.setItem('o',JSON.stringify(item))
tableInstent = GM.openInTab(newTabUrl,false);
curIndex +=1;
}
function checkLocalStorageValue() {
if (localStorage.getItem('k') === 'go') {
if(tableInstent){
tableInstent.then(r=>{
console.log(r)
tableInstent = null
r.close()
})
}else{
localStorage.removeItem('k');
openNTab();
}
} else {
console.log('Value of "k" is not "go"');
}
let checkLocalStorageValue_TIME = setTimeout(checkLocalStorageValue, 3000);
if(arr.length === curIndex){
console.log('清除checkLocalStorageValue')
clearTimeout(checkLocalStorageValue_TIME);
if(tableInstent){
tableInstent.then(r=>{
console.log(r)
tableInstent = null
r.close()
})
}
GM_notification({
text: "订单已导入完成,可能有部分因为网络问题导入失败,请再次核对",
title: "提示",
timeout: 10 * 1000, // 消失时间, 单位毫秒
onClick: function() {
console.log("用户点击了提示");
}
});
}
}
openNTab();
// 启动函数
checkLocalStorageValue();
})
.catch(error => {
console.error("Error reading CSV:", error);
});
}
})();