All pastes #1016221 Raw Edit

add_buffer.js

public javascript v1 · immutable
#1016221 ·published 2008-05-12 20:46 UTC
rendered paste body
// ==UserScript==// @include http://www.norbits.net/// @include http://www.norbits.net/*// @include http://norbits.net/// @include http://norbits.net/*// ==/UserScript==(function(){    if(location.hostname.indexOf('norbits.net') == -1) {       return false;   }     function getNodes(expression, startElement) {       startElement = startElement || document;       var nodes = document.evaluate( expression, startElement, null,                                       XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );              return nodes;   }      function trim(str) {        return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');      }      function getGigaBytes(text){        var gigabytes, numbers, suffix;        text = trim(text);                if(text == 'Inf.' || text == '---') {            return 0;        }                var _tmp = text.split(' ');        numbers  = Number(_tmp[0]);        suffix   = _tmp[1];        //alert('suffix er: "' + suffix + "'")        switch(suffix){            case 'GB':                gigabytes =  numbers;                break;            case 'TB':                gigabytes = numbers*1024;                break;            case 'MB':                gigabytes = numbers*0.0009765625;                break;            default:                gigabytes = numbers;                break;                            }           return gigabytes;      }    document.addEventListener(   'DOMContentLoaded',   function (e) {           try{           var activeTorrentsNode = getNodes('//font[@color="1900D1" and text()="Akt:"]').snapshotItem(0);                  var uploadNode = getNodes('//font[@color="green" and text()="Opp:"]').snapshotItem(0);           var uploaded = getGigaBytes(uploadNode.nextSibling.textContent);//always in GB                                 var downloadNode = getNodes('//font[@color="darkred" and text()="Ned:"]').snapshotItem(0);           var downloaded = getGigaBytes(downloadNode.nextSibling.textContent);//always in GB                      var container = document.createElement('span');                      var labelContainer = document.createElement('span');           labelContainer.style.color='purple';           labelContainer.appendChild(document.createTextNode('Buffer: '));           container.appendChild(labelContainer);                      if(downloaded >= uploaded) {                container.appendChild(document.createTextNode('---'));           } else {                var buffer = Math.round((uploaded-downloaded)*100)/100;//always in GB, two decimals                container.appendChild(document.createTextNode( buffer+ ' GB'));                      }           container.appendChild(document.createTextNode('  '));           //insert before activeTorrentsNode           activeTorrentsNode.parentNode.insertBefore(container, activeTorrentsNode);                  }catch(e) {            alert('feil: ' + e)       }   },   false );  })();