Created
May 14, 2016 10:45
-
-
Save iamsortiz/6da8a8063ab5d3b0152e37d760df5ff1 to your computer and use it in GitHub Desktop.
Extract Reddit page stats from the Browser console
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
//////////////////////////////////////////////////////////////////////////////// | |
// | |
// The MIT License (MIT) | |
// | |
// Copyright (c) 2016 Samuel Ortiz Reina (@iamsortiz) | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
//////////////////////////////////////////////////////////////////////////////// | |
//from: http://stackoverflow.com/questions/9716468/is-there-any-function-like-isnumeric-in-javascript-to-validate-numbers | |
function isNumeric(n) { | |
return !isNaN(parseFloat(n)) && isFinite(n); | |
} | |
/** | |
* Extract aggregated data from target element attributes | |
* | |
* @param {CSS selector} selector target elements selector | |
* @param {function} extractor return target text from JQuery element | |
* @return {object} max and average occurences | |
*/ | |
function extractStatsFor(selector, extractor) { | |
var sum = 0; | |
var count = 0; | |
var max = 0; | |
//For each target element | |
$(selector).each(function () { | |
//Get target text | |
var text = extractor($(this)); | |
//Extract agregate stats | |
if (isNumeric(text)) { | |
var number = parseInt(text); | |
if (number > max) max = number; | |
sum += number; | |
count++; | |
} | |
}); | |
var average = sum / count; | |
var stats = { | |
max: max, | |
average: average | |
}; | |
return stats; | |
} | |
/** | |
* Extract stats for multiple criteria | |
*/ | |
function extractStats() { | |
var votes_selector = '.score.unvoted'; | |
var votes_extractor = function (element) { | |
return element.text(); | |
}; | |
var votes = extractStatsFor(votes_selector, votes_extractor); | |
var comments_selector = '.comments'; | |
var comments_extractor = function (element) { | |
return (element.text().split(' '))[0]; | |
}; | |
var comments = extractStatsFor(comments_selector, comments_extractor); | |
var stats = { | |
id: window.location.href, | |
votes: votes, | |
comments: comments | |
}; | |
console.log(JSON.stringify(stats, null, 2)); | |
return stats; | |
} | |
extractStats(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment