Guest User

Untitled

a guest
May 6th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  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_deleteValue
  12. // @grant GM_xmlhttpRequest
  13. // @version 4.22
  14. // ==/UserScript==
  15.  
  16. if (!this.GM_getValue || (this.GM_getValue.toString && this.GM_getValue.toString().indexOf("not supported")>-1)) {
  17. this.GM_getValue=function (key,def) {
  18. return localStorage[key] || def;
  19. };
  20. this.GM_setValue=function (key,value) {
  21. return localStorage[key]=value;
  22. };
  23. this.GM_deleteValue=function (key) {
  24. return delete localStorage[key];
  25. };
  26. }
  27.  
  28. var result = null,
  29. mapID = null,
  30. mapMode = null,
  31. scoresMissing = false,
  32. time = 10,
  33. interval,
  34. requestedUpdate = false,
  35. updateInProgress=true,
  36. isNoModOnly=false,
  37. InfoBoxRef = null,
  38. scoresTableRef = null,
  39. scoresTableBodyRef = null;
  40.  
  41.  
  42. function Start() {
  43. scoresMissing = false;
  44. if (mapID != null && mapMode != null) {
  45. GetScores(mapID, mapMode, function(res) {
  46. result = JSON.parse(res);
  47. UpdateOsuScoresTable();
  48. if (scoresMissing) {
  49. if(requestedUpdate) {
  50. updateInProgress = false;
  51. SetInfoText("Updated successfully");
  52.  
  53. if(GM_getValue("Sort_by_pp"))
  54. SortOsuScoresTable(true);
  55. }
  56. else
  57. {
  58. if(!isNoModOnly)
  59. {
  60. RequestBeatmapUpdate(mapID, mapMode, function(accepted) {
  61. if (accepted) {
  62. interval = setInterval(Countdown, 1000);
  63. }
  64. });
  65. }
  66. }
  67. }
  68. else
  69. {
  70. updateInProgress = false;
  71.  
  72. if(GM_getValue("Sort_by_pp"))
  73. SortOsuScoresTable(true);
  74. }
  75. });
  76.  
  77.  
  78. }
  79. }
  80.  
  81. function Init() {
  82. InfoBoxRef = document.getElementsByClassName("content-with-bg")[0].getElementsByTagName("h2")[document.getElementsByClassName("content-with-bg")[0].getElementsByTagName("h2").length-1];
  83. if (InfoBoxRef != null) {
  84. mapID = InfoBoxRef.nextElementSibling.children[0].children[0].value;
  85. mapMode = InfoBoxRef.nextElementSibling.children[0].children[1].value;
  86. if (mapID != null) {
  87. RefreshTableReferences();
  88. isNoModOnly =InfoBoxRef.nextElementSibling.children[0].children[2].checked;
  89. AddppSortCheckbox();
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95.  
  96. function RefreshTableReferences() {
  97. scoresTableRef = document.getElementsByClassName("beatmapListing")[0].children[0];
  98. scoresTableBodyRef = scoresTableRef.getElementsByTagName('tbody')[0];
  99. }
  100.  
  101. function UpdateOsuScoresTable() {
  102. numOfRows = scoresTableBodyRef.getElementsByTagName("tr").length;
  103.  
  104. if (!requestedUpdate) {
  105. th = document.createElement('th');
  106. th.innerHTML = "pp"
  107. scoresTableBodyRef.children[0].insertBefore(th, scoresTableBodyRef.children[0].childNodes[3]);
  108. }
  109. for (var i = 1; i < numOfRows; i++) {
  110. row = scoresTableBodyRef.children[i];
  111.  
  112. if(requestedUpdate)
  113. {
  114. username = row.children[5].children[1].innerHTML;
  115. ppCell = scoresTableBodyRef.getElementsByTagName("tr")[i].children[3];
  116. }
  117. else
  118. {
  119. username = row.children[4].children[1].innerHTML;
  120. ppCell = scoresTableBodyRef.getElementsByTagName("tr")[i].insertCell(3);
  121. }
  122.  
  123. score = row.children[2].innerHTML.replace(/,/g, '').replace(/<b>/g, '').replace(/<\/b>/g, '');
  124. pp = GetPpFromUsername(username, score);
  125.  
  126. ppCell.innerHTML = pp;
  127. };
  128.  
  129. }
  130.  
  131. function GetPpFromUsername(username, score) {
  132. for (var i = 0; i < result.length; i++) {
  133. if (username == result[i].name) {
  134. if (score == result[i].score) {
  135. return (Math.round(result[i].pp * 100) / 100);
  136. }
  137. scoresMissing = true;
  138. return "N/U";
  139. }
  140. }
  141. scoresMissing = true;
  142. return "N/D";
  143. }
  144.  
  145. function GetScores(mapID, mapMode, callback) {
  146. GetPage("http://osustats.ppy.sh/api/beatmap/getScores/" + mapID + "/" + mapMode + "?nick="+ getCookie("last_login"), function(res) {
  147. callback(res);
  148. });
  149.  
  150. }
  151.  
  152. function RequestBeatmapUpdate(mapID, mapMode, callback) {
  153. GetPage("http://osustats.ppy.sh/api/beatmap/updateRequest/" + mapID + "/" + mapMode + "?nick="+ getCookie("last_login"), function(res) {
  154. requestedUpdate = true;
  155. res = JSON.parse(res);
  156.  
  157. if (res.status == "OK") {
  158. callback(true);
  159. } else {
  160. callback(false);
  161. SetInfoText("Request failed- try again later.");
  162. }
  163. });
  164. }
  165.  
  166. function GetPage(url, callback) {
  167. GM_xmlhttpRequest({
  168. method: "GET",
  169. url: url,
  170. synchronous: true,
  171. headers: {
  172. Referer: location.href
  173. },
  174. onload: function(resp) {
  175. callback(resp.responseText);
  176. }
  177. });
  178. }
  179.  
  180. function Countdown() {
  181. time--;
  182. SetInfoText("Missing scores detected- Update requested</br>Updating in " + time + " seconds");
  183.  
  184. if (time == 0) {
  185. clearInterval(interval);
  186. SetInfoText("Updating...");
  187. Start();
  188. }
  189. }
  190.  
  191. function SetInfoText(text) {
  192. InfoBoxRef.innerHTML = "<div style=\"font: 30px; text-align: center;\">" + text + "</div>"
  193. }
  194.  
  195. function AddppSortCheckbox() {
  196.  
  197. var checkbox = document.createElement('input');
  198. checkbox.type = "checkbox";
  199. checkbox.name = "name";
  200. checkbox.value = "value";
  201. checkbox.id = "id";
  202. checkbox.onchange=function x() {
  203. GM_setValue("Sort_by_pp", InfoBoxRef.nextElementSibling.children[0].children[4].checked);
  204. SortOsuScoresTable(GM_getValue("Sort_by_pp"));
  205. };
  206.  
  207. if(GM_getValue("Sort_by_pp"))
  208. checkbox.checked = GM_getValue("Sort_by_pp");
  209.  
  210. var label = document.createElement('label')
  211. label.htmlFor = "id";
  212. label.appendChild(document.createTextNode('Sort score table by pp'));
  213.  
  214. InfoBoxRef.nextElementSibling.children[0].appendChild(checkbox);
  215. InfoBoxRef.nextElementSibling.children[0].appendChild(label);
  216. }
  217.  
  218. function SortOsuScoresTable(sortByPp) {
  219. if(updateInProgress) return;
  220. if(sortByPp)
  221. column=3;
  222. else
  223. column=2;
  224.  
  225. function isNumeric(num){
  226. return !isNaN(num)
  227. }
  228. function RowCompareNumbers(a, b) {
  229. var aVal = parseInt(a.value);
  230. var bVal = parseInt(b.value);
  231. return (bVal - aVal);
  232. }
  233. var rows = scoresTableBodyRef.getElementsByTagName('tr');
  234. var rowArray = new Array();
  235. var tempValue;
  236. for (var i = 1, length = rows.length; i < length; i++) {
  237. rowArray[i] = new Object;
  238. rowArray[i].oldIndex = i;
  239. tempValue =rows[i].getElementsByTagName('td')[column].firstChild.textContent.replace(/\,/g, '');;
  240.  
  241. if(isNumeric(tempValue))
  242. rowArray[i].value = tempValue;
  243. else
  244. rowArray[i].value = "0";
  245. }
  246.  
  247. rowArray.sort(RowCompareNumbers);
  248. var newTbody = document.createElement('tbody');
  249. newTbody.appendChild(rows[0].cloneNode(true));
  250. for (var i = 0, length = rowArray.length - 1; i < length; i++) {
  251. newTbody.appendChild(rows[rowArray[i].oldIndex].cloneNode(true));
  252. }
  253. scoresTableRef.replaceChild(newTbody, scoresTableBodyRef);
  254. RefreshTableReferences();
  255. }
  256.  
  257. function getCookie(cname) {
  258. var name = cname + "=";
  259. var ca = document.cookie.split(';');
  260. for(var i=0; i<ca.length; i++) {
  261. var c = ca[i];
  262. while (c.charAt(0)==' ') c = c.substring(1);
  263. if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
  264. }
  265. return "";
  266. }
  267.  
  268. window.addEventListener('load', function() {
  269. if (Init()) {
  270. Start();
  271. }
  272. }, false);
Add Comment
Please, Sign In to add comment