Show institution on SPOJ score list

Adds an institution column to the score page on SPOJ

  1. // ==UserScript==
  2. // @name Show institution on SPOJ score list
  3. // @namespace spt.jobsafari.dk
  4. // @description Adds an institution column to the score page on SPOJ
  5. // @include http://www.spoj.com/*
  6. // @version 1
  7. // @grant none
  8. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
  9. // ==/UserScript==
  10. function key (user) { return "jix_institution_mapping_" + user; }
  11. function get_user_institution (user) {
  12. return window.localStorage.getItem( key(user) );
  13. }
  14. function set_user_institution (user, value) {
  15. return window.localStorage.setItem( key(user), value );
  16. }
  17.  
  18. console.log("Running script");
  19.  
  20. $( function () {
  21. var location = "" + document.location;
  22. var match;
  23. console.log(location);
  24. if (location.match(/\/ranks2\b/)) {
  25. console.log("Ranking page!");
  26. $('thead th:nth-child(2)').after('<th>Institution</th>');
  27.  
  28. setInterval( function () {
  29. $('.missing-user').each( function () {
  30. var $this = $(this);
  31. var username = $this.data('user');
  32. var institution = get_user_institution(username);
  33. if (institution) {
  34. console.log("Found institution for " + username);
  35. $this.text(institution);
  36. $this.removeClass('missing-user');
  37. }
  38. } );
  39. }, 2000 );
  40.  
  41. $('table.table a').each( function () {
  42. var $this = $(this);
  43. match = $this.attr('href').match(/\/users\b\/([^/]+)/);
  44. if (! match) return;
  45.  
  46. var username = match[1];
  47. var institution = get_user_institution(username);
  48. var container = $('<td>Loading...</td>');
  49. $this.parent().after(container);
  50.  
  51. if (institution) {
  52. container.text(institution);
  53. } else {
  54. container.addClass('missing-user').data('user', username);
  55. console.log("No institution known for " + username + "; attempting to load...");
  56. var frame = $('<iframe style="visibility: hidden; display: none">').attr('src', $this.attr('href'));
  57. $this.append(frame);
  58. }
  59. } );
  60. } else if (match = location.match(/\/users\b\/([^/]+)/)) {
  61. var username = match[1];
  62. console.log("User page for " + username);
  63. var $institution = $('#user-profile-left p:has(.fa-building-o)');
  64. if ($institution.length > 0 &&
  65. (match = $institution.text().match(/Institution: (.*)/))) {
  66. var institution = match[1];
  67. console.log("Institution found; it's: " + institution);
  68. set_user_institution(username, institution);
  69. } else {
  70. console.log("No institution found for " + username);
  71. set_user_institution(username, 'N/A');
  72. }
  73. }
  74. } );