-
Notifications
You must be signed in to change notification settings - Fork 3
/
hkg-lm-finder.user.js
385 lines (299 loc) · 10.3 KB
/
hkg-lm-finder.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
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
// ==UserScript==
// @name HKG LM finder
// @namespace http://github.com/Xelio/
// @version 6.1.1
// @description HKG LM finder
// @downloadURL https://github.com/Xelio/hkg-lm-finder/raw/master/hkg-lm-finder.user.js
// @include http://forum*.hkgolden.com/profilepage.aspx?userid=*
// @include http://profile.hkgolden.com/profilepage.aspx?userid=*
// @match http://*.hkgolden.com/profilepage.aspx?userid=*
// @require http://code.jquery.com/jquery-1.9.1.min.js
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @copyright 2015, Xelio, Peach(Fix of PM, 紅人榜)
// ==/UserScript==
/*
HKG LM finder (HKGolden LM finder)
Copyright (C) 2013 Xelio Cheong
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var $j = jQuery.noConflict();
var servers;
var selectedServer;
var ajaxTimeout = 30000;
var ajaxRequest;
var ajaxRequestTimer;
var changePage;
var changeFilterType;
var MAX_RETRY = 3;
var retryCounter = 0;
var needLogin = false;
var HISTORY_ID = 'ctl00_ContentPlaceHolder1_mp3';
var HISTORY_SELECTOR = 'span#' + HISTORY_ID;
var GetHistory = function(element) { return element.find(HISTORY_SELECTOR).parent().parent().parent(); };
var NEXT_BUTTON_SELECTOR = '#ctl00_ContentPlaceHolder1_nextBtn';
var PREVIOUS_BUTTON_SELECTOR = '#ctl00_ContentPlaceHolder1_previousBtn';
var GO_BUTTON_SELECTOR = '#ctl00_ContentPlaceHolder1_pageGoBtn';
var FILTER_TYPE_SELECTOR = '#ctl00_ContentPlaceHolder1_filter_type';
var FILTER_YEAR_SELECTOR = '#ctl00_ContentPlaceHolder1_ddl_filter_year';
var PAGE_NO_TEXTBOX_SELECTOR = '#ctl00_ContentPlaceHolder1_pageTextBox';
var RED_PEOPLE_POUND_SELECTOR = '#ctl00_ContentPlaceHolder1_mp4';
var WEB_PROXY_URL = 'http://hkg-lm-loader-1.xelio.eu.org/proxy/';
var GetRequestURL = function(page, filter_type) {
return WEB_PROXY_URL
+ selectedServer + '.hkgolden.com/ProfilePage.aspx?userid=' + window.location.href.match(/userid=(\d+)/)[1]
+ '&type=history&page=' + (page || 1) + '&yearFilter=3&filterType=' + (filter_type || 'all');
}
// Monitor window.LM_CHANGE_PAGE and window.LM_CHANGE_FILTER_TYPE and fire
// ajax request to change page or filter.
// Use this monitoring method because page cannot directly call function in
// Greasemonkey script context.
pageChangeByAjax = function() {
if((window.LM_CHANGE_PAGE && window.LM_CHANGE_PAGE !== window.LM_CURRENT_PAGE)
||(window.LM_CHANGE_FILTER_TYPE && window.LM_CHANGE_FILTER_TYPE !== window.LM_FILTER_TYPE)) {
if(window.LM_CHANGE_PAGE) changePage = window.LM_CHANGE_PAGE;
window.LM_CHANGE_PAGE = null;
if(window.LM_CHANGE_FILTER_TYPE) changeFilterType = window.LM_CHANGE_FILTER_TYPE;
window.LM_CHANGE_FILTER_TYPE = null;
var history = $j('div#lm_history');
history.find('#lm_btn_Next').attr("disabled", true);
history.find('#lm_btn_Previous').attr("disabled", true);
history.find('#lm_btn_GoPageNo').attr("disabled", true);
history.find('#lm_filter_type').attr("disabled", true);
requestProfilePage(changePage, changeFilterType);
} else {
setTimeout(pageChangeByAjax, 200);
}
}
// request profile page to get history
requestProfilePage = function(page, filter_type) {
loadingMessage();
var requestUrl = GetRequestURL(page, filter_type);
console.log('request url: ' + requestUrl);
var requestParm = {
method: 'GET',
url: requestUrl,
timeout: ajaxTimeout,
headers: {'Content-type': 'application/x-www-form-urlencoded'},
onload: function(response) {
ajaxRequest = null;
clearTimeout(ajaxRequestTimer);
if(replaceContent(response)) {
storeStatus();
setTimeout(pageChangeByAjax, 200);
} else {
handleError();
}
},
onerror: function(response) {
ajaxRequest = null;
clearTimeout(ajaxRequestTimer);
handleError();
},
ontimeout: handleTimeout
};
// Specical timout handling for TamperMonkey
if(typeof(TM_xmlhttpRequest) !== 'undefined') {
clearTimeout(ajaxRequestTimer);
ajaxRequestTimer = setTimeout(handleTamperMonkeyTimeout, ajaxTimeout);
}
console.log('Sending request.');
ajaxRequest = GM_xmlhttpRequest(requestParm);
}
// Error or timeout handling
// Can't simply set 'timeout' value in GM_xmlhttpRequest as TamperMonkey do not support it
handleTamperMonkeyTimeout = function() {
if(ajaxRequest && ajaxRequest.abort) {
ajaxRequest.abort();
handleTimeout();
}
}
handleTimeout = function() {
timeoutMessage();
RetryRequest();
}
handleError = function() {
console.log('server error');
errorMessage();
RetryRequest();
}
tooManyRetryError = function() {
tooManyRetryMessage();
}
RetryRequest = function() {
// Tried partial request a few times, change to full request
if(retryCounter < MAX_RETRY) {
retryCounter++;
selectedServer = servers.pop();
requestProfilePage();
} else {
tooManyRetryError();
}
}
// Handle data response
replaceContent = function(response) {
clearMessage();
var data = response.responseText;
var history;
if(!data || (data.length === 0) || (data.indexOf(HISTORY_ID) === -1)) {
// No history in data response
return false;
}
// Find history in response
$j.each($j.parseHTML(data), function(i, el) {
if(el.id === 'aspnetForm') {
history = GetHistory($j(el));
return false;
}
});
console.log(history);
if(history.length === 0) return false;
$j('div#lm_history').html(history.html());
replaceButton();
console.log('request finished');
useFullRequest = false;
return true;
}
// Change button event, let this script handle page changing
replaceButton = function() {
var history = $j('div#lm_history');
history.find(NEXT_BUTTON_SELECTOR)
.attr('id', 'lm_btn_Next')
.attr('name', 'lm_btn_Next')
.click(nextPage);
history.find(PREVIOUS_BUTTON_SELECTOR)
.attr('id', 'lm_btn_Previous')
.attr('name', 'lm_btn_Previous')
.click(previousPage);
history.find(GO_BUTTON_SELECTOR)
.attr('id', 'lm_btn_GoPageNo')
.attr('name', 'lm_btn_GoPageNo')
.click(gotoPage);
history.find(FILTER_TYPE_SELECTOR)
.attr('id', 'lm_filter_type')
.attr('name', 'lm_filter_type')
.attr('onchange', '')
.change(changeFilter);
history.find(FILTER_YEAR_SELECTOR)
.remove();
history.find(PAGE_NO_TEXTBOX_SELECTOR)
.attr('id', 'lm_PageNoTextBox')
.attr('name', 'lm_PageNoTextBox');
history.find(RED_PEOPLE_POUND_SELECTOR)
.remove();
}
// Store values needed for changing page
storeStatus = function() {
var history = $j('div#lm_history');
window.LM_CURRENT_PAGE = parseInt(history.find('#lm_PageNoTextBox').val());
window.LM_FILTER_TYPE = history.find('#lm_filter_type').val();
GM_setValue('lm_last_timestamp', (new Date().getTime()).toString());
storeLocal('lm_change_page', window.LM_CURRENT_PAGE);
storeLocal('lm_filter_type', window.LM_FILTER_TYPE);
console.log('lm page: ' + window.LM_CURRENT_PAGE);
}
// Store the target page number in cookie
nextPage = function() {
var page = window.LM_CURRENT_PAGE + 1;
storeLocal('lm_change_page', page);
window.LM_CHANGE_PAGE = page;
return false;
}
previousPage = function() {
var page = Math.max(window.LM_CURRENT_PAGE - 1, 1);
storeLocal('lm_change_page', page);
window.LM_CHANGE_PAGE = page;
return false;
}
gotoPage = function() {
var history = $j('div#lm_history');
var page = parseInt(history.find('#lm_PageNoTextBox').val());
storeLocal('lm_change_page', page)
window.LM_CHANGE_PAGE = page;
return false;
}
changeFilter = function() {
var history = $j('div#lm_history');
var filter = history.find('#lm_filter_type').val();
storeLocal('lm_filter_type', filter)
window.LM_CHANGE_FILTER_TYPE = filter;
return false;
}
// localStorage related
storeLocal = function(key, value) {
if(typeof(value) !== 'undefined' && value !== null) {
localStorage[key] = JSON.stringify(value);
} else {
localStorage.removeItem(key);
}
}
loadLocal = function(key) {
var objectJSON = localStorage[key];
if(!objectJSON) return;
return JSON.parse(objectJSON);
}
deleteLocal = function(key) {
localStorage.remoteItem(key);
}
// Status message related
loadingMessage = function() {
changeAndFlashMessage('Load緊呀等陣啦<img src="faces/angel.gif" />');
}
timeoutMessage = function() {
changeAndFlashMessage('太慢喇再Load過<img src="faces/sosad.gif" />');
}
errorMessage = function() {
changeAndFlashMessage('Server 有問題再Load過<img src="faces/sosad.gif" />');
}
tooManyRetryMessage = function() {
changeAndFlashMessage('唔知咩事試過幾次都唔得, 你Reload下啦<img src="faces/sosad.gif" />');
}
changeAndFlashMessage = function(message) {
var messageDiv = $j('div#lm_message');
messageDiv.html(message);
flashMessage(messageDiv);
}
clearMessage = function() {
var messageDiv = $j('div#lm_message');
messageDiv.html('');
messageDiv.stop();
}
flashMessage = function(item) {
item.stop();
item.animate({"opacity": "0"},50).animate({"opacity": "1"},50, function(){flashMessage(item);});
}
// Helper funcitons
shuffle = function(arr) {
for(
var j, x, i = arr.length; i;
j = Math.random() * i|0,
x = arr[--i], arr[i] = arr[j], arr[j] = x
);
return arr;
}
// Initialization
setupPageElement = function() {
$j('<div id="lm"></div><br />').insertAfter($j('div#ctl00_ContentPlaceHolder1_ProfileForm').children()[0]);
$j('div#lm').html('<div>起底</div><div id="lm_history"></div>');
$j('<div id="lm_message"></div>').insertBefore('div#lm');
loadingMessage();
}
start = function() {
servers = $j.map(shuffle([1,2,3,4,5,6,7,8,9,14,15]), function(n, i) {return 'forum' + n;});
selectedServer = servers.pop();
var history = $j(HISTORY_SELECTOR);
if(history.length === 1) return;
setupPageElement();
requestProfilePage();
}
start();