PTH stats since last

Displays the changes in stats on PTH and PTP

目前为 2016-12-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name PTH stats since last
  3. // @version 0.8
  4. // @description Displays the changes in stats on PTH and PTP
  5. // @author Chameleon
  6. // @include http*://*passtheheadphones.me/*
  7. // @include http*://*passthepopcorn.me/*
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @namespace https://greasyfork.org/users/87476
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. if((window.location.href.indexOf("threadid=1781") != -1 && window.location.host.indexOf('headphones') != -1) || (window.location.href.indexOf("threadid=30532") != -1 && window.location.host.indexOf('popcorn') != -1))
  17. showSettings();
  18.  
  19. var currentStats = {};
  20. var statspans = document.getElementById('userinfo_stats').querySelectorAll('span');
  21. currentStats.up = parseStats(statspans[0].textContent);
  22. currentStats.down = parseStats(statspans[1].textContent);
  23. currentStats.ratio = parseFloat(statspans[2].textContent);
  24. if(isNaN(currentStats.ratio))
  25. currentStats.ratio = 0;
  26. currentStats.time=(new Date())*1;
  27.  
  28. var oldStats = window.localStorage.lastStats;
  29.  
  30. if(!oldStats)
  31. oldStats = {up:currentStats.up, down:currentStats.down, ratio:currentStats.ratio};
  32. else
  33. oldStats = JSON.parse(oldStats);
  34.  
  35.  
  36. var settings = getSettings();
  37.  
  38. if(settings.persistTime && oldStats.time)
  39. {
  40. var difTime = (new Date())-oldStats.time;
  41. if(difTime > settings.persistTime*60000)
  42. window.localStorage.lastStats = JSON.stringify(currentStats);
  43. }
  44. else
  45. window.localStorage.lastStats = JSON.stringify(currentStats);
  46.  
  47. var li=false;
  48. if(settings.showBuffer)
  49. {
  50. li=document.createElement('li');
  51. if(window.location.host.indexOf('popcorn') != -1)
  52. li.setAttribute('class', 'user-info-bar__item');
  53. var before=document.getElementById('stats_ratio');
  54. before.parentNode.insertBefore(li, before);
  55. li.innerHTML='Buffer: <span class="stat">'+renderStats((currentStats.up/1.05)-currentStats.down)+'</span>';
  56. }
  57.  
  58. var change = {up:currentStats.up-oldStats.up, down:currentStats.down-oldStats.down, ratio:Math.round((currentStats.ratio-oldStats.ratio)*100)/100};
  59. if(settings.profileOnly && window.location.href.indexOf(document.getElementById('nav_userinfo').getElementsByTagName('a')[0].href) == -1)
  60. return;
  61. if(change.up != 0 || settings.noChange)
  62. statspans[0].innerHTML += ' ('+renderStats(change.up)+')';
  63. if(change.down != 0 || settings.noChange)
  64. statspans[1].innerHTML += ' ('+renderStats(change.down)+')';
  65. if((change.up != 0 || change.down != 0 || settings.noChange) && settings.showBuffer)
  66. {
  67. var span=li.getElementsByTagName('span')[0];
  68. span.innerHTML += ' ('+renderStats((change.up/1.05)-change.down)+')</span>';
  69. }
  70. if(change.ratio != 0 || settings.noChange)
  71. statspans[2].innerHTML += ' ('+change.ratio+')';
  72.  
  73. if(settings.alert && (change.up != 0 || change.down != 0 || change.ratio != 0))
  74. alert('Up: '+renderStats(change.up)+', Down: '+renderStats(change.down)+', Buffer: '+renderStats((change.up/1.05)-change.down)+', Ratio: '+change.ratio);
  75. })();
  76.  
  77. function showSettings()
  78. {
  79. var before = document.getElementsByClassName('forum_post')[0];
  80. var div = document.createElement('div');
  81. before.parentNode.insertBefore(div, before);
  82. div.setAttribute('style', 'width: 100%; text-align: center; padding-bottom: 10px;');
  83. div.setAttribute('class', 'box');
  84. div.innerHTML = '<h2>PTH stats since last Settings</h2><br />';
  85. var settings = getSettings();
  86.  
  87. var a=document.createElement('a');
  88. a.href='javascript:void(0);';
  89. a.innerHTML = 'Show on no change: '+(settings.noChange ? 'On' : 'Off');
  90. a.addEventListener('click', changeSetting.bind(undefined, a), false);
  91. div.appendChild(a);
  92. div.appendChild(document.createElement('br'));
  93.  
  94. var a=document.createElement('a');
  95. a.href='javascript:void(0);';
  96. a.innerHTML = 'Show on profile only: '+(settings.profileOnly ? 'On' : 'Off');
  97. a.addEventListener('click', changeSetting.bind(undefined, a), false);
  98. div.appendChild(a);
  99. div.appendChild(document.createElement('br'));
  100.  
  101. var a=document.createElement('a');
  102. a.href='javascript:void(0);';
  103. a.innerHTML = 'Show buffer: '+(settings.showBuffer ? 'On' : 'Off');
  104. a.addEventListener('click', changeSetting.bind(undefined, a), false);
  105. div.appendChild(a);
  106. div.appendChild(document.createElement('br'));
  107.  
  108. var a=document.createElement('a');
  109. a.href='javascript:void(0);';
  110. a.innerHTML = 'Alert on change: '+(settings.alert ? 'On' : 'Off');
  111. a.addEventListener('click', changeSetting.bind(undefined, a), false);
  112. div.appendChild(a);
  113. div.appendChild(document.createElement('br'));
  114. var input=document.createElement('input');
  115. input.setAttribute('placeholder', 'Persist Time');
  116. input.type='number';
  117. input.value = settings.persistTime ? settings.persistTime:'';
  118. div.appendChild(input);
  119. input.addEventListener('change', changeInput.bind(undefined, input), false);
  120. div.appendChild(document.createElement('br'));
  121.  
  122. var a=document.createElement('a');
  123. a.href='javascript:void(0);';
  124. a.innerHTML = 'Save';
  125. div.appendChild(a);
  126. div.appendChild(document.createElement('br'));
  127. }
  128.  
  129. function changeInput(input)
  130. {
  131. var settings = getSettings();
  132. settings.persistTime=input.value;
  133. GM_setValue('lastStatsSettings', JSON.stringify(settings));
  134. }
  135.  
  136. function changeSetting(a)
  137. {
  138. var on=false;
  139. if(a.innerHTML.indexOf('On') == -1)
  140. {
  141. on=true;
  142. a.innerHTML = a.innerHTML.replace('Off', 'On');
  143. }
  144. else
  145. {
  146. a.innerHTML = a.innerHTML.replace('On', 'Off');
  147. }
  148.  
  149. var settings = getSettings();
  150. if(a.innerHTML.indexOf('no change') != -1)
  151. {
  152. settings.noChange = on;
  153. }
  154. else if(a.innerHTML.indexOf('profile only') != -1)
  155. {
  156. settings.profileOnly = on;
  157. }
  158. else if(a.innerHTML.indexOf('Alert') != -1)
  159. {
  160. settings.alert = on;
  161. }
  162. else if(a.innerHTML.indexOf('Show buffer') != -1)
  163. {
  164. settings.showBuffer = on;
  165. }
  166. GM_setValue('lastStatsSettings', JSON.stringify(settings));
  167. }
  168.  
  169. function getSettings()
  170. {
  171. var settings = GM_getValue('lastStatsSettings', false);
  172. if(!settings)
  173. {
  174. settings = {noChange: false, profileOnly: false, alert: false, showBuffer: false, persistTime: ''};
  175. }
  176. else
  177. settings = JSON.parse(settings);
  178. return settings;
  179. }
  180.  
  181. function renderStats(number)
  182. {
  183. var amount = number;
  184. var pow = 0;
  185. for(var i=10; i<=50; i=i+10)
  186. {
  187. if(Math.abs(amount)/Math.pow(2, i) > 1)
  188. pow=i/10;
  189. }
  190. var suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  191. if(window.location.host.indexOf('popcorn') != -1)
  192. suffixes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
  193. return (Math.round(amount/Math.pow(2, pow*10)*100))/100+' '+suffixes[pow];
  194. }
  195.  
  196. function parseStats(string)
  197. {
  198. var suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  199. if(window.location.host.indexOf('popcorn') != -1)
  200. suffixes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
  201. var amount = parseFloat(string);
  202. if(string.indexOf(suffixes[1]) != -1)
  203. amount = amount*Math.pow(2, 10);
  204. else if(string.indexOf(suffixes[2]) != -1)
  205. amount = amount*Math.pow(2, 20);
  206. else if(string.indexOf(suffixes[3]) != -1)
  207. amount = amount*Math.pow(2, 30);
  208. else if(string.indexOf(suffixes[4]) != -1)
  209. amount = amount*Math.pow(2, 40);
  210. else if(string.indexOf(suffixes[5]) != -1)
  211. amount = amount*Math.pow(2, 50);
  212. return Math.round(amount);
  213. }