Last active
April 21, 2018 01:19
-
-
Save ewasion/8f3875baee474607bda6566126c45c92 to your computer and use it in GitHub Desktop.
AB stats change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name AB stats change | |
// @namespace https://github.com/ewasion | |
// @version 0.1.1 | |
// @description Displays the changes in stats on AB | |
// @author NullPhantom, Eva | |
// @homepage https://gist.github.com/ewasion/8f3875baee474607bda6566126c45c92 | |
// @icon https://animebytes.tv/favicon.ico | |
// @updateURL https://gist.github.com/ewasion/8f3875baee474607bda6566126c45c92/raw/ab-stats-change.user.js | |
// @downloadURL https://gist.github.com/ewasion/8f3875baee474607bda6566126c45c92/raw/ab-stats-change.user.js | |
// @grant none | |
// @match https://animebytes.tv/* | |
// @license GPL-3.0 | |
// @run-at document-end | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var currentStats = {}; | |
var statspans = document.querySelectorAll('#stats_menu li > span'); | |
currentStats.up = parseStats(statspans[0].childNodes[1].textContent); | |
currentStats.down = parseStats(statspans[1].childNodes[1].textContent); | |
currentStats.ratio = parseFloat(document.querySelector('#stats_menu > a > span').textContent); | |
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); | |
window.localStorage.lastStats = JSON.stringify(currentStats); | |
var change = {up:currentStats.up-oldStats.up, down:currentStats.down-oldStats.down, ratio:Math.round((currentStats.ratio-oldStats.ratio)*100)/100}; | |
if(change.up != 0 || change.down != 0 || change.ratio != 0) | |
alert('Up: '+renderStats(change.up)+', Down: '+renderStats(change.down)+', Buffer: '+renderStats(change.up-change.down)+', Ratio: '+change.ratio); | |
})(); | |
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', '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', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']; | |
var amount = parseFloat(string); | |
if(string.indexOf(suffixes[1]) != -1) | |
amount = amount*Math.pow(2, 10); | |
else if(string.indexOf(suffixes[2]) != -1) | |
amount = amount*Math.pow(2, 20); | |
else if(string.indexOf(suffixes[3]) != -1) | |
amount = amount*Math.pow(2, 30); | |
else if(string.indexOf(suffixes[4]) != -1) | |
amount = amount*Math.pow(2, 40); | |
else if(string.indexOf(suffixes[5]) != -1) | |
amount = amount*Math.pow(2, 50); | |
return Math.round(amount); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment