// ==UserScript==
// @name HKG LM finder F
// @namespace http://userscripts.org/users/peach
// @version 4.1.1
// @description HKGolden LM finder F
// @homepageURL https://greasyfork.org/scripts/874-hkg-lm-finder-f
// @include http://forum*.hkgolden.com/*
// @include http://search.hkgolden.com/*
// @include http://archive.hkgolden.com/*
// @include http://profile.hkgolden.com/*
// @exclude http://*.hkgolden.com/*rofile*age.aspx*
// @require http://code.jquery.com/jquery-1.10.2.min.js
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @copyright 2013-2014, Xelio & Peach (The part of the program is based on HKG LM finder)
// @downloadURL none
// ==/UserScript==
/*
HKG LM finder F (HKGolden LM finder F)
Copyright (C) 2013-2014 Xelio Cheong & Peach (The part of the program is based on HKG LM finder)
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 .
*/
var $j = jQuery.noConflict();
// Define CSS Style (You can override it by other script, if you want to)
var lmStyle = '';
var userID;
var errCode;
var ajaxTimeout = 15000;
var ajaxRequest;
var ajaxRequestTimer;
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);
}
// Common functions
changeAndFlashMessage = function(message) {
var messageDiv = $j('div#lm_message');
messageDiv.html(message);
// Show LM message
messageDiv.stop();
messageDiv.show().animate({"top": "0"},500);
}
clearMessage = function() {
var messageDiv = $j('div#lm_message');
// Hide LM message
messageDiv.stop();
messageDiv.delay(1000).animate({"top": "-30px"},1000, function(){messageDiv.hide(); messageDiv.html('');});
}
setup = function() {
var message = 'Load緊呀等陣啦';
changeAndFlashMessage(message);
}
handleError = function() {
if(errCode=='1'){
var message = '乜野都Load唔倒。';
changeAndFlashMessage(message);
} else if(errCode=='2'){
var lmServer = loadLocal('lmServer') || 'forum6';
var message = '請將"' + lmServer + '.hkgolden.com"放入禁止Cookie清單。';;
changeAndFlashMessage(message);
} else if(errCode=='3'){
var message = '請使用非會員伺服器。';
changeAndFlashMessage(message);
} else if(errCode=='4'){
var message = '搵唔倒起底資料。';
changeAndFlashMessage(message);
} else if(errCode=='5'){
loadtime = parseInt(ajaxTimeout)/1000
var message = 'Load左'+ loadtime + '秒都未load倒,你遲的再試下啦。';
changeAndFlashMessage(message);
} else {
console.log('server error');
var message = 'Server有問題,你遲的再試下啦。';
}
changeAndFlashMessage(message);
}
// LM Start
requestLM = function() {
var message = 'Load 緊';
changeAndFlashMessage(message);
var lmServer = loadLocal('lmServer') || 'forum6';
var lmPage = loadLocal('lmPage') || '1';
var requestUrl = 'http://' + lmServer + '.hkgolden.com/ProfilePage.aspx?type=history&page=' + lmPage + '&yearFilter=3&filterType=all&userid=' + userID;
GM_xmlhttpRequest({
method: "GET",
url: requestUrl,
timeout: ajaxTimeout,
onload: function(response) {
ajaxRequest = null;
clearTimeout(ajaxRequestTimer);
if(replaceContent(response)) {
// success
} else {
handleError();
}
},
ontimeout: function() {
errCode='5';
handleError();
}
});
}
// Handle data response
replaceContent = function(response) {
var message = 'Load 完';
changeAndFlashMessage(message);
clearMessage();
var data = response.responseText;
var history;
var hvCookie;
var needLogin;
if(!data || (data.length === 0)) {
// No history in data response
console.log('No data response');
errCode='1';
return false;
}
$j.each($j.parseHTML(data), function(i, el) {
if(el.id === 'aspnetForm') {
var doms = $j(el);
hvCookie = doms.find('#ctl00_ContentPlaceHolder1_mp1');
needLogin = doms.find('#ctl00_ContentPlaceHolder1_txt_email');
history = doms.find('#ctl00_ContentPlaceHolder1_dataLabel');
return false;
}
});
if(hvCookie.length != 0){
console.log('Requested with Cookie');
errCode='2';
return false;
}
if(needLogin.length != 0){
console.log('Need Login');
errCode='3';
return false;
}
if(history.find('#history>tbody>tr:eq(2)').length === 0) {
console.log('No history found');
//console.log(data);
errCode='4';
return false;
}
var lmPage = loadLocal('lmPage') || '1';
$j('div#lm_history').html('
' +
'
於三個月內的全部貼文 ' +
'第頁' +
' ' +
'' +
''+
'
' + history.html());
buttonTriggerLM();
// Display modal box when history was successfully loaded
$j('div#lm').fadeIn(800);
$j('div#lm-mask').fadeTo(500, 0.7);
// Update modal box height to fit the viewport
var wHeight = $j(window).height() - 72;
$j('div#lm_history').css('height', 'auto');
if($j('div#lm_history').height() > wHeight) $j('div#lm_history').css('height', wHeight+'px');
console.log('LM request finished');
return true;
}
buttonTriggerLM = function() {
lmPageGo = function(e) {
e.preventDefault;
var page = history.find('#pageTextBox').val();
storeLocal('lmPage', page);
requestLM();
return false;
}
previousBtn = function(e) {
e.preventDefault;
var lmPage = loadLocal('lmPage') || '1';
var page = parseInt(lmPage) - 1;
if(page<1) { page = '1' }
storeLocal('lmPage', page);
requestLM();
return false;
}
nextBtn = function(e) {
e.preventDefault;
var lmPage = loadLocal('lmPage') || '1';
var page = parseInt(lmPage) + 1;
storeLocal('lmPage', page);
requestLM();
return false;
}
var history = $j('div#lm_history');
history.find('#pageGoBtn').click(lmPageGo);
history.find('#previousBtn').click(previousBtn);
history.find('#nextBtn').click(nextBtn);
}
// LM End
// Ming E Lau Start
requestMEL = function() {
var message = 'Load 緊';
changeAndFlashMessage(message);
var bmType = loadLocal('bmType') || 'lastPost';
var bmPage = loadLocal('bmPage') || '1';
var requestUrl = 'http://' + window.location.hostname + '/ProfilePage.aspx?type=bookmark&page=' + bmPage + '&orderType=' + bmType + '&userid=' + userID;
GM_xmlhttpRequest({
method: "GET",
url: requestUrl,
timeout: ajaxTimeout,
onload: function(response) {
ajaxRequest = null;
clearTimeout(ajaxRequestTimer);
if(replaceContent2(response)) {
// success
} else {
handleError();
}
},
ontimeout: function() {
errCode='5';
handleError();
}
});
}
// Handle data response
replaceContent2 = function(response) {
var message = 'Load 完';
changeAndFlashMessage(message);
clearMessage();
var data = response.responseText;
var history;
if(!data || (data.length === 0)) {
// No history in data response
console.log('No data response');
return false;
}
$j.each($j.parseHTML(data), function(i, el) {
if(el.id === 'aspnetForm') {
var doms = $j(el);
history = doms.find('#ctl00_ContentPlaceHolder1_dataLabel');
bmTotal = doms.find('#ctl00_ContentPlaceHolder1_totalBM');
return false;
}
});
if(history.length === 0) {
console.log('No history found');
//console.log(data);
return false;
}
history.find('#bookmark tbody tr').find('td:eq(6)').remove();
history.find('#bookmark tbody tr:eq(0) td:eq(5)').remove();
var bmType = loadLocal('bmType') || 'lastPost';
var bmPage = loadLocal('bmPage') || '1';
var lp='';
var bm='';
if(bmType=='lastPost') {
lp=' selected="selected"';
} else {
bm=' selected="selected"';
}
$j('div#lm_history').html('
' + history.html());
buttonTrigger();
// Display modal box when history was successfully loaded
$j('div#lm').fadeIn(800);
$j('div#lm-mask').fadeTo(500, 0.7);
// Update modal box height to fit the viewport
var wHeight = $j(window).height() - 72;
$j('div#lm_history').css('height', 'auto');
if($j('div#lm_history').height() > wHeight) $j('div#lm_history').css('height', wHeight+'px');
console.log('MEL request finished');
return true;
}
buttonTrigger = function() {
bmPageGo = function(e) {
e.preventDefault;
var page = history.find('#pageTextBox').val();
var type = history.find('#bmOrder').val();
storeLocal('bmType', type);
storeLocal('bmPage', page);
requestMEL();
return false;
}
previousBtn = function(e) {
e.preventDefault;
var bmPage = loadLocal('bmPage') || '1';
var page = parseInt(bmPage) - 1;
var type = history.find('#bmOrder').val();
storeLocal('bmType', type);
if(page<1) { page = '1' }
storeLocal('bmPage', page);
requestMEL();
return false;
}
nextBtn = function(e) {
e.preventDefault;
var bmPage = loadLocal('bmPage') || '1';
var page = parseInt(bmPage) + 1;
var type = history.find('#bmOrder').val();
storeLocal('bmType', type);
if(page>25) { page = '25' }
storeLocal('bmPage', page);
requestMEL();
return false;
}
var history = $j('div#lm_history');
history.find('#pageGoBtn').click(bmPageGo);
history.find('#previousBtn').click(previousBtn);
history.find('#nextBtn').click(nextBtn);
}
// Ming E Lau End
// PM Start
requestPM = function() {
var message = 'Load 緊';
changeAndFlashMessage(message);
var pmPage = loadLocal('pmPage') || '1';
var requestUrl = 'http://' + window.location.hostname + '/ProfilePage.aspx?&type=pm&page=' + pmPage + '&userid=' + userID;
GM_xmlhttpRequest({
method: "GET",
url: requestUrl,
timeout: ajaxTimeout,
onload: function(response) {
ajaxRequest = null;
clearTimeout(ajaxRequestTimer);
if(replaceContent3(response)) {
// success
} else {
handleError();
}
},
ontimeout: function() {
errCode='5';
handleError();
}
});
}
// Handle data response
replaceContent3 = function(response) {
var message = 'Load 完';
changeAndFlashMessage(message);
clearMessage();
var data = response.responseText;
var history;
if(!data || (data.length === 0)) {
// No history in data response
console.log('No data response');
return false;
}
$j.each($j.parseHTML(data), function(i, el) {
if(el.id === 'aspnetForm') {
var doms = $j(el);
history = doms.find('#ctl00_ContentPlaceHolder1_dataLabel');
return false;
}
});
if(history.length === 0) {
console.log('No history found');
//console.log(data);
return false;
}
history.find('#pm tbody tr').find('td:eq(3)').remove();
var pmPage = loadLocal('pmPage') || '1';
$j('div#lm_history').html('
訊息內容 -
' +
'
' +
'
第頁' +
' ' +
'' +
'' +
'
' + history.html());
buttonTrigger2();
// Display modal box when history was successfully loaded
$j('div#lm').fadeIn(800);
$j('div#lm-mask').fadeTo(500, 0.7);
// Update modal box height to fit the viewport
var wHeight = $j(window).height() - 72;
$j('div#lm_history').css('height', 'auto');
if($j('div#lm_history').height() > wHeight) $j('div#lm_history').css('height', wHeight+'px');
console.log('PM request finished');
return true;
}
buttonTrigger2 = function() {
pmPageGo = function(e) {
e.preventDefault;
var page = history.find('#pageTextBox').val();
storeLocal('pmPage', page);
requestPM();
return false;
}
previousBtn = function(e) {
e.preventDefault;
var pmPage = loadLocal('pmPage') || '1';
var page = parseInt(pmPage) - 1;
if(page<1) { page = '1' }
storeLocal('pmPage', page);
requestPM();
return false;
}
nextBtn = function(e) {
e.preventDefault;
var pmPage = loadLocal('pmPage') || '1';
var page = parseInt(pmPage) + 1;
storeLocal('pmPage', page);
requestPM();
return false;
}
var history = $j('div#lm_history');
history.find('#pageGoBtn').click(pmPageGo);
history.find('#previousBtn').click(previousBtn);
history.find('#nextBtn').click(nextBtn);
}
// PM End
// Setting Start
settingLM = function() {
var lmServer = loadLocal('lmServer') || 'forum6';
var serverList = ['forum1', 'forum2', 'forum3', 'forum4', 'forum5', 'forum6', 'forum7', 'forum8', 'forum9', 'forum14', 'forum15', 'archive'];
var message = '
請選擇起底專用Server
';
changeAndFlashMessage(message);
saveSetting = function(e) {
e.preventDefault;
var lmMessage = $j('div#lm_message');
storeLocal('lmServer', lmMessage.find("#lmServer").val());
var message = '已Save';
changeAndFlashMessage(message);
clearMessage();
return false;
}
setTimeout(function() {
var lmMessage = $j('div#lm_message');
lmMessage.find("#lmServer").val(lmServer);
lmMessage.find('#lm-setting').css("display", "block");
lmMessage.find('#saveSetting').click(saveSetting);
}, 100);
}
// Setting End
// Wait 500ms; Sometimes HKG loading is very slow, if after 500ms does not get the userID, this script will not active.
setTimeout(function() {
// Get User ID
userID = $j('#ctl00_ContentPlaceHolder1_lb_UserName a:eq(0)').attr('href').replace(/[^0-9]/g, '');
if(userID) {
$j('head').append(lmStyle);
$j('body').append('