forked from f2404/torn-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_lists.user.js
More file actions
399 lines (349 loc) · 17 KB
/
sort_lists.user.js
File metadata and controls
399 lines (349 loc) · 17 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
393
394
395
396
397
398
399
// ==UserScript==
// @name Torn: Sort lists
// @namespace lugburz.sort_lists
// @version 0.5.12
// @description Sort lists (such as blacklist, friendlist, userlist, faction members, company employees, stocks) by various columns.
// @author Lugburz
// @match https://www.torn.com/blacklist.php*
// @match https://www.torn.com/friendlist.php*
// @match https://www.torn.com/userlist.php*
// @match https://www.torn.com/page.php*
// @match https://www.torn.com/factions.php*
// @match https://www.torn.com/stockexchange.php*
// @match https://www.torn.com/companies.php*
// @match https://www.torn.com/joblist.php*
// @require https://raw.githubusercontent.com/f2404/torn-userscripts/31f4faa6da771b7a16cf732c1a78970506effeeb/lib/lugburz_lib.js
// @updateURL https://github.com/f2404/torn-userscripts/raw/master/sort_lists.user.js
// @grant GM_addStyle
// ==/UserScript==
GM_addStyle(`
.headerSortable {
cursor: pointer;
}
.headerSortDown:before {
content: " ▾" !important;
}
.headerSortUp:before {
content: " ▴" !important;
}`);
function compare (aText, bText, asc) {
// Returning -1 will place element `a` before element `b`
if (aText < bText) {
if (asc) return -1;
else return 1;
}
// Returning 1 will do the opposite
if (aText > bText) {
if (asc) return 1;
else return -1;
}
// Returning 0 leaves them as-is
return 0;
}
function doSort(items, column, ascending, divPrefix = '.title-black > .') {
if ('level'.localeCompare(column) == 0 || 'lvl'.localeCompare(column) == 0 || 'days'.localeCompare(column) == 0) {
let sortedByLevel = Array.prototype.sort.bind(items);
sortedByLevel(function (a, b) {
let aText = $(a).find('.'+column).text().match(/(\d+)/)[0];
let bText = $(b).find('.'+column).text().match(/(\d+)/)[0];
return compare(Number(aText), Number(bText), ascending);
});
} else if ('title'.localeCompare(column) == 0 || 'desk'.localeCompare(column) == 0 ||
'name'.localeCompare(column) == 0 || 'employee'.localeCompare(column) == 0 || 'member'.localeCompare(column) == 0) {
let sortedByName = Array.prototype.sort.bind(items);
sortedByName(function (a, b) {
// works with honors enabled or disabled
let aText = $(a).find('.name').find('img').attr('title') || $(a).find('.name:first').text();
let bText = $(b).find('.name').find('img').attr('title') || $(b).find('.name:first').text();
if (typeof aText !== 'undefined') aText = aText.trim().toLowerCase();
if (typeof bText !== 'undefined') bText = bText.trim().toLowerCase();
return compare(aText, bText, ascending);
});
} else if ('status'.localeCompare(column) == 0 || 'position'.localeCompare(column) == 0) {
let sortedByStatus = Array.prototype.sort.bind(items);
sortedByStatus(function (a, b) {
let aText = $(a).find('.'+column).text().replace('Status:', '').trim();
let bText = $(b).find('.'+column).text().replace('Status:', '').trim();
return compare(aText, bText, ascending);
});
// 'Last Action' script support
} else if ('member-icons'.localeCompare(column) == 0) {
// Do not show arrows if no 'Last Action'
if (!$(divPrefix+column).text() || !$(divPrefix+column).text().includes('Last Action'))
return items;
let sortedByLastAction = Array.prototype.sort.bind(items);
sortedByLastAction(function (a, b) {
let aText = $(a).find('.last-action').text().trim();
let bText = $(b).find('.last-action').text().trim();
let days = aText.match(/((\d+) day)?/)[2] || 0;
let hours = aText.match(/((\d+) hour)?/)[2] || 0;
let mins = aText.match(/((\d+) minute)?/)[2] || 0;
let aMins = Number(days)*24*60 + Number(hours)*60 + Number(mins);
days = bText.match(/((\d+) day)?/)[2] || 0;
hours = bText.match(/((\d+) hour)?/)[2] || 0;
mins = bText.match(/((\d+) minute)?/)[2] || 0;
let bMins = Number(days)*24*60 + Number(hours)*60 + Number(mins);
return compare(Number(aMins), Number(bMins), ascending);
});
} else if ('price'.localeCompare(column) == 0 || 'owned'.localeCompare(column) == 0 || 'change'.localeCompare(column) == 0) {
let sortedByPrice = Array.prototype.sort.bind(items);
sortedByPrice(function (a, b) {
let aText = $(a).find('.'+column).text().match(/((\d+\,)?\d+(\.\d+)?)/)[0].replace(',', '');
let bText = $(b).find('.'+column).text().match(/((\d+\,)?\d+(\.\d+)?)/)[0].replace(',', '');
// 'down' means the number is negative
if ('change'.localeCompare(column) == 0) {
if ($(a).find('.'+column).hasClass('down')) aText = -1 * Number(aText);
if ($(b).find('.'+column).hasClass('down')) bText = -1 * Number(bText);
}
return compare(Number(aText), Number(bText), ascending);
});
} else if ('rank'.localeCompare(column) == 0) {
let sortedByStatus = Array.prototype.sort.bind(items);
sortedByStatus(function (a, b) {
let aText = $(a).find('.'+column).text().replace('Rank:', '').trim();
let bText = $(b).find('.'+column).text().replace('Rank:', '').trim();
return compare(aText, bText, ascending);
});
} else if ('effectiveness'.localeCompare(column) == 0) {
let sortedByStatus = Array.prototype.sort.bind(items);
sortedByStatus(function (a, b) {
let aText = $(a).find('.'+column).attr('data-effectiveness');
let bText = $(b).find('.'+column).attr('data-effectiveness');
return compare(aText, bText, ascending);
});
} else {
// shouldn't happen
return items;
}
[ 'level', 'lvl', 'title', 'desk', 'name', 'days', 'status', 'member-icons', 'price', 'owned', 'change', 'member', 'position', 'employee', 'rank' ].forEach((elem) => {
$(divPrefix+elem).removeClass('headerSortUp');
$(divPrefix+elem).removeClass('headerSortDown');
});
if (ascending) {
$(divPrefix+column).addClass('headerSortDown');
} else {
$(divPrefix+column).addClass('headerSortUp');
}
return items;
}
// Blacklist, friendlist, userlist
function addUserlistSort() {
let ascending = true;
let last_sort = '';
// for friendlist and blacklist
let user_list = $('ul.user-info-blacklist-wrap');
let users = $(user_list).children('li');
// for userlist (search results)
if (users.length == 0) {
user_list = $('ul.user-info-list-wrap');
users = $(user_list).children('li');
}
let columns = ['title', 'level', 'status'].forEach((column) => {
$('div.title-black > div.'+column).addClass('headerSortable');
$('div.'+column).on('click', function() {
// discard old data
if(!$(users).is(':visible')) {
return;
}
if (column != last_sort) ascending = true;
last_sort = column;
users = doSort(users, column, ascending);
ascending = !ascending;
$(user_list).append(users);
});
});
}
// Faction members
function addMemberlistSort() {
let user_list = $('ul.table-body');
let users = $(user_list).children('li');
let ascending = true;
let last_sort = '';
let columns = ['member', 'lvl', 'position', 'days', 'status', 'member-icons'].forEach((column) => {
$('ul.table-header > li.'+column).addClass('headerSortable');
$('ul.table-header > li.'+column).on('click', function() {
if (column != last_sort) ascending = true;
last_sort = column;
users = doSort(users, column, ascending, 'ul.table-header > li.');
ascending = !ascending;
$(user_list).append(users);
});
});
}
// Faction control (money & points)
function doFactionDepSort(items, column, ascending) {
if ('name-money'.localeCompare(column) == 0 || 'name-points'.localeCompare(column) == 0) {
let sortedByName = Array.prototype.sort.bind(items);
sortedByName(function (a, b) {
// avoid conflicts with other scripts
if ($(a).find('.name:first').hasClass('btFaction') || $(b).find('.name:first').hasClass('btFaction')) return 0;
if ($(a).attr('id') == 'surplusInfo' || $(a).attr('id') == 'surplusInfo1') return 0;
if ($(b).attr('id') == 'surplusInfo' || $(b).attr('id') == 'surplusInfo1') return 0;
// works with honors enabled or disabled
let aText = $(a).find('.name').find('img').attr('title') || $(a).find('.name:first').text();
let bText = $(b).find('.name').find('img').attr('title') || $(b).find('.name:first').text();
if (typeof aText !== 'undefined') aText = aText.trim().toLowerCase();
if (typeof bText !== 'undefined') bText = bText.trim().toLowerCase();
return compare(aText, bText, ascending);
});
} else if ('money'.localeCompare(column) == 0 || 'points'.localeCompare(column) == 0) {
let sortedByPrice = Array.prototype.sort.bind(items);
sortedByPrice(function (a, b) {
let aText = $(a).find('.'+column).attr('data-value');
let bText = $(b).find('.'+column).attr('data-value');
return compare(Number(aText), Number(bText), ascending);
});
}
if (column.includes('money')) {
$('div.money-wrap > div.userlist-wrapper > div.info > span.bold').removeClass('headerSortUp');
$('div.money-wrap > div.userlist-wrapper > div.info > span.bold').removeClass('headerSortDown');
if (ascending) {
if ('name-money'.localeCompare(column) == 0) $('div.money-wrap > div.userlist-wrapper > div.info').find('span.bold:first').addClass('headerSortDown');
else if ('money'.localeCompare(column) == 0) $('div.money-wrap > div.userlist-wrapper > div.info').find('span.bold.amount:first').addClass('headerSortDown');
} else {
if ('name-money'.localeCompare(column) == 0) $('div.money-wrap > div.userlist-wrapper > div.info').find('span.bold:first').addClass('headerSortUp');
else if ('money'.localeCompare(column) == 0) $('div.money-wrap > div.userlist-wrapper > div.info').find('span.bold.amount:first').addClass('headerSortUp');
}
} else if (column.includes('points')) {
$('div.point-wrap > div.userlist-wrapper > div.info > span.bold').removeClass('headerSortUp');
$('div.point-wrap > div.userlist-wrapper > div.info > span.bold').removeClass('headerSortDown');
if (ascending) {
if ('name-points'.localeCompare(column) == 0) $('div.point-wrap > div.userlist-wrapper > div.info').find('span.bold:first').addClass('headerSortDown');
else if ('points'.localeCompare(column) == 0) $('div.point-wrap > div.userlist-wrapper > div.info').find('span.bold.amount:first').addClass('headerSortDown');
} else {
if ('name-points'.localeCompare(column) == 0) $('div.point-wrap > div.userlist-wrapper > div.info').find('span.bold:first').addClass('headerSortUp');
else if ('points'.localeCompare(column) == 0) $('div.point-wrap > div.userlist-wrapper > div.info').find('span.bold.amount:first').addClass('headerSortUp');
}
}
return items;
}
function addFactionMoneyDepSort() {
let user_list = $('ul.user-info-list-wrap.money-depositors');
let users = $(user_list).children('li.depositor');
let ascending = true;
let last_sort = '';
$('div.money-wrap > div.userlist-wrapper > div.info > span.bold').addClass('headerSortable');
$('div.money-wrap > div.userlist-wrapper > div.info').find('span.bold:first').on('click', function() {
if ('name-money' != last_sort) ascending = true;
last_sort = 'name-money';
users = doFactionDepSort(users, 'name-money', ascending);
ascending = !ascending;
$(user_list).append(users);
});
$('div.money-wrap > div.userlist-wrapper > div.info').find('span.bold.amount:first').on('click', function() {
if ('money' != last_sort) ascending = true;
last_sort = 'money';
users = doFactionDepSort(users, 'money', ascending);
ascending = !ascending;
$(user_list).append(users);
});
}
function addFactionPointsDepSort() {
let user_list = $('ul.user-info-list-wrap.points-depositors');
let users = $(user_list).children('li.depositor');
let ascending = true;
let last_sort = '';
$('div.point-wrap > div.userlist-wrapper > div.info > span.bold').addClass('headerSortable');
$('div.point-wrap > div.userlist-wrapper > div.info').find('span.bold:first').on('click', function() {
if ('name-points' != last_sort) ascending = true;
last_sort = 'name-points';
users = doFactionDepSort(users, 'name-points', ascending);
ascending = !ascending;
$(user_list).append(users);
});
$('div.point-wrap > div.userlist-wrapper > div.info').find('span.bold.amount:first').on('click', function() {
if ('points' != last_sort) ascending = true;
last_sort = 'points';
users = doFactionDepSort(users, 'points', ascending);
ascending = !ascending;
$(user_list).append(users);
});
}
// ===
// Stocks
function addStocklistSort() {
let stock_list = $('ul.stock-list');
let stocks = $(stock_list).children('li');
let ascending = true;
let last_sort = '';
let columns = ['name', 'price', 'change', 'owned'].forEach((column) => {
$('div.title-black > ul.title > li.'+column).addClass('headerSortable');
$('div.title-black > ul.title > li.'+column).on('click', function() {
if (column != last_sort) ascending = true;
last_sort = column;
stocks = doSort(stocks, column, ascending, 'div.title-black > ul.title > li.');
ascending = !ascending;
$(stock_list).append(stocks);
});
});
}
// Company (your company)
function addCompanylistSort() {
let user_list = $('ul.employee-list');
let users = $(user_list).children('li');
let ascending = true;
let last_sort = '';
let columns = ['employee', 'days', 'effectiveness'].forEach((column) => {
$('ul.employee-list-title > li.'+column).addClass('headerSortable');
$('ul.employee-list-title > li.'+column).on('click', function() {
if (column != last_sort) ascending = true;
last_sort = column;
users = doSort(users, column, ascending);
ascending = !ascending;
$(user_list).append(users);
});
});
}
// Joblist (someone else's company)
function addJoblistSort() {
let user_list = $('ul.employees-list');
let users = $(user_list).children('li');
let ascending = true;
let last_sort = '';
let columns = ['employee', 'rank', 'lvl'].forEach((column) => {
$('div.title-black > ul.title > li.'+column).addClass('headerSortable');
$('div.title-black > ul.title > li.'+column).on('click', function() {
if (column != last_sort) ascending = true;
last_sort = column;
users = doSort(users, column, ascending, 'div.title-black > ul.title > li.');
ascending = !ascending;
$(user_list).append(users);
});
});
}
(function() {
'use strict';
// Your code here...
ajax((page) => {
if (page == "userlist") {
$('ul.user-info-blacklist-wrap').ready(addUserlistSort);
} else if (page == "page") {
$('ul.user-info-list-wrap').ready(addUserlistSort);
} else if (page == "factions") {
$('ul.member-list').ready(addMemberlistSort);
if ($(location).attr('href').includes('tab=controls')) {
$('ul.user-info-list-wrap.money-depositors').ready(addFactionMoneyDepSort);
$('ul.user-info-list-wrap.points-depositors').ready(addFactionPointsDepSort);
}
} else if (page == "stockexchange") {
$('ul.stock-list').ready(addStocklistSort);
} else if (page == "companies") {
$('ul.employee-list').ready(addCompanylistSort);
} else if (page == "joblist") {
$('ul.employees-list').ready(addJoblistSort);
}
});
if ($(location).attr('href').includes('factions.php')) {
$('ul.member-list').ready(addMemberlistSort);
if ($(location).attr('href').includes('tab=controls')) {
$('ul.user-info-list-wrap.money-depositors').ready(addFactionMoneyDepSort);
$('ul.user-info-list-wrap.points-depositors').ready(addFactionPointsDepSort);
}
} else if ($(location).attr('href').includes('stockexchange.php')) {
$('ul.stock-list').ready(addStocklistSort);
} else if ($(location).attr('href').includes('companies.php')) {
$('ul.employee-list').ready(addCompanylistSort);
} else if ($(location).attr('href').includes('joblist.php')) {
$('ul.employees-list').ready(addCompanylistSort);
}
})();