imgur_show_user_stats_light

Show user statistics on imgur

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

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