Guest User

Stock Ticker 1.3

a guest
Apr 15th, 2015
1,973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          Stock Ticker
  3. // @description   Makes a stock ticker appear on the home page
  4. // @author        boo [1544641]
  5. // @include       http://www.torn.com/index.php*
  6. // @include       https://www.torn.com/index.php*
  7. // @version       1.3
  8. // ==/UserScript==
  9.  
  10. $.get("stockexchange.php?step=portfolio&rfcv="+getCookie("rfc_v"), function(data) {
  11.     var jqueryDocument = $(data);
  12.     var stocks = [];
  13.     jqueryDocument.find("li.info").each(function (i) {
  14.         var currStock = {};
  15.         currStock["title"] = $(this).parent().find(".logo img").prop("title");
  16.         currStock["changeSign"] = ($(this).find(".change.down").length > 0 ? "down" : ($(this).find(".change.up").length > 0 ? "up" : "no change"));        
  17.         $(this).find(".price-wrap").each(function (i) {
  18.             var firstRow = $(this).find(".first-row");
  19.             var secondRow = $(this).find(".second-row");
  20.             var fieldName = firstRow.find("span.bold").text().trim().slice(0, -1);
  21.             firstRow.children().remove();
  22.             currStock[fieldName] = firstRow.text().trim();
  23.             if (secondRow.find(".value").length > 0) {
  24.                 if (secondRow.find(".value").length > 0) {
  25.                     var fieldName = secondRow.find(".prop-wrap > .bold").text().trim().slice(0, -1);
  26.                     currStock[fieldName] = secondRow.find(".value").text().trim();
  27.                 } else {
  28.                     var fieldName = secondRow.find("span").text().trim().slice(0, -1);
  29.                     secondRow.children().remove();
  30.                     currStock[fieldName] = secondRow.text().trim();
  31.                 }
  32.             }
  33.         });
  34.         stocks.push(currStock);
  35.     });
  36.     displayStockData(stocks);
  37. });
  38.  
  39. function displayStockData (stockArray) {
  40.     if (stockArray.length > 0) {
  41.         var listContainer = document.createElement("ul");
  42.         for (var i in stockArray) {
  43.             var stock = stockArray[i];
  44.             var li = document.createElement("li");
  45.             var percent = stock['Change'].split("(")[1].slice(0,-3);
  46.             var color = "";
  47.             if (stock['changeSign']=='down') { var color = '#D83500'; }
  48.             if (stock['changeSign']=='up') { var color = '#678C00'; }
  49.             if (percent==0) { var color = '#A57B00'; stock['changeSign'] = 'no change'; }
  50.             li.innerHTML = "<strong>"+stock['title']+"</strong> (<strong>"+abbrNum(parseInt(stock["Shares"].replace(/,/g,"")),2)+"</strong> shares <strong><span style='color:"+color+"'>"+percent+"%<span></strong> "+stock['changeSign']+")";
  51.             listContainer.appendChild(li);
  52.             listContainer.style.fontWeight = "normal";
  53.             li.style.marginLeft = "15px";
  54.             li.style.display = "inline-block";
  55.         }
  56.         listContainer.style.display = "inline-block";
  57.         var marquee = document.createElement("marquee");
  58.         marquee.appendChild(listContainer);
  59.         marquee.style.height = "18px";
  60.         marquee.style.lineHeight = "18px";
  61.         listContainer.style.height = "100%";
  62.         marquee.onmouseover = function () { marquee.scrollAmount = "2"; };
  63.         marquee.onmouseout = function () { marquee.scrollAmount = "6"; };
  64.         marquee.onclick = function () { window.location = "/stockexchange.php?step=portfolio"; };
  65.         marquee.style.cursor = "pointer";
  66.         var hr = document.createElement("hr");
  67.         hr.className = "page-head-delimiter";
  68.         $(".page-head-delimiter").first().after(hr).after(marquee);
  69.     }
  70. }
  71.  
  72. // abbrNum function from http://stackoverflow.com/questions/2685911/is-there-a-way-to-round-numbers-into-a-reader-friendly-format-e-g-1-1k
  73. function abbrNum(number, decPlaces) {
  74.     // 2 decimal places => 100, 3 => 1000, etc
  75.     decPlaces = Math.pow(10,decPlaces);
  76.     // Enumerate number abbreviations
  77.     var abbrev = [ "k", "m", "b", "t" ];
  78.     // Go through the array backwards, so we do the largest first
  79.     for (var i=abbrev.length-1; i>=0; i--) {
  80.         // Convert array index to "1000", "1000000", etc
  81.         var size = Math.pow(10,(i+1)*3);
  82.         // If the number is bigger or equal do the abbreviation
  83.         if(size <= number) {
  84.             // Here, we multiply by decPlaces, round, and then divide by decPlaces.
  85.             // This gives us nice rounding to a particular decimal place.
  86.             number = Math.round(number*decPlaces/size)/decPlaces;
  87.             // Handle special case where we round up to the next abbreviation
  88.             if((number == 1000) && (i < abbrev.length - 1)) {
  89.                 number = 1;
  90.                 i++;
  91.             }
  92.             // Add the letter for the abbreviation
  93.             number += abbrev[i];
  94.             // We are done... stop
  95.             break;
  96.         }
  97.     }
  98.     return number;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment