Follow Player

Skip replays that don't have a selected player

当前为 2014-05-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Follow Player
  3. // @namespace pbr
  4. // @description Skip replays that don't have a selected player
  5. // @include http://goallineblitz.com/game/replay.pl?game_id=*&pbp_id=*
  6. // @include http://glb.warriorgeneral.com/game/replay.pl?game_id=*&pbp_id=*
  7. // @copyright 2010, pabst
  8. // @license (CC) Attribution Share Alike; http://creativecommons.org/licenses/by-sa/3.0/
  9. // @version 13.12.29
  10. // @require http://userscripts.org/scripts/source/54630.user.js
  11. // ==/UserScript==
  12.  
  13. var scriptName = "Follow Player";
  14. var scriptVersion = "13.12.29";
  15. var scriptWebpage = "http://userscripts.org/scripts/show/88782";
  16.  
  17. var lastSelected = null;
  18. var playerIds = new Array();
  19. var playerNames = new Array();
  20. var playerReplays = new Array();
  21. var plays = null;
  22.  
  23. window.setTimeout( function() {
  24. init(true);
  25. }, 1000);
  26.  
  27.  
  28. function activate(e) {
  29. console.log("activate follow");
  30. lock();
  31.  
  32. pbp();
  33.  
  34. if (document.getElementById("player_select") == null) {
  35. createControls();
  36. }
  37. removePlayers();
  38. addPlayers();
  39.  
  40. unlock();
  41. }
  42.  
  43. function pbp() {
  44. var tag = document.getElementById("pbp");
  45. if (tag == null) {
  46. setTimeout(pbp, 300);
  47. }
  48. else if (tag.childNodes.length == 0) {
  49. setTimeout(pbp, 300);
  50. }
  51. else {
  52. var p = new Array();
  53. var plText = tag.innerHTML.split("pbp_id=");
  54. for (var i=1; i<plText.length; i++) {
  55. p.push(parseInt(plText[i])+"");
  56. }
  57. console.log("fp pl.len="+p.length);
  58. plays = p;
  59. }
  60. }
  61.  
  62. function createControls() {
  63. var div = document.createElement("div");
  64. div.setAttribute("id","player_controls");
  65. div.setAttribute("style","width: 650px; margin-top: 4px;");
  66.  
  67. var left = document.createElement("a");
  68. left.setAttribute("class","button left");
  69. left.innerHTML = "<span>&lt; Prev</span>";
  70. left.addEventListener("click",prevPlayer,true);
  71.  
  72. var select = document.createElement("select");
  73. select.setAttribute("id","player_select");
  74.  
  75. var right = document.createElement("a");
  76. right.setAttribute("class","button left");
  77. right.innerHTML = "<span>&gt; Next</span>";
  78. right.addEventListener("click",nextPlayer,true);
  79.  
  80. var info = document.createElement("div");
  81. info.setAttribute("id","player_replay_count");
  82. div.appendChild(left);
  83. div.appendChild(select);
  84. div.appendChild(right);
  85. div.appendChild(info);
  86.  
  87. var footer = document.getElementById("replay_footer");
  88. footer.insertBefore(div,footer.childNodes[5]);
  89. }
  90.  
  91. function removePlayers() {
  92. while (document.getElementById("player_select").options.length > 0) {
  93. document.getElementById("player_select").remove(0);
  94. }
  95. }
  96.  
  97. function addPlayers() {
  98. var select = document.getElementById("player_select");
  99.  
  100. for (p in unsafeWindow.players) {
  101. var pos = unsafeWindow.players[p].position;
  102. var name = unsafeWindow.players[p].name;
  103. var id = p;
  104. var option = document.createElement("option");
  105. option.text = name;
  106.  
  107. var pos = null;
  108. for (var i=0; i<select.options.length; i++) {
  109. if (select.options[i].text > option.text) {
  110. pos = select.options[i];
  111. break;
  112. }
  113. }
  114. select.add(option, pos);
  115. }
  116.  
  117. var others = false;
  118. for (var j=0; j<playerNames.length; j++) {
  119. var found = false;
  120. for (var i=0; i<select.options.length; i++) {
  121. if (select.options[i].text == playerNames[j]) {
  122. found = true;
  123. break;
  124. }
  125. }
  126. if (found == false) {
  127. if (others == false) {
  128. others = true;
  129. var option = document.createElement("option");
  130. option.text = "---------------";
  131. select.add(option, null);
  132. }
  133. var option = document.createElement("option");
  134. option.text = playerNames[j];
  135. select.add(option, null);
  136. }
  137. }
  138.  
  139. var options = select.options;
  140. for (var i=0; i<options.length; i++) {
  141. if (options[i].text == lastSelected) {
  142. select.selectedIndex = i;
  143. break;
  144. }
  145. }
  146.  
  147. }
  148.  
  149. function prevPlayer() {
  150. if (plays == null) setTimeout(prevPlayer, 500);
  151. console.log("prevPlayer: plays.length="+plays.length);
  152.  
  153. var select = document.getElementById("player_select");
  154. if (select.selectedIndex < 0) return;
  155.  
  156. var id = null;
  157. var name = select.options[select.selectedIndex].text;
  158. for (var i=0; i<playerNames.length; i++) {
  159. if (name == playerNames[i]) {
  160. id = playerIds[i];
  161. break;
  162. }
  163. }
  164. if (id == null) {
  165. for (p in unsafeWindow.players) {
  166. if (unsafeWindow.players[p].name == name) {
  167. id = p;
  168. break;
  169. }
  170. }
  171. }
  172. if (id == null) {
  173. console.log(id+") "+name+" : player not found?");
  174. return;
  175. }
  176.  
  177. var data = null;
  178. for (var i=0; i<playerIds.length; i++) {
  179. if (playerIds[i] == id) {
  180. data = playerReplays[i];
  181. playerNames[i] = name;
  182. lastSelected = name;
  183. break;
  184. }
  185. }
  186.  
  187. if (data == null) {
  188. if (id == null) return;
  189. var address = document.location.href.toString();
  190. address = address.split("&")[0];
  191. address = address.replace("replay","player_replays");
  192. address += "&player_id="+id;
  193. console.log(address);
  194.  
  195. getInetPage(address,loadPlayer,null);
  196. setTimeout(prevPlayer, 500);
  197. return;
  198. }
  199. else {
  200. var currentPlay = document.getElementById("rrplay").value;
  201. currentPlay = parseInt(currentPlay.split("pbp_id=")[1]);
  202. console.log("current play is : "+currentPlay);
  203.  
  204. for (var i=1; i<data.length; i++) {
  205. if (data[i] >= currentPlay) {
  206. var prevPlay = data[i-1];
  207. console.log("prev play is : "+prevPlay);
  208.  
  209. for (var j=0; j<plays.length; j++) {
  210. if (plays[j] == prevPlay) {
  211. var pplaybtn = document.getElementsByClassName("pplaybtn")[0];
  212. pplaybtn.setAttribute("id",j);
  213. console.log(prevPlay+" : "+currentPlay+" : "+j);
  214.  
  215. var info = document.getElementById("player_replay_count");
  216. info.innerHTML = (i)+" of "+data.length;
  217.  
  218. var evt = document.createEvent("MouseEvents");
  219. evt.initMouseEvent("click", false, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  220. pplaybtn.firstChild.dispatchEvent(evt);
  221. break;
  222. }
  223. }
  224. break;
  225. }
  226. }
  227. }
  228. }
  229.  
  230. function nextPlayer() {
  231. if (plays == null) setTimeout(nextPlayer, 500);
  232. console.log("nextPlayer: plays.length="+plays.length);
  233.  
  234. var select = document.getElementById("player_select");
  235. if (select.selectedIndex < 0) return;
  236.  
  237. var id = null;
  238. var name = select.options[select.selectedIndex].text;
  239. for (var i=0; i<playerNames.length; i++) {
  240. if (name == playerNames[i]) {
  241. id = playerIds[i];
  242. break;
  243. }
  244. }
  245. if (id == null) {
  246. for (p in unsafeWindow.players) {
  247. if (unsafeWindow.players[p].name == name) {
  248. id = p;
  249. break;
  250. }
  251. }
  252. }
  253. if (id == null) {
  254. console.log(id+") "+name+" : player not found?");
  255. return;
  256. }
  257.  
  258. var data = null;
  259. for (var i=0; i<playerIds.length; i++) {
  260. if (playerIds[i] == id) {
  261. data = playerReplays[i];
  262. playerNames[i] = name;
  263. lastSelected = name;
  264. break;
  265. }
  266. }
  267.  
  268. if (data == null) {
  269. if (id == null) return;
  270. var address = document.location.href.toString();
  271. address = address.split("&")[0];
  272. address = address.replace("replay","player_replays");
  273. address += "&player_id="+id;
  274. console.log(address);
  275.  
  276. getInetPage(address,loadPlayer,null);
  277. setTimeout(nextPlayer, 500);
  278. return;
  279. }
  280. else {
  281. var currentPlay = document.getElementById("rrplay").value;
  282. currentPlay = parseInt(currentPlay.split("pbp_id=")[1]);
  283. console.log("current play is : "+currentPlay);
  284.  
  285. for (var i=0; i<data.length; i++) {
  286. if (data[i] > currentPlay) {
  287. var nextPlay = data[i];
  288. console.log("next play is : "+nextPlay);
  289.  
  290. for (var j=0; j<plays.length; j++) {
  291. if (plays[j] == nextPlay) {
  292. var nplaybtn = document.getElementsByClassName("nplaybtn")[0];
  293. nplaybtn.setAttribute("id",j);
  294. console.log(nextPlay+" : "+currentPlay+" : "+j);
  295.  
  296. var info = document.getElementById("player_replay_count");
  297. info.innerHTML = (i+1)+" of "+data.length;
  298.  
  299. var evt = document.createEvent("MouseEvents");
  300. evt.initMouseEvent("click", false, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  301. nplaybtn.firstChild.dispatchEvent(evt);
  302. break;
  303. }
  304. }
  305. break;
  306. }
  307. }
  308. }
  309. }
  310.  
  311. function loadPlayer(address, page) {
  312. var playerId = parseInt(address.split("player_id=")[1])+"";
  313.  
  314. var pbp = new Array();
  315. var text = page.responseText.split("pbp_id=");
  316. for (var i=1; i<text.length; i++) {
  317. pbp.push(parseInt(text[i])+"");
  318. }
  319.  
  320. playerIds.push(playerId);
  321. playerReplays.push(pbp);
  322. }
  323.  
  324.  
  325.