PTH stats since last

Displays the changes in stats on PTH and PTP

目前为 2017-09-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name PTH stats since last
  3. // @version 1.3
  4. // @description Displays the changes in stats on PTH and PTP
  5. // @author Chameleon
  6. // @include http*://*redacted.ch/*
  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('redacted') != -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(window.location.href.indexOf("redacted.ch") !== -1 && !isNaN(parseFloat(statspans[0].title)))
  25. {
  26. currentStats.up = parseStats(statspans[0].title);
  27. currentStats.down = parseStats(statspans[1].title);
  28. currentStats.ratio = parseFloat(statspans[2].title);
  29. }
  30. if(isNaN(currentStats.ratio))
  31. currentStats.ratio = 0;
  32. currentStats.time=(new Date())*1;
  33.  
  34. var oldStats = window.localStorage.lastStats;
  35.  
  36. if(!oldStats)
  37. oldStats = {up:currentStats.up, down:currentStats.down, ratio:currentStats.ratio};
  38. else
  39. oldStats = JSON.parse(oldStats);
  40.  
  41.  
  42. var settings = getSettings();
  43.  
  44. if(settings.persistTime && oldStats.time)
  45. {
  46. var difTime = (new Date())-oldStats.time;
  47. if(difTime > settings.persistTime*60000)
  48. window.localStorage.lastStats = JSON.stringify(currentStats);
  49. }
  50. else
  51. window.localStorage.lastStats = JSON.stringify(currentStats);
  52.  
  53. var difTime=false;
  54. if(oldStats.time)
  55. {
  56. difTime = (new Date())-oldStats.time;
  57. }
  58.  
  59. var li=false;
  60. if(settings.showBuffer)
  61. {
  62. li=document.createElement('li');
  63. if(window.location.host.indexOf('popcorn') != -1)
  64. li.setAttribute('class', 'user-info-bar__item');
  65. var before=document.getElementById('stats_ratio');
  66. before.parentNode.insertBefore(li, before);
  67. var buffer=renderStats((currentStats.up/1.05)-currentStats.down);
  68. if(window.location.host.indexOf('redacted') != -1)
  69. buffer=renderStats((currentStats.up/0.6)-currentStats.down);
  70. li.innerHTML='Buffer: <span class="stat">'+buffer+'</span>';
  71. }
  72.  
  73. var change = {up:currentStats.up-oldStats.up, down:currentStats.down-oldStats.down, ratio:Math.round((currentStats.ratio-oldStats.ratio)*100)/100};
  74. if(settings.profileOnly && window.location.href.indexOf(document.getElementById('nav_userinfo').getElementsByTagName('a')[0].href) == -1)
  75. return;
  76. if(change.up != 0 || settings.noChange)
  77. {
  78. statspans[0].innerHTML += ' <span class="stats_last up">('+renderStats(change.up)+')</span>';
  79. if(difTime)
  80. statspans[0].title = (prettyTime(difTime))+' ago';
  81. }
  82. if(change.down != 0 || settings.noChange)
  83. {
  84. statspans[1].innerHTML += ' <span class="stats_last down">('+renderStats(change.down)+')</span>';
  85. if(difTime)
  86. statspans[1].title = (prettyTime(difTime))+' ago';
  87. }
  88. if((change.up != 0 || change.down != 0 || settings.noChange) && settings.showBuffer)
  89. {
  90. var span=li.getElementsByTagName('span')[0];
  91. var buffer=renderStats((change.up/1.05)-change.down);
  92. if(window.location.host.indexOf('redacted') != -1)
  93. buffer=renderStats((change.up/0.6)-change.down);
  94. span.innerHTML += ' <span class="stats_last buffer">('+buffer+')</span>';
  95. if(difTime)
  96. span.title = (prettyTime(difTime))+' ago';
  97. }
  98. if(change.ratio != 0 || settings.noChange)
  99. {
  100. statspans[2].innerHTML += ' <span class="stats_last ratio">('+change.ratio+')</span>';
  101. if(difTime)
  102. statspans[2].title = (prettyTime(difTime))+' ago';
  103. }
  104.  
  105. if(settings.alert && (change.up != 0 || change.down != 0 || change.ratio != 0))
  106. alert('Up: '+renderStats(change.up)+', Down: '+renderStats(change.down)+', Buffer: '+renderStats((change.up/1.05)-change.down)+', Ratio: '+change.ratio);
  107. })();
  108.  
  109. function prettyTime(time)
  110. {
  111. var t=time;
  112. if(t/60000 < 1)
  113. return Math.round(time/1000)+'s';
  114. if(t/(60000*60) < 1)
  115. return Math.round(time/60000)+'m';
  116. if(t/(60000*60*24) < 1)
  117. return Math.round(time/(60000*60))+'h';
  118. return Math.round(time/(60000*60*24))+'d '+(Math.round((time%(60000*60*24))/(60000*60)))+'h';
  119. }
  120.  
  121. function showSettings()
  122. {
  123. var before = document.getElementsByClassName('forum_post')[0];
  124. var div = document.createElement('div');
  125. before.parentNode.insertBefore(div, before);
  126. div.setAttribute('style', 'width: 100%; text-align: center; padding-bottom: 10px;');
  127. div.setAttribute('class', 'box');
  128. div.innerHTML = '<h2>PTH stats since last Settings</h2><br />';
  129. var settings = getSettings();
  130.  
  131. var a=document.createElement('a');
  132. a.href='javascript:void(0);';
  133. a.innerHTML = 'Show on no change: '+(settings.noChange ? 'On' : 'Off');
  134. a.addEventListener('click', changeSetting.bind(undefined, a), false);
  135. div.appendChild(a);
  136. div.appendChild(document.createElement('br'));
  137.  
  138. var a=document.createElement('a');
  139. a.href='javascript:void(0);';
  140. a.innerHTML = 'Show on profile only: '+(settings.profileOnly ? 'On' : 'Off');
  141. a.addEventListener('click', changeSetting.bind(undefined, a), false);
  142. div.appendChild(a);
  143. div.appendChild(document.createElement('br'));
  144.  
  145. var a=document.createElement('a');
  146. a.href='javascript:void(0);';
  147. a.innerHTML = 'Show buffer: '+(settings.showBuffer ? 'On' : 'Off');
  148. a.addEventListener('click', changeSetting.bind(undefined, a), false);
  149. div.appendChild(a);
  150. div.appendChild(document.createElement('br'));
  151.  
  152. var a=document.createElement('a');
  153. a.href='javascript:void(0);';
  154. a.innerHTML = 'Alert on change: '+(settings.alert ? 'On' : 'Off');
  155. a.addEventListener('click', changeSetting.bind(undefined, a), false);
  156. div.appendChild(a);
  157. div.appendChild(document.createElement('br'));
  158.  
  159. var input=document.createElement('input');
  160. input.setAttribute('placeholder', 'Persist Time');
  161. input.type='number';
  162. input.value = settings.persistTime ? settings.persistTime:'';
  163. div.appendChild(input);
  164. input.addEventListener('change', changeInput.bind(undefined, input), false);
  165. div.appendChild(document.createElement('br'));
  166.  
  167. var a=document.createElement('a');
  168. a.href='javascript:void(0);';
  169. a.innerHTML = 'Save';
  170. div.appendChild(a);
  171. div.appendChild(document.createElement('br'));
  172. }
  173.  
  174. function changeInput(input)
  175. {
  176. var settings = getSettings();
  177. settings.persistTime=input.value;
  178. GM_setValue('lastStatsSettings', JSON.stringify(settings));
  179. }
  180.  
  181. function changeSetting(a)
  182. {
  183. var on=false;
  184. if(a.innerHTML.indexOf('On') == -1)
  185. {
  186. on=true;
  187. a.innerHTML = a.innerHTML.replace('Off', 'On');
  188. }
  189. else
  190. {
  191. a.innerHTML = a.innerHTML.replace('On', 'Off');
  192. }
  193.  
  194. var settings = getSettings();
  195. if(a.innerHTML.indexOf('no change') != -1)
  196. {
  197. settings.noChange = on;
  198. }
  199. else if(a.innerHTML.indexOf('profile only') != -1)
  200. {
  201. settings.profileOnly = on;
  202. }
  203. else if(a.innerHTML.indexOf('Alert') != -1)
  204. {
  205. settings.alert = on;
  206. }
  207. else if(a.innerHTML.indexOf('Show buffer') != -1)
  208. {
  209. settings.showBuffer = on;
  210. }
  211. GM_setValue('lastStatsSettings', JSON.stringify(settings));
  212. }
  213.  
  214. function getSettings()
  215. {
  216. var settings = GM_getValue('lastStatsSettings', false);
  217. if(!settings)
  218. {
  219. settings = {noChange: false, profileOnly: false, alert: false, showBuffer: false, persistTime: ''};
  220. }
  221. else
  222. settings = JSON.parse(settings);
  223. return settings;
  224. }
  225.  
  226. function renderStats(number)
  227. {
  228. var amount = number;
  229. var pow = 0;
  230. for(var i=10; i<=50; i=i+10)
  231. {
  232. if(Math.abs(amount)/Math.pow(2, i) > 1)
  233. pow=i/10;
  234. }
  235. var suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  236. if(window.location.host.indexOf('popcorn') != -1)
  237. suffixes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
  238. return (Math.round(amount/Math.pow(2, pow*10)*100))/100+' '+suffixes[pow];
  239. }
  240.  
  241. function parseStats(string)
  242. {
  243. string=string.replace(/,/g, '');
  244. var suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  245. if(window.location.host.indexOf('popcorn') != -1)
  246. suffixes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
  247. var amount = parseFloat(string);
  248. if(string.indexOf(suffixes[1]) != -1)
  249. amount = amount*Math.pow(2, 10);
  250. else if(string.indexOf(suffixes[2]) != -1)
  251. amount = amount*Math.pow(2, 20);
  252. else if(string.indexOf(suffixes[3]) != -1)
  253. amount = amount*Math.pow(2, 30);
  254. else if(string.indexOf(suffixes[4]) != -1)
  255. amount = amount*Math.pow(2, 40);
  256. else if(string.indexOf(suffixes[5]) != -1)
  257. amount = amount*Math.pow(2, 50);
  258. return Math.round(amount);
  259. }