Elo-Rang AT

show current ELO Rang in profile

  1. /* This program is free software. It comes without any warranty, to
  2. * the extent permitted by applicable law. You can redistribute it
  3. * and/or modify it under the terms of the Do What The Fuck You Want
  4. * To Public License, Version 2, as published by Sam Hocevar. See
  5. * http://www.wtfpl.net/ for more details. */
  6.  
  7. // ==UserScript==
  8.  
  9. // @name Elo-Rang AT
  10. // @namespace https://greasyfork.org/users/83290
  11. // @include http://fussballcup.at/*
  12. // @version 0.1.4
  13. // @description show current ELO Rang in profile
  14. // @author Philipp, edited by mot33 / 2018
  15. // @grant GM_addStyle
  16. // @connect <value>
  17. // ==/UserScript==
  18.  
  19. var timeout = 5000;
  20.  
  21. /**
  22. * Simply creates async html request with url theURL; calls back callback with result string as first parameter and args as second parameter
  23. *
  24. * Case of error; null is returned as result String
  25. */
  26. function httpGetAsync(theUrl, callback, args){
  27. var xmlHttp = new XMLHttpRequest();
  28.  
  29. xmlHttp.onreadystatechange = function() {
  30. if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
  31. callback(xmlHttp.responseText, args);
  32. }
  33. };
  34.  
  35. xmlHttp.open("GET", theUrl, true); // true for asynchronous
  36. xmlHttp.send(null);
  37.  
  38. // handle timeout
  39. window.setTimeout(function(){
  40. if (xmlHttp.readyState != 4 || xmlHttp.status != 200){
  41. // something went wrong
  42. // cancel
  43. xmlHttp.abort();
  44. // call callback with error
  45. callback(null, args);
  46. }
  47. }, timeout);
  48. }
  49.  
  50. /**
  51. * really (like really really) simple logger :)
  52. */
  53. function logError(msg){
  54. console.log("ELO-rank show script: " + msg);
  55. }
  56.  
  57. // make sure changes() isn't executed twice the same time
  58. var lock=false;
  59. // do not search for same name twice (if once failed, will probably fail again; performance!)
  60. var lastName="";
  61. function checkForSearchNameOrReturnNull(){
  62. // and that's just lots of text to make sure, this script isn't executed more than necesarry
  63.  
  64. // make sure this function isn't executed twice at the same time
  65. if(lock){
  66. return null;
  67. }
  68.  
  69. // make sure, rank must be added (= profil show page)
  70. var url = window.location.href;
  71. if(!(/module=([^&]+)/.exec(url)[1]=='profile')){
  72. return null;
  73. }
  74. if(!(/action=([^&]+)/.exec(url)[1]=='show')){
  75. return null;
  76. }
  77.  
  78. // make sure rank isn't allready sucessfully added
  79. if(document.getElementById('rankshow')!=null){
  80. return null;
  81. }
  82.  
  83. // make sure, profile-show exists
  84. var profile_show = document.getElementById('profile-show');
  85. if(profile_show == null || profile_show.firstChild == null){
  86. // log something strange happend; actually profile page should have profile-show element
  87. logError("Something strange happended! Recognized profile page but no profile-show element to extract name from!");
  88. return null;
  89. }
  90.  
  91. // extract profile name name
  92. // substring(11): String is "Profil von (...)". Extract (...)
  93. var name = profile_show.firstChild.textContent.substring(11);
  94.  
  95. // do not execute script for same name twice
  96. if(lastName == name){
  97. lock=false;
  98. return null;
  99. }
  100. lastName = name;
  101.  
  102. // finally return result
  103. return name;
  104. }
  105.  
  106. /**
  107. * Takes rank as attribute
  108. * Creates html elements and addes rank information to info box
  109. */
  110. function appendRank(rank){
  111. // create html frame
  112. var s ='<li><strong id="rankshow" <style=top:-10px; class="player-name">Elo-Rang:&nbsp';
  113. s+="";{
  114. s+=rank = "<font color='yellow'>"+ String(rank) + "&nbsp</font></strong></b>";
  115. }
  116. var div = document.createElement('div');
  117. div.innerHTML = s;
  118.  
  119. // insert
  120. var elementsProfileBox = document.getElementsByClassName('player-name');
  121. if(elementsProfileBox.length==0){
  122. // okay, that's strange
  123. logError("Strange error while adding rank information: No Element of class 'profile-box-squad' found! Don't know where to add information!");
  124. }else{
  125. elementsProfileBox[0].appendChild(div);
  126. }
  127.  
  128. // unlock
  129. lock=false;
  130. }
  131.  
  132.  
  133. /**
  134. * gets resulst String
  135. * parses and extracts ELO rank
  136. * checks, if it is ELO rank for args['name']
  137. * calls args['call'] with first parameter: rank
  138. */
  139. function fetchELOResults(requestResult, args){
  140. // check for valid result
  141. if(requestResult == null){
  142. args['call']('error');
  143. logError("Error html request!");
  144. return;
  145. }
  146.  
  147. // parse result
  148. try{
  149. var parser = document.createElement('html');
  150. parser.innerHTML = JSON.parse(requestResult).content;
  151.  
  152. // get right fieled
  153. var results = parser.getElementsByClassName(' odd');
  154.  
  155. if(results.length == 0){
  156. // okay, no problem! Just no rank found: probably user doesn't have a rank
  157. args['call']('/');
  158. return;
  159. }
  160. results = results[0];
  161.  
  162. // check if name is right
  163. if(results.children[2].children[1].innerHTML!=args['name']){
  164. // okay, no problem! Just not right user found: problably user doesn't have a rank
  165. args['call']('/');
  166. return;
  167. }
  168.  
  169. // call callback with rank to handle everything else
  170. args['call'](results.firstChild.innerHTML);
  171.  
  172. }catch(e){
  173. args['call']('error');
  174. logError("Error parsing resulst String of ELO-rank request! Error: " + e);
  175. return;
  176. }
  177. }
  178.  
  179.  
  180. /**
  181. * searches ELO rang
  182. * will call back toCall with first paramter elo rang of player with name "name"
  183. */
  184. function searchELORang(name, toCall){
  185. // wrap parameters in set
  186. params={};
  187. params['name'] = name;
  188. params['call'] = toCall;
  189.  
  190. // schedule html requst
  191. httpGetAsync('http://fussballcup.at/index.php?club=' + name + '&_qf__form=&module=rating&action=index&area=user&league=&path=index.php&layout=none', fetchELOResults , params);
  192. }
  193.  
  194.  
  195. /**
  196. * Main function! Will check, if script execution is necessar; will lookup ELO rank and will append rank to profile
  197. */
  198. function changes(){
  199. // get name; will catch all cases where nothing has to be done
  200. var name = checkForSearchNameOrReturnNull();
  201.  
  202. if(name == null){
  203. // nothing to do
  204. return;
  205. }
  206.  
  207. // set lock
  208. lock=true;
  209.  
  210. // Real script execution!!!
  211. searchELORang(name, appendRank);
  212. }
  213.  
  214. window.setTimeout(function() { changes() }, 2500);
  215. window.setInterval(function() { changes() }, 5000);