Created
April 10, 2016 03:56
-
-
Save anonymous/1d8251bf8f58ef7321f5172643e75075 to your computer and use it in GitHub Desktop.
jump to v2ex last reply
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name v2ex last reply | |
// @namespace http://random.com | |
// @version 0.1 | |
// @description jump to last reply, no scroll anymore! | |
// @author xiandao7997 | |
// @match http*://v2ex.com/t/* | |
// @require http://cdn.bootcss.com/jquery/2.2.1/jquery.js | |
// @grant GM_getValue | |
// ==/UserScript== | |
/* jshint -W097 */ | |
$(function() { | |
var URL = window.location.href; | |
var REPLY_TOTAL_PREFIX = 'V2EX_totalcount_'; | |
var LAST_REPLY_ID = 'V2EX_lastreplyid_'; | |
function getPostId() { | |
var flag = '/t/'; | |
var startIndex = URL.indexOf(flag) + flag.length; | |
var endIndex = URL.indexOf('#') >= 0 ? URL.indexOf('#') : URL.length; | |
var postId = URL.substring(startIndex, endIndex); | |
return postId; | |
} | |
function getReplyTotalFromLS(postId) { | |
return localStorage[REPLY_TOTAL_PREFIX + postId]; | |
} | |
function setReplyTotalToLS(postId, count) { | |
localStorage.setItem(REPLY_TOTAL_PREFIX + postId, count); | |
} | |
function setLastReplyIdToLS(postId, value) { | |
localStorage.setItem(LAST_REPLY_ID + postId, value); | |
} | |
function getLastReplyIdFromLS(postId) { | |
return localStorage[LAST_REPLY_ID + postId]; | |
} | |
function jumpToNextPage(iLastTimeTotal, currentReplyCount, pureUrl) { | |
if (iLastTimeTotal === currentReplyCount) { | |
// no new reply added | |
} else { | |
var newPageNo = iLastTimeTotal/100 + 1; | |
var newUrl = pureUrl + '?p=' + newPageNo; | |
window.location.href = newUrl; | |
} | |
} | |
var postId = getPostId(); | |
var lastTimeTotalCount = getReplyTotalFromLS(postId); | |
var strCurrentReplyCount = $('div.cell > span.gray').html(); | |
var currentReplyCount = 0; | |
if (strCurrentReplyCount !== undefined) { | |
currentReplyCount = parseInt(strCurrentReplyCount.substring(0, strCurrentReplyCount.indexOf(' ')), 10); | |
} | |
var iLastTimeTotal = parseInt(lastTimeTotalCount, 10); | |
if (iLastTimeTotal === undefined || iLastTimeTotal === null || isNaN(iLastTimeTotal)) { | |
} else { | |
var pureUrl = URL; | |
var anchorIndex = URL.indexOf('#'); | |
if (anchorIndex !== -1 ) { | |
pureUrl = URL.substring(0, anchorIndex); | |
} | |
if (iLastTimeTotal === 0) { | |
} else if (iLastTimeTotal % 100 === 0) { | |
jumpToNextPage(iLastTimeTotal, currentReplyCount, pureUrl); | |
} else { | |
window.location.href = pureUrl + '#' + getLastReplyIdFromLS(postId); | |
} | |
} | |
setReplyTotalToLS(postId, currentReplyCount); | |
var lastestReplyId = $('div#Main > div.box > div[id]:last').attr('id'); | |
setLastReplyIdToLS(postId, lastestReplyId); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment