-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebank-adapterId-extension.js
More file actions
392 lines (385 loc) · 13.5 KB
/
debank-adapterId-extension.js
File metadata and controls
392 lines (385 loc) · 13.5 KB
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
// ==UserScript==
// @name Debank Pools AdapterId
// @namespace http://tampermonkey.net/
// @version 1.1.0
// @description 提供快捷的Debank池子分类
// @author cuukenn
// @match https://debank.com/protocols/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=greasyfork.org
// @require https://cdn.bootcdn.net/ajax/libs/jquery/3.2.1/jquery.min.js
// @charset UTF-8
// @license MIT License
// @grant unsafeWindow
// @grant GM_addStyle
// @grant GM_openInTab
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @grant GM_log
// ==/UserScript==
const debankAdapterExtension = (function () {
const util = (function () {
function findTargetElement(targetContainer) {
const body = window.document;
let tabContainer;
let tryTime = 0;
const maxTryTime = 120;
let startTimestamp;
return new Promise((resolve, reject) => {
function tryFindElement(timestamp) {
if (!startTimestamp) {
startTimestamp = timestamp;
}
const elapsedTime = timestamp - startTimestamp;
if (elapsedTime >= 500) {
GM_log(
"查找元素:" +
targetContainer +
",第" +
tryTime +
"次"
);
tabContainer = body.querySelector(targetContainer);
if (tabContainer) {
resolve(tabContainer);
} else if (++tryTime === maxTryTime) {
reject();
} else {
startTimestamp = timestamp;
}
}
if (!tabContainer && tryTime < maxTryTime) {
requestAnimationFrame(tryFindElement);
}
}
requestAnimationFrame(tryFindElement);
});
}
function syncRequest(option) {
return new Promise((resolve, reject) => {
option.onload = (res) => {
resolve(res);
};
option.onerror = (err) => {
reject(err);
};
GM_xmlhttpRequest(option);
});
}
return {
req: (option) => syncRequest(option),
findTargetEle: (targetEle) => findTargetElement(targetEle),
};
})();
const dataUtil = (function () {
const fetchData = (finalPools, id, start, limit, key) => {
return fetch(
`https://api.debank.com/protocol/pools?start=${start}&limit=${limit}&id=${id}&name=`,
{
headers: {
accept: "*/*",
"accept-language": "zh-CN,zh;q=0.9",
},
referrer: "https://debank.com/",
referrerPolicy: "strict-origin-when-cross-origin",
body: null,
method: "GET",
mode: "cors",
credentials: "omit",
}
)
.then((res) => res.json())
.then((res) => res.data.pools)
.then((data) => {
let newSize = 0;
for (let item of data) {
const typeName = item["name"];
let typeMap = finalPools.get(typeName);
if (typeMap == null) {
typeMap = new Map();
finalPools.set(typeName, typeMap);
}
const adapterId = item["adapter_id"];
let adapterSet = typeMap.get(adapterId);
if (adapterSet == null) {
adapterSet = new Set();
typeMap.set(adapterId, adapterSet);
}
adapterSet.add(item[key]);
newSize++;
}
return new Promise((resolve) =>
resolve({
finalPools,
id,
limit,
key,
start: start + newSize,
hasNext: newSize > 0,
})
);
});
};
const fetchAllData = (id, limit, key, dealTask) => {
const sleep = (task, time) => {
return new Promise((resolve) =>
setTimeout(() => resolve(task), time)
);
};
const sleepTask = (param) => {
sleep(
fetchData(
param.finalPools,
param.id,
param.start,
param.limit,
param.key
),
500 + Math.random() * 500
).then((res) => {
if (res.hasNext) {
return new Promise((resolve) =>
resolve(sleepTask(res))
);
} else {
dealTask(res.finalPools);
}
});
};
return sleepTask({
finalPools: new Map(),
id,
start: 0,
limit,
key,
});
};
const transform = (data) => {
const transformed = [];
let id = 1;
data.forEach((subData, type) => {
const child = [];
subData.forEach((v, adapterId) => {
const subChild = [];
for (let item of v) {
subChild.push({
id: id++,
city: item,
child: [],
});
}
child.push({
id: id++,
city: adapterId,
child: subChild,
});
});
transformed.push({
id: id++,
city: type,
child,
});
});
return transformed;
};
return {
fetchAllData,
transform,
};
})();
const domUtil = (function () {
const init = (container, config) => {
$(container).append(createContainer());
$("#expand").on("click", function (e) {
$("#tree").toggle("nomal", function () {
$(e.target).html() == "+ 展开"
? $(e.target).html("- 收缩")
: $(e.target).html("+ 展开");
});
});
$("#loadData").on("click", function (e) {
if (config.transformedPools.length == 0) {
$("#loadData").html("加载中...");
$("#loadData").attr("disabled", true);
dataUtil.fetchAllData(
config.protocol,
config.limit,
"id",
(res) => {
return new Promise((resolve) => {
resolve(res);
})
.then((data) => {
config.pools = res;
config.transformedPools =
dataUtil.transform(config.pools);
loadTree(config.transformedPools);
})
.finally(() => {
$("#loadData").html("加载数据");
$("#loadData").removeAttr("disabled");
});
}
);
} else {
loadTree(config.transformedPools);
}
});
};
const loadTree = (data) => {
$("#tree").html(createDataContainer(data));
$("#tree").on("click", function (e) {
var targetNode = $(e.target);
var nodeName = targetNode.get(0).tagName.toLowerCase();
if (nodeName == "div") {
if ($(e.target).children("span").html() == " + ") {
$(e.target).children("span").html(" - ");
} else {
$(e.target).children("span").html(" + ");
}
// 移除其他选中样式
if ($(".active").length > 0)
$(".active").toggleClass("active");
targetNode.toggleClass("active");
$(e.target).parent().children("ul").toggle();
onclickTreeItem($(e.target).data("id"));
}
});
};
const createContainer = () => {
return `
<div id="adapterId-extension">
<div>
<button id="expand">- 收缩</button>
<button id="loadData">加载数据</button>
</div>
<div id="tree" class="tree"></div>
</div>
`;
};
const createDataContainer = (data, num = 0) => {
const createTree = (data, num = 0) => {
var content = "<ul>";
data.forEach((item, index) => {
content +=
'<li><div class="ul-item" style=" padding-left:' +
25 * num +
'px" data-id="' +
(item.child.length > 0 ? null : item.id) +
' "><span ' +
(item.child.length > 0 ? "" : 'class="none"') +
"> + </span>" +
item.city +
"</div>";
if (item.child.length > 0)
content += createTree(item.child, num + 1);
content += "</li>";
});
content += "</ul>";
return content;
};
return createTree(data, num);
};
return {
init,
loadTree,
};
})();
class BaseConsumer {
#_domUtil;
#_config;
constructor(protocol, limit) {
this.#_domUtil = domUtil;
this.#_config = {
protocol,
limit,
pools: [],
transformedPools: [],
};
this.parse = () => {
util.findTargetEle("body").then((container) =>
this.generateElement(container)
);
};
}
generateElement(container) {
GM_addStyle(`
#tree {
width: fit-content;
height: 80%;
background-color: white;
border-radius: 3%;
border: 1px solid black;
}
#tree ul {
margin: 0;
padding: 0;
}
.tree ul li {
list-style-type: none;
}
.tree a {
cursor: pointer;
}
.tree span {
cursor: pointer;
}
.none {
display: none;
}
.active {
background-color: #ccc;
}
.ul-item {
cursor: pointer;
}
.ul-item:hover {
background-color: #ccc;
}
#adapterId-extension {
width: fit-content;
position: fixed;
top: 30%;
left: 1%;
z-index: 1000;
}
#adapterId-extension > div {
display: inline-block;
}
#adapterId-extension > div:nth-child(2) {
overflow-y: scroll;
max-height: 40vh;
}
#expand,
#loadData {
cursor: pointer;
margin-bottom: 10px;
display: block;
}
`);
this.#_domUtil.init(container, this.#_config);
this.#_domUtil.loadTree([]);
}
}
class DefaultConsumer extends BaseConsumer {
constructor(protocol, limit) {
super(protocol, limit);
}
}
return {
injectEnhance: () => {
const url = window.location.href,
prefix = "https://debank.com/protocols/";
if (url.startsWith(prefix)) {
const protocol = url.replace(prefix, "").split("/")[0];
GM_log(`mached protocolId: ${protocol}`);
new DefaultConsumer(protocol, 20).parse();
} else {
GM_log("no protocolId founded");
}
},
};
})();
(function () {
debankAdapterExtension.injectEnhance();
})();