Bettor Status

Puts the betting status of the person in their avatar

当前为 2015-09-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Bettor Status
  3. // @namespace d2jsp
  4. // @description Puts the betting status of the person in their avatar
  5. // @include http://forums.d2jsp.org/*
  6. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js
  7. // @version 3.04
  8. // ==/UserScript==
  9. /**************************************
  10. ** COPYRIGHT NUMONE@D2jsp.org ******
  11. **************************************/
  12. //globals
  13. var VERSION = 3.04;
  14. var CACHE_TIME = 1000 * 60 * 60; //1000 to convert to seconds, 60 to convert to min, 60 for 60 minutes
  15. var HEADER_URL = 'https://raw.githubusercontent.com/numone/bettor-status-d2jsp/devel/lists/master.json';
  16. var PAGE_TYPE = window.location.href.match(/\/topic\.php/) ? 'thread'
  17. : window.location.href.match(/\/pm\.php\?c\=3/) ? 'pm'
  18. : window.location.href.match(/\/user\.php/) ? 'user'
  19. : window.location.href.match(/\/settings\.php/) ? 'settings'
  20. : 'UNKNOWN';
  21. var LIST = {}; // holds the list of everything
  22.  
  23. // universal fieldset
  24. $('BODY').append('<fieldset style="padding:5px;position:absolute;z-index:100;background-color:#D4E0FF;" id="bsFieldset"><legend style="background-color:#D4E0FF;border:1px solid #B0B0B0;"><span></span><img style="vertical-align:inherit;margin-left:2px;cursor:pointer;" src="images/x.gif" /></legend><div class="main"></div></fieldset>');
  25. $('#bsFieldset LEGEND IMG').click(function(){$('#bsFieldset').hide();});
  26. $('#bsFieldset').hide();
  27.  
  28.  
  29. function showPreferencesMenu(){
  30. $('BODY').css({'overflow':'hidden'});
  31. $('BODY').append('<div id="Bettor_Status_bkgrnd" style="position:absolute;top:0%;left:0%;width:100%;height:100%;background-color:black;z-index:1001;-moz-opacity:0.6;opacity:.60;filter:alpha(opacity=60)">' +
  32. '</div><div id="Bettor_Status_forgrnd" style="position:absolute;top:25%;left:25%;width:50%;height:50%;padding:16px;border:5px solid #8FBEFF;background-color:#E4EBFF;z-index:1002;overflow:auto;color:black;"></div>');
  33. var holder = $('#Bettor_Status_forgrnd');
  34. $(holder).html('<div style="font-size:12pt;"><span style="font-weight:bold;text-decoration:underline;">Bettor Status Settings:</span><span id="Bettor_Status_close" style="float:right;cursor:pointer;color:blue;text-decoration:underline;">Close</span></div>');
  35. $('#Bettor_Status_close').click(function(){
  36. $('#Bettor_Status_bkgrnd').remove();
  37. $('#Bettor_Status_forgrnd').remove();
  38. $('BODY').css({'overflow':'auto'});
  39. });
  40. // display type
  41. $(holder).append('<span style="font-weight:bold;">Status Display:</span> <input type="radio" name="dispType" value="rolled" />Combined <input type="radio" name="dispType" value="seporated" />Separated');
  42. $(holder).find('INPUT[name="dispType"][value="' + GM_getValue('BSDispType','rolled') + '"]').prop('checked',true);
  43. $(holder).find('INPUT[name="dispType"]').click(function(){
  44. GM_setValue('BSDispType',$(this).prop('value'));
  45. });
  46. // use list checkboxes
  47. $(holder).append('<div style="margin-top:15px;font-weight:bold;">Use Status From The Following Sports:</div>');
  48. for(var i=0;i<LIST.sports.length;i++){
  49. $(holder).append('<input type="checkbox" name="useSport" value="' + LIST.sports[i].title + '" /> ' + LIST.sports[i].title + '<br />');
  50. if(!(GM_getValue('BSHideSpt' + LIST.sports[i].title,false))){
  51. $(holder).find('INPUT[type="checkbox"][value="' + LIST.sports[i].title + '"]').prop('checked',true);
  52. }
  53. }
  54. $(holder).find('INPUT[type="checkbox"][name="useSport"]').click(function(){
  55. if($(this).prop('checked')){
  56. GM_setValue('BSHideSpt' + $(this).prop('value'),false);
  57. }else{
  58. GM_setValue('BSHideSpt' + $(this).prop('value'),true);
  59. }
  60. });
  61. // names last updated
  62. var timeAgo = Math.round(((new Date().getTime()) - (GM_getValue('BSExpireTime') - CACHE_TIME)) / 60000);
  63. if(GM_getValue('BSExpireTime') == '0'){
  64. $(holder).append('<br /><br /><br /><span id="Bettor_Status_refreshSpan">Names List will reload when the page is refreshed (F5)</span>');
  65. }else{
  66. $(holder).append('<br /><br /><br /><span id="Bettor_Status_refreshSpan"><span style="font-style:italic;">Names List Last Updated ' + timeAgo + ' minutes ago.</span> <a href="#" id="Bettor_Status_refresh">Reload Now</a></span>');
  67. $('#Bettor_Status_refresh').click(function(){
  68. GM_setValue('BSExpireTime','0');
  69. $('#Bettor_Status_refreshSpan').html('Will now reload when the page is refreshed (F5)');
  70. });
  71. }
  72. };
  73.  
  74. function showPreferencesLink(){
  75. //$('BODY DIV.bar DIV.barR').append('<a href="#" id="Bettor_Status_pref">Bettor Status Settings</a>');
  76. $('BODY TABLE DL DD UL.mL').append('<li><a href="#" id="Bettor_Status_pref">Bettor Status Settings</a></li>');
  77. $('#Bettor_Status_pref').click(function(){
  78. showPreferencesMenu();
  79. });
  80. };
  81.  
  82. function showStatus(nameList,nameHolders){
  83. for(var i=0;i<nameList.length;i++){
  84. var resultsArray = [];
  85. var results = {rank:-1};
  86. for(var j=0;j<LIST.sports.length;j++){
  87. var sport = LIST.sports[j];
  88. if(!(GM_getValue('BSHideSpt' + sport.title,false))){
  89. if(sport.names[nameList[i].toUpperCase()]){
  90. resultsArray.push({sport:sport.title,status:sport.names[nameList[i].toUpperCase()].status,id:j});
  91. if(results.rank < LIST.statusInfo[sport.names[nameList[i].toUpperCase()].status].rank){
  92. results = {rank:LIST.statusInfo[sport.names[nameList[i].toUpperCase()].status].rank,status:sport.names[nameList[i].toUpperCase()].status};
  93. }
  94. }else{
  95. resultsArray.push({sport:sport.title,status:'Unknown',id:j});
  96. if(results.rank < 0){
  97. results = {rank:0,status:'Unknown'};
  98. }
  99. }
  100. }
  101. }
  102. if(results.rank > -1){
  103. if(GM_getValue('BSDispType','rolled') == 'rolled'){
  104. function createLink(){
  105. $(nameHolders[i]).append('<div class="sportStatusHolder"><a href="javascript:void(0);">Bettor Status</a>: ' + colorTheStatus(results.status) + '</div>');
  106. var resArray = resultsArray;
  107. $(nameHolders[i]).find('DIV.sportStatusHolder:last A').click(function(){
  108. showMultiSportsInfo(resArray,this);
  109. });
  110. }
  111. createLink();
  112. }else{
  113. for(var j=0;j<resultsArray.length;j++){
  114. function createLinkAll(){
  115. var appendStr = '<div class="sportStatusHolder"><a href="javascript:void(0);">' + resultsArray[j].sport + '</a>: ' + colorTheStatus(resultsArray[j].status) + '</div>';
  116. $(nameHolders[i]).append(appendStr);
  117. var theID = resultsArray[j].id;
  118. $(nameHolders[i]).find('DIV.sportStatusHolder:last A').click(function(){
  119. showSingleSportInfo(theID,this);
  120. });
  121. }
  122. createLinkAll();
  123. }
  124. }
  125. }
  126. }
  127. };
  128.  
  129. function showSingleSportInfo(id,link){
  130. var offset = $(link).offset();
  131. var sportInfo = LIST.sports[id];
  132. $('#bsFieldset').css({top:offset.top,left:offset.left}).show();
  133. $('#bsFieldset LEGEND SPAN').html(sportInfo.title + ' Info');
  134. $('#bsFieldset DIV.main').html('<a href="' + sportInfo.list + '" target="_blank">Link To ' + sportInfo.title + ' List Image</a><br /><br />List Ran By:<br />');
  135. for(var i=0;i<sportInfo.listRunner.length;i++){
  136. $('#bsFieldset DIV.main').append('<a href="' + sportInfo.listRunner[i].link + '" target="_blank">' + sportInfo.listRunner[i].name + '</a><br />');
  137. }
  138. };
  139.  
  140. function showMultiSportsInfo(resultsArray,link){
  141. var offset = $(link).offset();
  142. $('#bsFieldset').css({top:offset.top,left:offset.left}).show();
  143. $('#bsFieldset LEGEND SPAN').html('Bettor Status Info');
  144. $('#bsFieldset DIV.main').html('');
  145. for(var i=0;i<resultsArray.length;i++){
  146. var t = resultsArray[i];
  147. $('#bsFieldset DIV.main').append('<div><a title="links to ' + t.sport + ' list" href="' + LIST.sports[t.id].list + '" target="_blank">' + t.sport + '</a>: ' + colorTheStatus(t.status) + ' <span style="font-size:7pt;">Ran By: </span></div>');
  148. for(var j=0;j<LIST.sports[t.id].listRunner.length;j++){
  149. var o = LIST.sports[t.id].listRunner[j];
  150. $('#bsFieldset DIV.main DIV:last SPAN:last').append(' <a href="' + o.link + '" target="_blank">' + o.name + '</a>');
  151. }
  152. }
  153. };
  154.  
  155. function colorTheStatus(status){
  156. if(LIST.statusInfo[status]){
  157. return '<span style="color:' + LIST.statusInfo[status].color + ';font-weight:bold;">' + status + '</span>';
  158. }else{
  159. return '<span style="color:gray;font-weight:bold;">' + status + '</span>';
  160. }
  161. };
  162.  
  163. function showMedList(){
  164. var forumID = parseInt(window.location.href.split('&f=')[1]);
  165. var theSport = false;
  166. for(var i=0;i<LIST.sports.length;i++){
  167. if(LIST.sports[i].medForumIDs){
  168. var medList = LIST.sports[i].medForumIDs;
  169. for(var j=0;j<medList.length;j++){
  170. if(medList[j] == forumID){
  171. theSport = LIST.sports[i];
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. if(!theSport || theSport.medList.length == 0){
  178. return;
  179. }
  180. $('BODY DIV.tbb DIV.links B').append('<a id="BSMedListLink" href="javascript:void(0);">Bettor Med List</a>');
  181. $('#BSMedListLink').click(function(){
  182. showMediators(theSport,this);
  183. });
  184. };
  185.  
  186. function showMediators(sportObj,link){
  187. var offset = $(link).offset();
  188. $('#bsFieldset').css({top:offset.top,left:offset.left}).show();
  189. $('#bsFieldset LEGEND SPAN').html('Bettor Med List');
  190. $('#bsFieldset DIV.main').html('');
  191. for(var i=0;i<sportObj.medList.length;i++){
  192. if(sportObj.medList[i].id){
  193. $('#bsFieldset DIV.main').append('<span style="font-weight:bold;">' + sportObj.medList[i].name + '</span> ( <a href="http://forums.d2jsp.org/user.php?i=' + sportObj.medList[i].id + '" target="_blank">http://forums.d2jsp.org/user.php?i=' + sportObj.medList[i].id + '</a> )<br />');
  194. }else{
  195. $('#bsFieldset DIV.main').append('<span style="font-weight:bold;">' + sportObj.medList[i].name + '</span><br />');
  196. }
  197. }
  198. };
  199.  
  200. function parsePage(){
  201. var names = [],nameHolders = [];
  202. switch(PAGE_TYPE){
  203. case 'thread':
  204. $('BODY DIV.tbb FORM[name="REPLIER"] DL DT A[href^="user.php"]').each(function(){
  205. names.push($(this).text());
  206. });
  207. $('BODY DIV.tbb FORM[name="REPLIER"] DL DD DIV.pud').each(function(){
  208. nameHolders.push(this);
  209. });
  210. break;
  211. case 'pm':
  212. $('BODY FORM[name="a"] DL.c DT A[href^="user.php"]').each(function(){
  213. names.push($(this).text());
  214. });
  215. $('BODY FORM[name="a"] DL.c DD DIV.pud').each(function(){
  216. nameHolders.push(this);
  217. });
  218. break;
  219. case 'user':
  220. $('BODY TABLE TR TD DL DT A[href="#"]').each(function(){
  221. names.push($(this).text());
  222. });
  223. $('BODY TABLE TR TD DL DD UL.bc1').each(function(){
  224. var list = document.createElement('LI');
  225. this.appendChild(list);
  226. nameHolders.push(list);
  227. });
  228. break;
  229. case 'settings':
  230. showPreferencesLink();
  231. break;
  232. }
  233.  
  234. if(names.length > 0 && nameHolders.length > 0){
  235. showStatus(names,nameHolders);
  236. }
  237. if(PAGE_TYPE == 'thread'){
  238. showMedList();
  239. }
  240. showFlushLink();
  241. };
  242.  
  243. function showFlushLink(){
  244. var timeAgo = Math.round(((new Date().getTime()) - (localStorage['BSExpireTime'] - CACHE_TIME)) / 60000);
  245. $('BODY DIV.crt.links').append(' | Bettor Status version: ' + VERSION.toFixed(2) + ', <span id="BSBottomSpan">cached for ' + timeAgo + ' minutes <a href="javascript:void(0);" id="BSBottomFlush">flush</a></span>');
  246. $('#BSBottomFlush').click(function(){
  247. GM_setValue('BSExpireTime','0');
  248. $('#BSBottomSpan').html('Will now reload when the page is refreshed (F5)');
  249. });
  250. };
  251.  
  252. function gatherSport(sequence){
  253. if(sequence + 1 > LIST.sports.length){
  254. //save to cache
  255. GM_setValue('BSExpireTime',(new Date().getTime() + CACHE_TIME) + '');
  256. GM_setValue('BSListInfo',JSON.stringify(LIST));
  257. parsePage();
  258. return;
  259. }
  260. LIST.sports[sequence].names = {};
  261. LIST.sports[sequence].medList = [];
  262. GM_xmlhttpRequest({
  263. method:'GET',
  264. url:LIST.sports[sequence].namesURL + '?' + (new Date().getTime()),
  265. headers: {
  266. 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
  267. 'Accept': 'application/atom+xml,application/xml,text/xml',
  268. },
  269. onload:function(response){
  270. parseNames(sequence,response.responseText);
  271. gatherSport(sequence + 1);
  272. },
  273. onerror:function(err){
  274. showErrorMsg('Error retrieving list ' + LIST.sports[sequence].title);
  275. }
  276. });
  277. };
  278.  
  279. function parseNames(sequence,responseText){
  280. var userStatus;
  281. responseText = responseText.replace(/\r\n|\r|\n/gi,'#EL##SL#');
  282. var split = responseText.split('#EL##SL#');
  283. for(var i=0;i<split.length;i++){
  284. var temp = split[i];
  285. if(temp.match(/###[a-zA-Z0-9\s]+###/)){
  286. //console.log('Title: ' + temp.replace(/#/g,'').trim());
  287. userStatus = temp.replace(/#/g,'').trim();
  288. }else if(temp.length > 0){
  289. if(temp.match(/[^\s]\s+\/\/[0-9]+/)){
  290. var temp2 = temp.split(/\s+\/\//);
  291. //console.log('Name: ' + temp2[0].replace(/\/\//g,'').trim() + ' with number: ' + temp2[1].trim());
  292. LIST.sports[sequence].names[temp2[0].replace(/\/\//g,'').trim().toUpperCase()] = {status:userStatus,id:temp2[1].trim()};
  293. if(userStatus == 'Mediator'){
  294. LIST.sports[sequence].medList.push({name: temp2[0].replace(/\/\//g,'').trim(), id: temp2[1].trim()});
  295. }
  296. }else{
  297. //console.log('Name: ' + temp.trim() + ' length: ' + temp.trim().length);
  298. LIST.sports[sequence].names[temp.trim().toUpperCase()] = {status:userStatus};
  299. if(userStatus == 'Mediator'){
  300. LIST.sports[sequence].medList.push({name: temp.trim()});
  301. }
  302. }
  303. }else{
  304. //console.log('seporator');
  305. // empty line (hopefully)
  306. }
  307. }
  308. };
  309.  
  310. function showErrorMsg(msg){
  311. var div = document.createElement('DIV');
  312. $(div).css({
  313. position:'absolute',
  314. backgroundColor:'yellow',
  315. width:'50%',
  316. padding:'5px',
  317. top:'95px',
  318. textAlign:'center',
  319. left:'25%',
  320. fontWeight:'bold'
  321. }).html('Bettor Status Message: ' + msg);
  322. document.body.appendChild(div);
  323. };
  324.  
  325. function retrieveCache(){//return false;
  326. var expTime = GM_getValue('BSExpireTime');
  327. if(!expTime){//first load?
  328. return false;
  329. }
  330. var nowTime = new Date().getTime();
  331. if(nowTime > expTime){ //we've expired
  332. return false;
  333. }
  334. LIST = JSON.parse(GM_getValue('BSListInfo'));
  335. parsePage();
  336. return true;
  337. };
  338.  
  339. function retrieveHeader(){
  340. GM_xmlhttpRequest({
  341. method:'GET',
  342. url:HEADER_URL + '?' + (new Date().getTime()),
  343. headers: {
  344. 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
  345. 'Accept': 'application/atom+xml,application/xml,text/xml',
  346. },
  347. onload:function(response){
  348. LIST = JSON.parse(response.responseText);
  349. if(LIST.version != VERSION){
  350. showErrorMsg('Your bettor status script is out of date, <a href="http://userscripts.org/scripts/show/50720" target="_blank">Click Here</a> to upgrade!');
  351. }
  352. gatherSport(0);
  353. },
  354. onerror:function(err){
  355. showErrorMsg('Error retrieving list header. Will used cached lists if available.');
  356. if(GM_getValue('BSListInfo')){// we have old lists, we will use that for now
  357. LIST = JSON.parse(GM_getValue('BSListInfo'));
  358. parsePage();
  359. }
  360. }
  361. });
  362. };
  363.  
  364.  
  365. retrieveCache() || retrieveHeader();