Elo-Rang

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址