Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Stock Ticker
- // @description Makes a stock ticker appear on the home page
- // @author boo [1544641]
- // @include http://www.torn.com/index.php*
- // @include https://www.torn.com/index.php*
- // @version 1.3
- // ==/UserScript==
- $.get("stockexchange.php?step=portfolio&rfcv="+getCookie("rfc_v"), function(data) {
- var jqueryDocument = $(data);
- var stocks = [];
- jqueryDocument.find("li.info").each(function (i) {
- var currStock = {};
- currStock["title"] = $(this).parent().find(".logo img").prop("title");
- currStock["changeSign"] = ($(this).find(".change.down").length > 0 ? "down" : ($(this).find(".change.up").length > 0 ? "up" : "no change"));
- $(this).find(".price-wrap").each(function (i) {
- var firstRow = $(this).find(".first-row");
- var secondRow = $(this).find(".second-row");
- var fieldName = firstRow.find("span.bold").text().trim().slice(0, -1);
- firstRow.children().remove();
- currStock[fieldName] = firstRow.text().trim();
- if (secondRow.find(".value").length > 0) {
- if (secondRow.find(".value").length > 0) {
- var fieldName = secondRow.find(".prop-wrap > .bold").text().trim().slice(0, -1);
- currStock[fieldName] = secondRow.find(".value").text().trim();
- } else {
- var fieldName = secondRow.find("span").text().trim().slice(0, -1);
- secondRow.children().remove();
- currStock[fieldName] = secondRow.text().trim();
- }
- }
- });
- stocks.push(currStock);
- });
- displayStockData(stocks);
- });
- function displayStockData (stockArray) {
- if (stockArray.length > 0) {
- var listContainer = document.createElement("ul");
- for (var i in stockArray) {
- var stock = stockArray[i];
- var li = document.createElement("li");
- var percent = stock['Change'].split("(")[1].slice(0,-3);
- var color = "";
- if (stock['changeSign']=='down') { var color = '#D83500'; }
- if (stock['changeSign']=='up') { var color = '#678C00'; }
- if (percent==0) { var color = '#A57B00'; stock['changeSign'] = 'no change'; }
- 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']+")";
- listContainer.appendChild(li);
- listContainer.style.fontWeight = "normal";
- li.style.marginLeft = "15px";
- li.style.display = "inline-block";
- }
- listContainer.style.display = "inline-block";
- var marquee = document.createElement("marquee");
- marquee.appendChild(listContainer);
- marquee.style.height = "18px";
- marquee.style.lineHeight = "18px";
- listContainer.style.height = "100%";
- marquee.onmouseover = function () { marquee.scrollAmount = "2"; };
- marquee.onmouseout = function () { marquee.scrollAmount = "6"; };
- marquee.onclick = function () { window.location = "/stockexchange.php?step=portfolio"; };
- marquee.style.cursor = "pointer";
- var hr = document.createElement("hr");
- hr.className = "page-head-delimiter";
- $(".page-head-delimiter").first().after(hr).after(marquee);
- }
- }
- // abbrNum function from http://stackoverflow.com/questions/2685911/is-there-a-way-to-round-numbers-into-a-reader-friendly-format-e-g-1-1k
- function abbrNum(number, decPlaces) {
- // 2 decimal places => 100, 3 => 1000, etc
- decPlaces = Math.pow(10,decPlaces);
- // Enumerate number abbreviations
- var abbrev = [ "k", "m", "b", "t" ];
- // Go through the array backwards, so we do the largest first
- for (var i=abbrev.length-1; i>=0; i--) {
- // Convert array index to "1000", "1000000", etc
- var size = Math.pow(10,(i+1)*3);
- // If the number is bigger or equal do the abbreviation
- if(size <= number) {
- // Here, we multiply by decPlaces, round, and then divide by decPlaces.
- // This gives us nice rounding to a particular decimal place.
- number = Math.round(number*decPlaces/size)/decPlaces;
- // Handle special case where we round up to the next abbreviation
- if((number == 1000) && (i < abbrev.length - 1)) {
- number = 1;
- i++;
- }
- // Add the letter for the abbreviation
- number += abbrev[i];
- // We are done... stop
- break;
- }
- }
- return number;
- }
Advertisement
Add Comment
Please, Sign In to add comment