Player Play Watch Or/And/Xor/Not

Only show the plays the selected players were in on a GLB play-by-play page.

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
  1. // ==UserScript==
  2. // @name Player Play Watch Or/And/Xor/Not
  3. // @namespace GLB
  4. // @include http://goallineblitz.com/game/game.pl?game_id=*&mode=pbp
  5. // @include http://glb.warriorgeneral.com/game/game.pl?game_id=*&mode=pbp
  6. // @author numone (http://userscripts.org/users/74270)
  7. // @version 13.12.29
  8. // @description Only show the plays the selected players were in on a GLB play-by-play page.
  9. // ==/UserScript==
  10.  
  11. // pabst added Or/And/Xor/Not on 7/23/2013
  12.  
  13. var CACHE = {};
  14. PlayerWatch = function(){
  15. var that = this;
  16. // div that holds everything
  17. this.mainDiv = document.createElement('DIV');
  18. var area = document.getElementById('pbp');
  19. area.insertBefore(this.mainDiv,area.firstChild);
  20. // open/close list button
  21. this.button = document.createElement('Input');
  22. this.button.setAttribute('type','button');
  23. this.button.setAttribute('value','Open Pannel');
  24. this.button.addEventListener('click',function() { that.gatherLists.apply(that); },false);
  25. this.mainDiv.appendChild(this.button);
  26. // div that holds the player's names
  27. this.innerDiv = document.createElement('DIV');
  28. this.mainDiv.appendChild(this.innerDiv);
  29. this.innerDiv.style.display = 'none';
  30. };
  31. PlayerWatch.prototype = {};
  32.  
  33. PlayerWatch.prototype.gatherLists = function(){
  34. // take care of the button and div
  35. var that = this;
  36. this.button.setAttribute('value','Close Pannel');
  37. this.button.addEventListener('click',function() { that.closeLists.apply(that); },false);
  38. this.innerDiv.style.display = 'block';
  39. if(this.isLoaded){
  40. return;
  41. }
  42. // get the team's names and links
  43. this.team1 = {};
  44. this.team2 = {};
  45. var temp = document.getElementById('scoreboard');
  46. var links = temp.getElementsByTagName('A');
  47. this.team1.name = links[0].innerHTML;
  48. this.team1.link = links[0].getAttribute('href');
  49. this.team1.teamID = this.team1.link.split('=')[1];
  50. this.team1.rosterLink = '/game/roster.pl?team_id=' + this.team1.teamID;
  51. this.team2.name = links[1].innerHTML;
  52. this.team2.link = links[1].getAttribute('href');
  53. this.team2.teamID = this.team2.link.split('=')[1];
  54. this.team2.rosterLink = '/game/roster.pl?team_id=' + this.team2.teamID;
  55. this.getAjaxRequest(this.team1.rosterLink,this.setRoster1,this.innerDiv);
  56. };
  57.  
  58. PlayerWatch.prototype.setRoster1 = function(address,xmlhttp){
  59. this.team1.results = xmlhttp;
  60. this.team1.holder = document.createElement('SPAN');
  61. this.team1.holder.innerHTML = xmlhttp.responseText;
  62. //this.mainDiv.appendChild(this.team1.holder);
  63. this.getAjaxRequest(this.team2.rosterLink,this.setRoster2,this.innerDiv);
  64. };
  65.  
  66. PlayerWatch.prototype.setRoster2 = function(address,xmlhttp){
  67. var that = this;
  68. this.isLoaded = true;
  69. this.innerDiv.innerHTML = '';
  70. // embed the results
  71. this.team2.holder = document.createElement('SPAN');
  72. this.team2.holder.innerHTML = xmlhttp.responseText;
  73. // create results table
  74. this.mainTable = document.createElement('table');
  75. var tbody = document.createElement('tbody');
  76. var tr = document.createElement('tr');
  77. tr.setAttribute('class','nonalternating_color');
  78. this.innerDiv.appendChild(this.mainTable);
  79. this.mainTable.appendChild(tbody);
  80. tbody.appendChild(tr);
  81. // create the team names
  82. var td = document.createElement('td');
  83. td.setAttribute('colSpan','2');
  84. tr.appendChild(td);
  85. td.innerHTML = this.team1.name + ':'
  86. td = document.createElement('td');
  87. td.setAttribute('colSpan','2');
  88. tr.appendChild(td);
  89. td.innerHTML = this.team2.name + ':'
  90. // team 1
  91. this.team1.rows = [];
  92. var rosTable = this.team1.holder.getElementsByTagName('TABLE')[0].parentNode;
  93. this.team1.rows = rosTable.getElementsByTagName('TR');
  94. this.team1.stat = 0;
  95. // team 2
  96. this.team2.rows = [];
  97. rosTable = this.team2.holder.getElementsByTagName('TABLE')[0].parentNode;
  98. this.team2.rows = rosTable.getElementsByTagName('TR');
  99. this.team2.stat = 0;
  100. // writes the team to the div
  101. this.printTheTeams();
  102. this.orButton = document.createElement('input');
  103. this.orButton.setAttribute('id','playerPlayWatch_Or');
  104. this.orButton.setAttribute('type','radio');
  105. this.orButton.setAttribute('name','logic');
  106. this.orButton.setAttribute('checked', true);
  107. this.orLabel = document.createElement('label');
  108. this.orLabel.innerHTML = "Or";
  109. this.orSpan = document.createElement('span');
  110. this.orSpan.appendChild(this.orButton);
  111. this.orSpan.appendChild(this.orLabel);
  112. this.innerDiv.appendChild(this.orSpan);
  113. this.andButton = document.createElement('input');
  114. this.andButton.setAttribute('id','playerPlayWatch_And');
  115. this.andButton.setAttribute('type','radio');
  116. this.andButton.setAttribute('name','logic');
  117. this.andLabel = document.createElement('label');
  118. this.andLabel.innerHTML = "And";
  119. this.andSpan = document.createElement('span');
  120. this.andSpan.appendChild(this.andButton);
  121. this.andSpan.appendChild(this.andLabel);
  122. this.innerDiv.appendChild(this.andSpan);
  123.  
  124. this.xorButton = document.createElement('input');
  125. this.xorButton.setAttribute('id','playerPlayWatch_Xor');
  126. this.xorButton.setAttribute('type','radio');
  127. this.xorButton.setAttribute('name','logic');
  128. this.xorLabel = document.createElement('label');
  129. this.xorLabel.innerHTML = "Xor";
  130. this.xorSpan = document.createElement('span');
  131. this.xorSpan.appendChild(this.xorButton);
  132. this.xorSpan.appendChild(this.xorLabel);
  133. this.innerDiv.appendChild(this.xorSpan);
  134. this.notButton = document.createElement('input');
  135. this.notButton.setAttribute('id','playerPlayWatch_Not');
  136. this.notButton.setAttribute('type','radio');
  137. this.notButton.setAttribute('name','logic');
  138. this.notLabel = document.createElement('label');
  139. this.notLabel.innerHTML = "Not";
  140. this.notSpan = document.createElement('span');
  141. this.notSpan.appendChild(this.notButton);
  142. this.notSpan.appendChild(this.notLabel);
  143. this.innerDiv.appendChild(this.notSpan);
  144. // create the button to filter the results
  145. this.runButton = document.createElement('input');
  146. this.runButton.setAttribute('type','button');
  147. this.runButton.setAttribute('value','Filter Results');
  148. this.innerDiv.appendChild(this.runButton);
  149. this.runButton.addEventListener('click',function() { that.startFiltering.apply(that); },false);
  150. // create a status div
  151. this.statusDiv = document.createElement('DIV');
  152. this.innerDiv.appendChild(this.statusDiv);
  153. this.info1 = document.createElement('SPAN');
  154. this.info2 = document.createElement('SPAN');
  155. this.info3 = document.createElement('SPAN');
  156. this.statusDiv.appendChild(this.info1);
  157. this.statusDiv.appendChild(this.info2);
  158. this.statusDiv.appendChild(this.info3);
  159. };
  160.  
  161. PlayerWatch.prototype.startFiltering = function(){
  162. var checkboxs = this.mainTable.getElementsByTagName('input');
  163. var names = [];
  164. for(var i=0;i<checkboxs.length;i++){
  165. if(checkboxs[i].checked){
  166. names.push(checkboxs[i].getAttribute('playerName'));
  167. }
  168. }
  169. if(names.length == 0){
  170. alert('No Names Checked');
  171. return;
  172. }
  173. //this.runButton.setAttribute('disabled',true);
  174. // for(var i=0;i<names.length;i++){
  175. // console.log('checked: ' + names[i]);
  176. // }
  177. if(!this.cached){
  178. this.names = names;
  179. this.cachePlays(names);
  180. }else{
  181. this.filterRows(names);
  182. }
  183. };
  184.  
  185. PlayerWatch.prototype.cachePlays = function(names){
  186. var that = this;
  187. this.hasMonsterkill = document.getElementById('statsDiv') ? true : false;
  188. console.log('monsterkill: ' + this.hasMonsterkill);
  189. var table = document.getElementById('play_by_play_table');
  190. var rows = table.getElementsByTagName('TR');
  191. // remove rows that aren't needed and count the total rows needed
  192. var useRows = [];
  193. for(var i=0;i<rows.length;i++){
  194. var c = " " + rows[i].className + " ";
  195. // it is a play
  196. if(c.indexOf(" pbp_play_row ") != -1 || c.indexOf(" pbp_play_row_scoring ") != -1 || c.indexOf(" pbp_play_row_turnover ") != -1){
  197. //console.log('row innerhtml: ' + rows[i].innerHTML);
  198. var link = rows[i].lastChild.previousSibling.firstChild.href;
  199. if(this.hasMonsterkill && link){
  200. if(this.isTimeout(rows[i])){
  201. useRows.push(rows[i]);
  202. continue;
  203. }else{
  204. useRows.push(rows[i]);
  205. i = i + 2;
  206. continue;
  207. }
  208. }else{ // replays only take up 1 row with MK
  209. useRows.push(rows[i]);
  210. continue;
  211. }
  212. }
  213. }
  214. this.totalPlays = useRows.length;
  215. this.playNum = 0;
  216. this.info1.innerHTML = 'Cacheing play ';
  217. this.info2.innerHTML = this.playNum;
  218. this.info3.innerHTML = ' of ' + this.totalPlays;
  219. for(var i=0;i<useRows.length;i++){
  220. var link = useRows[i].lastChild.previousSibling.firstChild.href;
  221. if(link){
  222. this.cachePlay(link);
  223. }else{
  224. this.postCachePlay();
  225. }
  226. }
  227. };
  228.  
  229. PlayerWatch.prototype.isTimeout = function(row){
  230. var td = getElementsByClassName('pbp_play',row)[0];
  231. //console.log('td innerhtml: ' + td.innerHTML);
  232. if(td.innerHTML == 'Offensive Timeout Called: ' + this.team1.name ||
  233. td.innerHTML == 'Offensive Timeout Called: ' + this.team2.name ||
  234. td.innerHTML == this.team1.name + ' calls timeout' ||
  235. td.innerHTML == this.team2.name + ' calls timeout' ||
  236. td.innerHTML == 'Defensive Timeout Called: ' + this.team1.name ||
  237. td.innerHTML == 'Defensive Timeout Called: ' + this.team2.name){
  238. return true;
  239. }
  240. return false;
  241. };
  242.  
  243. PlayerWatch.prototype.cachePlay = function(link){
  244. this.getAjaxRequest(link,this.postCachePlay);
  245. };
  246.  
  247. PlayerWatch.prototype.postCachePlay = function(link,xmlhttp){
  248. this.playNum++;
  249. this.info2.innerHTML = this.playNum;
  250. if(this.playNum == this.totalPlays){
  251. this.cached = true;
  252. this.filterRows(this.names);
  253. }
  254. };
  255.  
  256. PlayerWatch.prototype.filterRows = function(names){
  257. var that = this;
  258. this.hasMonsterkill = document.getElementById('statsDiv') ? true : false;
  259. var table = document.getElementById('play_by_play_table');
  260. var rows = table.getElementsByTagName('TR');
  261. // remove rows that aren't needed and count the total rows needed
  262. var useRows = [];
  263. for(var i=0;i<rows.length;i++){
  264. var c = " " + rows[i].className + " ";
  265. // it is a play
  266. if(c.indexOf(" pbp_play_row ") != -1 || c.indexOf(" pbp_play_row_scoring ") != -1 || c.indexOf(" pbp_play_row_turnover ") != -1){
  267. var link = rows[i].lastChild.previousSibling.firstChild;
  268. link.target = '_blank';
  269. link = link.href;
  270. if(this.hasMonsterkill && link){
  271. if(this.isTimeout(rows[i])){
  272. useRows.push(rows[i]);
  273. continue;
  274. }else{
  275. useRows.push(rows[i]);
  276. i = i + 2;
  277. continue;
  278. }
  279. }else{ // replays only take up 1 row with MK
  280. useRows.push(rows[i]);
  281. continue;
  282. }
  283. }
  284. }
  285. this.totalRows = useRows.length;
  286. this.currPlay = 0;
  287. this.info1.innerHTML = 'Processing play ';
  288. this.info2.innerHTML = this.currPlay;
  289. this.info3.innerHTML = ' of ' + this.totalRows;
  290.  
  291. for(var i=0;i<useRows.length;i++){
  292. if(useRows[i]){
  293. var link = useRows[i].lastChild.previousSibling.firstChild.href;
  294. if(!link){
  295. this.filterFGPlays(useRows[i],names);
  296. }else{
  297. this.runFilter(link,useRows[i],names);
  298. }
  299. }
  300. }
  301. };
  302.  
  303. PlayerWatch.prototype.filterFGPlays = function(row,names){
  304. var str = row.getElementsByTagName('TD')[3].innerHTML;
  305. for(var i=0;i<names.length;i++){
  306. if(str.indexOf(names[i]) != -1){
  307. row.style.display = '';
  308. return;
  309. }
  310. }
  311. row.style.display = 'none';
  312. this.currPlay++;
  313. this.info2.innerHTML = this.currPlay;
  314. if(this.currPlay >= this.totalRows){
  315. this.finishUpFiltering();
  316. }
  317. };
  318.  
  319. // nextSibling can include line breaks in FF, this function will ignore them
  320. PlayerWatch.prototype.nextSibling = function(node){
  321. var endBrother = node.nextSibling;
  322. while(endBrother.nodeType != 1){
  323. if(!endBrother.nextSibling){
  324. console.log('%o',node);
  325. }
  326. endBrother = endBrother.nextSibling;
  327. }
  328. return endBrother;
  329. };
  330.  
  331. PlayerWatch.prototype.runFilter = function(link,row,names){
  332. var that = this;
  333. if(this.isTimeout(row)){
  334. row.style.display = 'none';
  335. this.currPlay++;
  336. this.info2.innerHTML = this.currPlay;
  337. if(this.currPlay >= this.totalRows){
  338. this.finishUpFiltering();
  339. }
  340. return;
  341. }
  342. this.getAjaxRequest(link,function(theLink,xmlhttp){
  343. if(that.processPlay(names,xmlhttp)){
  344. row.style.display = '';
  345. if(that.hasMonsterkill){
  346. row = that.nextSibling(row);
  347. row.style.display = '';
  348. row = that.nextSibling(row);
  349. row.style.display = '';
  350. }
  351. }else{
  352. row.style.display = 'none';
  353. if(that.hasMonsterkill){
  354. row = that.nextSibling(row);
  355. row.style.display = 'none';
  356. row = that.nextSibling(row);
  357. row.style.display = 'none';
  358. }
  359. }
  360. that.currPlay++;
  361. that.info2.innerHTML = that.currPlay;
  362. if(that.currPlay >= that.totalRows){
  363. that.finishUpFiltering();
  364. }
  365. });
  366. };
  367.  
  368. PlayerWatch.prototype.finishUpFiltering = function(){
  369. var that = this;
  370. window.setTimeout(function(){
  371. that.info1.innerHTML = '';
  372. that.info2.innerHTML = '';
  373. that.info3.innerHTML = '';
  374. },250);
  375. //this.runButton.setAttribute('disabled',false);
  376. };
  377.  
  378. PlayerWatch.prototype.processPlay = function(names,result){
  379. var span = document.createElement('SPAN');
  380. span.innerHTML = result.responseText;
  381. var replayNames = getElementsByClassName('player_name',span);
  382. //console.log('replayNames length: ' + replayNames.length);
  383. var pbpNames = [];
  384. for (var k=0; k<replayNames.length; k++) {
  385. pbpNames.push(replayNames[k].innerHTML);
  386. }
  387. if (document.getElementById("playerPlayWatch_Or").checked) {
  388. for (var k=0; k<names.length; k++){
  389. var idx = pbpNames.indexOf(names[k]);
  390. if (idx != -1) return true;
  391. }
  392. return false;
  393. }
  394. if (document.getElementById("playerPlayWatch_Xor").checked) {
  395. var result = 0;
  396. for (var k=0; k<names.length; k++){
  397. var idx = pbpNames.indexOf(names[k]);
  398. if (idx != -1) result++;
  399. if (result > 1) return false;
  400. }
  401. return (result == 1);
  402. }
  403. if (document.getElementById("playerPlayWatch_And").checked) {
  404. var result = 0;
  405. for (var k=0; k<names.length; k++){
  406. var idx = pbpNames.indexOf(names[k]);
  407. if (idx == -1) return false;
  408. }
  409. return true;
  410. }
  411.  
  412. if (document.getElementById("playerPlayWatch_Not").checked) {
  413. for (var k=0; k<names.length; k++){
  414. var idx = pbpNames.indexOf(names[k]);
  415. if (idx != -1) return false;
  416. }
  417. return true;
  418. }
  419. return false;
  420. };
  421.  
  422. PlayerWatch.prototype.printTheTeams = function(){
  423. // see which team has the most players
  424. var count = this.team1.rows.length > this.team2.rows.length ? this.team1.rows.length : this.team2.rows.length;
  425. for(var i=0;i<count;i++){
  426. // create the row
  427. var tr = document.createElement('TR');
  428. if(i % 2 == 0){
  429. tr.setAttribute('class','alternating_color1');
  430. }else{
  431. tr.setAttribute('class','alternating_color2');
  432. }
  433. this.mainTable.appendChild(tr);
  434. if(this.team1.rows.length > i){// if this row exists for this team
  435. if(this.team1.rows[i].getElementsByTagName('A').length > 0){// see if it was a player or header
  436. var aID = this.team1.rows[i].getElementsByTagName('A').length > 2 ? 2 : 0;
  437. var td = document.createElement('TD');
  438. tr.appendChild(td);
  439. var checkbox = document.createElement('input');
  440. checkbox.setAttribute('type','checkbox');
  441. checkbox.setAttribute('playerName',this.team1.rows[i].getElementsByTagName('A')[aID].innerHTML);
  442. td.appendChild(checkbox);
  443. td = document.createElement('TD');
  444. tr.appendChild(td);
  445. td.innerHTML = this.team1.rows[i].getElementsByTagName('A')[aID].innerHTML;
  446. var tds = getElementsByClassName('player_position',this.team1.rows[i]);
  447. td.innerHTML += ' (' + tds[0].firstChild.innerHTML.replace('<div></div>','') + ')';
  448. }else{
  449. var td = document.createElement('TD');
  450. td.setAttribute('colSpan','2');
  451. tr.appendChild(td);
  452. if(this.team1.stat == 0){
  453. td.innerHTML = 'Offense';
  454. this.team1.stat++;
  455. }else if(this.team1.stat == 1){
  456. td.innerHTML = 'Defense';
  457. this.team1.stat++;
  458. }else if(this.team1.stat == 2){
  459. td.innerHTML = 'Kicker';
  460. this.team1.stat++;
  461. }
  462. }
  463. }else{
  464. var td = document.createElement('TD');
  465. td.setAttribute('colSpan','2');
  466. tr.appendChild(td);
  467. }
  468. if(this.team2.rows.length > i){
  469. if(this.team2.rows[i].getElementsByTagName('A').length > 0){
  470. var aID = this.team2.rows[i].getElementsByTagName('A').length > 2 ? 2 : 0;
  471. var td = document.createElement('TD');
  472. tr.appendChild(td);
  473. var checkbox = document.createElement('input');
  474. checkbox.setAttribute('type','checkbox');
  475. checkbox.setAttribute('playerName',this.team2.rows[i].getElementsByTagName('A')[aID].innerHTML);
  476. td.appendChild(checkbox);
  477. td = document.createElement('TD');
  478. tr.appendChild(td);
  479. td.innerHTML = this.team2.rows[i].getElementsByTagName('A')[aID].innerHTML;
  480. var tds = getElementsByClassName('player_position',this.team2.rows[i]);
  481. td.innerHTML += ' (' + tds[0].firstChild.innerHTML.replace('<div></div>','') + ')';
  482. }else{
  483. var td = document.createElement('TD');
  484. td.setAttribute('colSpan','2');
  485. tr.appendChild(td);
  486. if(this.team2.stat == 0){
  487. td.innerHTML = 'Offense';
  488. this.team2.stat++;
  489. }else if(this.team2.stat == 1){
  490. td.innerHTML = 'Defense';
  491. this.team2.stat++;
  492. }else if(this.team2.stat == 2){
  493. td.innerHTML = 'Kicker';
  494. this.team2.stat++;
  495. }
  496. }
  497. }else{
  498. var td = document.createElement('TD');
  499. td.setAttribute('colSpan','2');
  500. tr.appendChild(td);
  501. }
  502. }
  503. };
  504.  
  505. PlayerWatch.prototype.closeLists = function(){
  506. var that = this;
  507. this.innerDiv.style.display = 'none';
  508. this.button.setAttribute('value','Open Pannel');
  509. this.button.addEventListener('click',function() { that.gatherLists.apply(that); },false);
  510. };
  511.  
  512. PlayerWatch.prototype.mergeArrays = function(oldArray,newArray){
  513. for(var i=0;i<newArray.length;i++){
  514. oldArray.push(newArray[i]);
  515. }
  516. return oldArray;
  517. };
  518.  
  519. PlayerWatch.prototype.getAjaxRequest = function(address,callback,loadingDiv){
  520. var that = this;
  521. if(loadingDiv){
  522. loadingDiv.innerHTML = 'Loading...';
  523. }
  524. if(!CACHE[address]){
  525. var xmlhttp = new XMLHttpRequest();
  526. xmlhttp.open( 'GET', address, true );
  527.  
  528. xmlhttp.onload = function() {
  529. if (this.status != 200) {
  530. alert("Error loading a page: Error "+this.status+" loading "+address);
  531. }else {
  532. console.log("loaded: "+address);
  533. CACHE[address] = this;
  534. if(callback){
  535. callback.apply(that,[address,this]);
  536. }
  537. }
  538. };
  539.  
  540. xmlhttp.send(null);
  541. return xmlhttp;
  542. }else{
  543. console.log("loaded from cache: "+address);
  544. if(callback){
  545. callback.apply(that,[address,CACHE[address]]);
  546. }
  547. }
  548. };
  549.  
  550. function getElementsByClassName(classname, par){
  551. var a=[];
  552. var re = new RegExp('\\b' + classname + '\\b');
  553. var els = par.getElementsByTagName("*");
  554. for(var i=0,j=els.length; i<j; i++){
  555. if(re.test(els[i].className)){
  556. a.push(els[i]);
  557. }
  558. }
  559. return a;
  560. };
  561.  
  562. window.setTimeout(function() { new PlayerWatch(); },0);