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