imgur_show_user_stats_light

Show user statistics on imgur

目前为 2017-09-09 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name imgur_show_user_stats_light
  3. // @namespace someName
  4. // @include http://imgur.com/user/*
  5. // @include https://imgur.com/user/*
  6. // @version 0.3.6
  7. // @grant none
  8. // @description Show user statistics on imgur
  9. // ==/UserScript==
  10. // TODO: Catch errors and check result.success
  11. // TODO: Think about useable version numbers...
  12. // TODO: Add support for username.imgur.com style urls
  13. // TODO: Show errors and handle no credits remaining ?
  14.  
  15. $(window).ready(function () {
  16. var CLIENT_ID = 'cd0695f1226536b';
  17. var MAX_SUB_PAGES = 15;
  18. var _submissions_left = -1;
  19. var _sub_site = 0;
  20. var _submissions = [];
  21. var _last_sub_id = -1;
  22. var _last_post = -1;
  23. function _update_limits_from_header(xhr) {
  24. xhr.getResponseHeader('Header')
  25. $('#stats_credits_user').html('User Credits: ' + xhr.getResponseHeader('X-RateLimit-UserRemaining') + ' / ' + xhr.getResponseHeader('X-RateLimit-UserLimit'));
  26. $('#stats_credits_script').html('Script Credits: : ' + xhr.getResponseHeader('X-RateLimit-ClientRemaining') + ' / ' + xhr.getResponseHeader('X-RateLimit-ClientLimit'));
  27. }
  28.  
  29. function request_user_info(endpoint, success_cb, error_cb) {
  30. $.ajax({
  31. url: 'https://api.imgur.com/3/account/' + username + '/' + endpoint,
  32. method: 'GET',
  33. headers: {
  34. Authorization: 'Client-ID ' + CLIENT_ID,
  35. Accept: 'application/json'
  36. },
  37. success: function (a, b, c) {
  38. _update_limits_from_header(c);
  39. success_cb(a, b, c);
  40. },
  41. error: error_cb
  42. });
  43. }
  44. if (window.location.pathname.indexOf('/user/') === 0 && $('.button').filter('.comments').length > 0) {
  45. // Set up UI
  46. var username = window.location.pathname.split('/', 3) [2]; // TODO: look for a more stable way (is there an imgur js var maybe ?)
  47. var newBox = $('<div id="statsBox" class="textbox"></div>');
  48. var tble = $('<table id="_stats_table" width="100%">' +
  49. '<tr><td colspan="2" align="center" style="display:none; color:#cf3131; font-weight:bold;" id="stats_is_mod">Moderator</td></tr>' +
  50. '<tr><td colspan="2"><hr></td></tr>'+
  51. '<tr><td>Account creation</td><td align="right"><span id="stats_created"> - </span></td></tr>' +
  52. '<tr><td>Comments</td><td align="right"><a href="http://imgur.com/user/' + username + '/" id="stats_comments"> - </a></td></tr>' +
  53. '<tr><td>Submissions</td><td align="right"><a href="http://imgur.com/user/' + username + '/submitted" id="stats_submissions"> - </a></td></tr>' +
  54. '<tr><td>Albums</td><td align="right"><a href="http://' + username + '.imgur.com" id="stats_albums"> - </a></td></tr>' +
  55. '<tr><td>Images</td><td align="right"><a href="http://' + username + '.imgur.com/all" id="stats_images"> - </a></td></tr>' +
  56. '<tr><td>Favorites</td><td align="right"><a href="http://imgur.com/user/' + username + '/favorites" id="stats_favorites"> - </a></td></tr>' +
  57. '<tr><td colspan="2"><hr></td></tr>'+
  58. '<tr><td colspan="2" align="center"><button id="_btn_extend">Extended statistics</button></td></tr>' +
  59. '<tr class="_extended"><td colspan="2"><hr></td></tr>'+
  60. '<tr class="_extended"><td>Last activity</td><td align="right" id="stats_last_active"></td></tr>' +
  61. '<tr class="_extended"><td>AVG post points</td><td align="right" id="stats_score"></td></tr>' +
  62. '<tr class="_extended"><td>AVG post views</td><td align="right" id="stats_views"></td></tr>' +
  63. '<tr class="_extended"><td>Most viral posts </td><td align="right" id="stats_most_viral"></td></tr>' +
  64. '<tr class="_extended"><td>NSFW posts </td><td align="right" id="stats_nsfw"></td></tr>' +
  65. '<tr class="_extended"><td>Top tags</td><td align="right" id="stats_toptags"></td></tr>' +
  66. '<tr><td colspan="2"><hr></td></tr>'+
  67. '<tr><td colspan="2" align="center"><a href="http://community.imgur.com/users/' + username + '">IC profile</a></td></tr>' +
  68. '<tr><td style="color: #2B2B2B; font-size: 0.7em;" colspan="2" align="center" id="stats_credits_user"> - </td></tr>' +
  69. '<tr><td style="color: #2B2B2B; font-size: 0.7em;" colspan="2" align="center" id="stats_credits_script"> - </td></tr>' +
  70. '</table>');
  71. newBox.append(tble);
  72. newBox.insertBefore($('.notoriety-container'));
  73. tble.find('._extended').hide();
  74. // Add loading icon to all rows
  75. var tmp = $('#_stats_table tr td:nth-child(2)'); tmp.children().hide();
  76. tmp.append('<img style="height:1em" src="https://i.imgur.com/LR2v0rh.gif" />');
  77. $('#_btn_extend').click(function(){
  78. if(_submissions_left == 0) return;
  79. tble.find('._extended').show();
  80. get_submissions();
  81. });
  82. // get coments / submission stats
  83. request_user_info('gallery_profile', function (result, status, request) {
  84. $('#stats_comments').text(result.data.total_gallery_comments.toLocaleString()).closest('td').children().show().filter('img').remove();
  85. $('#stats_submissions').text(result.data.total_gallery_submissions.toLocaleString()).closest('td').children().show().filter('img').remove();
  86. $('#stats_favorites').text(result.data.total_gallery_favorites.toLocaleString()).closest('td').children().show().filter('img').remove();
  87. _submissions_left = result.data.total_gallery_submissions;
  88. if(_submissions_left == 0) $('#_btn_extend, #_stats_table hr:nth(1)').hide()
  89. }, function (a, b, c) {
  90. console.log('Failed to load', a, b, c);
  91. $('#stats_comments, #stats_submissions, #stats_favorites').text('Failed to load').closest('td').children().show().filter('img').remove();
  92. }
  93. );
  94. // get the exact (*) join date. TODO: I bet i messed up the time(zones) here.
  95. request_user_info('', function (result, status, request) {
  96. var date = new Date(result.data.created * 1000);
  97. var cake = new Date(result.data.created * 1000);
  98. var now = new Date();
  99. cake.setFullYear(now.getFullYear());
  100. if(cake < now){
  101. cake.setFullYear(cake.getFullYear() + 1);
  102. }
  103. var dist = cake - now;
  104. var foo = [[1000*60*60*24, 'day'], [1000*60*60, 'hour'], [1000*60, 'minute'], [1000, 'second']];
  105. var till_str = "";
  106. for(var i=0; i< foo.length; ++i){
  107. var val = Math.floor(dist / foo[i][0]);
  108. dist -= val * foo[i][0];
  109. if(val == 0 && till_str.length == 0)
  110. continue;
  111. till_str += val + " " + foo[i][1];
  112. if(val > 1) till_str += "s";
  113. till_str += " ";
  114. }
  115. $('#stats_created').text(date.toLocaleDateString()).attr('title', date.toLocaleString() + "\n" + "In: " + till_str).closest('td').children().show().filter('img').remove();
  116. if(result.data.pro_expiration != false){
  117. $('#stats_is_mod').show();
  118. }
  119. }, function (a, b, c) {
  120. console.log('Failed to load', a, b, c);
  121. $('#stats_created').text('Failed to load').closest('td').children().show().filter('img').remove();
  122. }
  123. );
  124. // album count (inklusive not submitted to gallery), if album settings are set to public.
  125. request_user_info('albums/count', function (result, status, request) {
  126. $('#stats_albums').text(result.data.toLocaleString()).closest('td').children().show().filter('img').remove();
  127. }, function (a, b, c) {
  128. console.log('Failed to load', a, b, c);
  129. $('#stats_albums').text('private').closest('td').children().show().filter('img').remove();
  130. }
  131. );
  132. // image count (inklusive not submitted to gallery), if image settings are set to public.
  133. request_user_info('images/count', function (result, status, request) {
  134. $('#stats_images').text(result.data.toLocaleString()).closest('td').children().show().filter('img').remove();
  135. }, function (a, b, c) {
  136. console.log('Failed to load', a, b, c);
  137. $('#stats_images').text('private').closest('td').children().show().filter('img').remove();
  138. }
  139. );
  140. function get_submissions() {
  141. if(_submissions_left <= 0){
  142. return;
  143. }
  144. console.log("Requesting submission page #" + _sub_site);
  145. request_user_info('submissions/' + _sub_site, function (result, status, request) {
  146. _submissions.push.apply(_submissions, result.data);
  147. _submissions_left -= result.data.length;
  148. var done = true;
  149. if(result.data.length > 0 && _submissions_left > 0 && _last_sub_id != result.data[0].id){
  150. if(_sub_site+1 == MAX_SUB_PAGES){
  151. var a = $('<span style="color:yellow; font-weight: bold; padding: 0 5px 0 5px;">!</span>');
  152. a.attr('title', 'Only the last '+ _submissions.length +' submissions are considered.');
  153. $('#stats_score, #stats_views, #stats_toptags, #stats_nsfw, #stats_most_viral').parent().children('td:nth-child(1)').append(a);
  154. }else{
  155. _last_sub_id = result.data[0].id;
  156. _sub_site += 1;
  157. get_submissions();
  158. done = false;
  159. }
  160. }
  161. if(done){
  162. if(_submissions.length > 0){
  163. // TODO: are they always returned newest first ?
  164. _last_post = _submissions[0].datetime;
  165. }
  166. calc_views();
  167. calc_score();
  168. collect_tags();
  169. get_last_comment();
  170. calc_most_viral();
  171. calc_nsfw();
  172. }
  173. }, function (a, b, c) {
  174. console.log('Failed to load', a, b, c);
  175. }
  176. );
  177. }
  178. function _get_field(array, field){
  179. var ret = Array();
  180. for(var i=0; i < array.length; ++i){
  181. ret.push(array[i][field])
  182. }
  183. return ret;
  184. }
  185. // some array funcs for convinience
  186. Array.prototype.sum = function() {
  187. return this.reduce(function(a, b) { return a + b; }, 0);
  188. };
  189. Array.prototype.max = function() {
  190. return Math.max.apply(null, this);
  191. };
  192. Array.prototype.min = function() {
  193. return Math.min.apply(null, this);
  194. };
  195. var round = Math.round;
  196. function calc_views(){
  197. var views = _get_field(_submissions ,'views');
  198. var view_sum = views.sum();
  199. var a = $('<span></span>');
  200. a.text(round(view_sum / views.length).toLocaleString());
  201. a.attr('title', 'All: ' + round(view_sum).toLocaleString() + ". Max: " + views.max().toLocaleString() + ". Min: " + views.min().toLocaleString() + ".");
  202. $('#stats_views').append(a).closest('td').children().show().filter('img').remove();
  203. }
  204. function calc_score(){
  205. var points = _get_field(_submissions ,'points');
  206. var ups = _get_field(_submissions ,'ups').sum() / points.length;
  207. var downs = _get_field(_submissions ,'downs').sum() / points.length;
  208. var point_sum = points.sum();
  209. var a = $('<span></span>');
  210. a.text( round(point_sum / points.length).toLocaleString());
  211. a.attr('title', 'Upvotes avg: ' + round(ups).toLocaleString() + ". Downvotes avg: " + round(downs).toLocaleString() + ".");
  212. $('#stats_score').append(a).closest('td').children().show().filter('img').remove();
  213. }
  214. function calc_most_viral(){
  215. var virals = _get_field(_submissions ,'in_most_viral');
  216. var _sum = virals.sum();
  217. if(virals.length > 0){
  218. val = Math.round((100.0 / virals.length) * _sum);
  219. }
  220. var a = $('<span></span>');
  221. a.text( val + "%" );
  222. a.attr('title', _sum + " / " + virals.length + " posts");
  223. $('#stats_most_viral').append(a).closest('td').children().show().filter('img').remove();
  224. }
  225. function calc_nsfw(){
  226. var nsfws = _get_field(_submissions ,'nsfw');
  227. var _sum = nsfws.sum();
  228. var val = 0;
  229. if(nsfws.length > 0){
  230. val = Math.round((100.0 / nsfws.length) * _sum);
  231. }
  232. var a = $('<span></span>');
  233. a.text( val + "%" );
  234. a.attr('title', _sum + " / " + nsfws.length + " posts");
  235. $('#stats_nsfw').append(a).closest('td').children().show().filter('img').remove();
  236. }
  237. function collect_tags(){
  238. console.log('collect_tags');
  239. var counter = {};
  240. for(var i=0; i < _submissions.length; ++i){
  241. var tags = _submissions[i].tags;
  242. for(var j=0; j < tags.length; ++j){
  243. var count = 1; var k = tags[j].display_name;
  244. if( k in counter ){
  245. count += counter[k][0];
  246. }
  247. counter[k] = [count, tags[j]];
  248. }
  249. }
  250. var vals = Object.values(counter);
  251. vals.sort(function(a, b) {
  252. return b[0] - a[0];
  253. });
  254. $('#stats_toptags').children().show().filter('img').remove();
  255. for(var i=0; i < 5 && i < vals.length; ++i){
  256. var node = $('<a>-</a><br>');
  257. node.filter('a').text(vals[i][1].display_name).attr('title', "Used "+vals[i][0]+" times").attr('href', 'https://imgur.com/t/'+vals[i][1].name);
  258. $('#stats_toptags').append(node);
  259. }
  260. }
  261. }
  262. function get_last_comment(){
  263. request_user_info('comments/newest', function (result, status, request) {
  264. if(result.data.length == 0 && _last_post == -1){
  265. $('#stats_last_active').text('-');
  266. return;
  267. }
  268. if(result.data.length == 0) return;
  269. var d = result.data[0].datetime;
  270. var link = result.data[0].datetime;
  271. if(d > _last_post){
  272. link = 'https://imgur.com/gallery/' +result.data[0].image_id+ '/comment/' + result.data[0].id;
  273. }else{
  274. link = 'https://imgur.com/gallery/' + _submissions[0].id;
  275. d = _last_post;
  276. }
  277. var delta = (new Date().getTime()/1000) - d;
  278. function pluralize(val, word){
  279. val = Math.floor(val);
  280. return val + " " + Imgur.Util.pluralize(val, word);
  281. }
  282. // thats propably somewhere already
  283. if(delta >= 356*24*60*60) delta = pluralize(delta / (356*24*60*60), " year");
  284. else if(delta >= 24*60*60) delta = pluralize(delta / (24*60*60), " day");
  285. else if(delta >= 60*60) delta = pluralize(delta / (60*60), " hour");
  286. else if(delta >= 60) delta = pluralize(delta / 60, " minute");
  287. else delta = pluralize(delta, " second");
  288. d = new Date(d * 1000);
  289. var a = $('<a></a>').attr('href', link).text(delta + " ago").attr('title', d.toLocaleDateString() + " " + d.toLocaleTimeString());
  290. $('#stats_last_active').append(a).children().show().filter('img').remove();
  291. }, function (a, b, c) {
  292. console.log('Failed to load', a, b, c);
  293. $('#stats_last_active').text('Failed to load comment').closest('td').children().show().filter('img').remove();
  294. }
  295. );
  296. }
  297. });