pp for osu score pages

shows pp data from osustats.ppy.sh on osu score pages.

当前为 2014-11-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name pp for osu score pages
  3. // @namespace http://osustats.ppy.sh
  4. // @description shows pp data from osustats.ppy.sh on osu score pages.
  5. // @include http*://osu.ppy.sh/b/*
  6. // @include http*://osu.ppy.sh/s/*
  7. // @include http*://osu.ppy.sh/p/beatmap?b=*
  8. // @include http*://osu.ppy.sh/p/beatmap?s=*
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @grant GM_xmlhttpRequest
  12. // @version 3
  13. // ==/UserScript==
  14.  
  15. var result=null;
  16. var mapID=null;
  17. var mapMode=null;
  18. var scoresMissing=false;
  19. var time = 10;
  20. var interval;
  21. var requestedUpdate=false;
  22. function Start()
  23. {
  24. scoresMissing=false;
  25. if(mapID!=null && mapMode!=null)
  26. {
  27. GetScores(mapID,mapMode,function(res){
  28. result = JSON.parse(res);
  29. UpdateOsuScoresTable();
  30. if(scoresMissing && !requestedUpdate)
  31. {
  32. RequestBeatmapUpdate(mapID,mapMode,function(accepted){
  33. if(accepted)
  34. {
  35. interval = setInterval(Countdown, 1000);
  36. }
  37. });
  38. }
  39. else
  40. if(requestedUpdate)
  41. {
  42. SetInfoText("Updated successfully");
  43. }
  44. });
  45. }
  46.  
  47. }
  48. function Init()
  49. {
  50. var mapTab = document.getElementsByClassName("beatmapTab active");
  51. if(mapTab.length==1)
  52. {
  53. mapID = mapTab[0].href.split("/")[4];
  54. mapID = mapID.split("&")[0];
  55.  
  56. mapMode = document.getElementsByClassName("active");
  57. mapMode = mapMode[mapMode.length-1].href.split("&m=")[1];
  58. }
  59. }
  60. function UpdateOsuScoresTable()
  61. {
  62. var rows,username,score,row,pp;
  63.  
  64. rows = document.evaluate('//div[@class=\'beatmapListing\']/table/tbody/tr',
  65. document,
  66. null,
  67. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  68. null);
  69. rows.snapshotItem(0).children[5].innerHTML="Combo / pp";
  70.  
  71. for (var i=1;i<rows.snapshotLength;i++) {
  72. row = rows.snapshotItem(i);
  73. username = row.children[4].children[1].innerHTML;
  74. score = row.children[2].innerHTML.replace(/,/g,'').replace(/<b>/g,'').replace(/<\/b>/g,'');
  75. pp = GetPpFromUsername(username,score);
  76. console.log(requestedUpdate);
  77. if(requestedUpdate)
  78. {
  79. row.children[5].innerHTML = row.children[5].innerHTML.substring(0, row.children[5].innerHTML.indexOf('/'));
  80. }
  81. row.children[5].innerHTML += pp;
  82. }
  83. }
  84. function GetPpFromUsername(username,score)
  85. {
  86. for (var i=0;i<result.length;i++) {
  87. if(username == result[i].name)
  88. {
  89. if(score == result[i].score)
  90. {
  91. return " / "+(Math.round(result[i].pp * 100) / 100)+"pp";
  92. }
  93. scoresMissing=true;
  94. return " / N/U";
  95. }
  96. }
  97. scoresMissing=true;
  98. return " / N/D";
  99. }
  100. function GetScores(mapID,mapMode,callback)
  101. {
  102. GetPage("http://osustats.ppy.sh/api/beatmap/getScores/"+ mapID + "/"+ mapMode,function(res){
  103. callback(res);
  104. });
  105. }
  106. function RequestBeatmapUpdate(mapID,mapMode,callback)
  107. {
  108. GetPage("http://osustats.ppy.sh/api/beatmap/updateRequest/"+ mapID + "/"+ mapMode,function(res){
  109. requestedUpdate=true;
  110. res = JSON.parse(res);
  111. console.log(res.status);
  112. if(res.status=="OK"){
  113. callback(true);
  114. }
  115. else{
  116. callback(false);
  117. SetInfoText("Request failed- try again later.");
  118. }
  119. });
  120. }
  121. function GetPage(url,callback)
  122. {
  123. GM_xmlhttpRequest({
  124. method: "GET",
  125. url: url,
  126. synchronous: true,
  127. headers: {
  128. Referer: location.href
  129. },
  130. onload: function(resp) {
  131. callback(resp.responseText);
  132. }
  133. });
  134. }
  135. function Countdown()
  136. {
  137. time--;
  138. SetInfoText("Missing scores detected- Update requested</br>Updating in "+time+" seconds");
  139. console.log(time);
  140. if(time == 0){
  141. clearInterval(interval);
  142. SetInfoText("Updating...");
  143. Start();
  144. }
  145. }
  146. function SetInfoText(text)
  147. {
  148. rows = document.evaluate('//div[@class=\'content-with-bg\']/h2',
  149. document,
  150. null,
  151. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  152. null);
  153. row = rows.snapshotItem(0)
  154. row.previousElementSibling.innerHTML = "<h1 style=\"font: 30px; text-align: center;\">"+text+"</h1>";
  155. }
  156. window.addEventListener('load', function() {
  157. Init();
  158. Start();
  159. }, false);