Skip to content

Instantly share code, notes, and snippets.

@florian
Last active March 18, 2016 16:16
Show Gist options
  • Save florian/dd408a8399aebba43c04 to your computer and use it in GitHub Desktop.
Save florian/dd408a8399aebba43c04 to your computer and use it in GitHub Desktop.
Quora user profile: Sort answers by upvotes

It's mostly dirty jQuery scripting, so it might break whenever Quora updates their markup.

// We need this method because Quora displays 1500 upvotes as "1.5k"
function parseUpvotes (str) {
if (match = str.match(/(\d+)\.(\d+)/)) {
return Number(match[1]) * 1000 + Number(match[2]) * 100
} else {
return parseInt(str, 10)
}
}
function sortByUpvotes () {
// Prepending is important, it will only move the elements and that way we don't lose the event handlers
$('.PagedList.UserAnswerProfileFeed').prepend($('.pagedlist_item').sort((a,b) => {
var x = $(a).find('.count').html()
var y = $(b).find('.count').html()
return parseUpvotes(y) - parseUpvotes(x)
}))
}
// This might take a while
function loadAllAnswers (callback) {
var total = parseInt($('.list_header').text().replace(",", ""), 10)
var timer = setInterval(() => {
console.log("%s / %s answers loaded", $('.pagedlist_item').length, total)
if ($('.pagedlist_item').length >= total) {
clearInterval(timer)
callback()
} else {
window.scrollTo(0,document.body.scrollHeight)
}
}, 1000)
}
// Sorting currently loaded answers by upvotes
sortByUpvotes()
// First loading all answers and then sorting them by upvotes
loadAllAnswers(() => {
sortByUpvotes()
window.scrollTo(0, 0)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment