Follow Player

Skip replays that don't have a selected player

目前为 2014-05-21 提交的版本。查看 最新版本

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