Advertisement
Guest User

Untitled

a guest
Aug 13th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Vanguard TLH
  3. // @namespace    http://google.com
  4. // @version      0.1
  5. // @description  TLH helper for Vanguard
  6. // @author       Me
  7. // @match        https://personal.vanguard.com/us/XHTML/com/vanguard/costbasisnew/xhtml/CostBasisSummary.xhtml
  8. // @grant        none
  9. // @require      https://code.jquery.com/jquery-2.2.4.min.js
  10. // ==/UserScript==
  11.  
  12. let MAIN_TABLE_SELECTOR = "table[id^=unrealizedTabForm]";
  13. let SHOW_DETAILS_DIV_SELECTOR_FMT = "div[id*={0}][class=vg-NavboxHeadTxt]:contains('Show details')";
  14. let LOTS_TABLE_SELECTOR_FMT = "table[id*={0}]";
  15.  
  16. let TLH_BUTTON_FMT = `
  17. <div id="tlh">
  18.   <input type="button" value="TLH" id="tlh_button_{0}">
  19. </div>
  20. `;
  21.  
  22. // Adding format() method to strings
  23. String.prototype.format = function() {
  24.    var content = this;
  25.    for (var i=0; i < arguments.length; i++) {
  26.         var replacement = '{' + i + '}';
  27.         content = content.replace(replacement, arguments[i]);
  28.    }
  29.    return content;
  30. };
  31.  
  32. (function() {
  33.     'use strict';
  34.     $(document).ready(function() {
  35.         // As soon as the page loads, let's wait until the main table is loaded
  36.         waitUntilVisible(MAIN_TABLE_SELECTOR, onMainTableLoaded);
  37.     });
  38.  
  39. })();
  40.  
  41. // Callback when the main table is loaded
  42. function onMainTableLoaded(mainTable) {
  43.  
  44.     // This searchs for the Buy | Sell | Exchange div and adds a TLH at the end
  45.     let links = $("div.linkBarWrapper", mainTable);
  46.     for (var i = 0; i < links.length; i++) {
  47.         let link = links[i];
  48.         let hldId = getHldId(link);
  49.  
  50.         let tlhButton = TLH_BUTTON_FMT.format(hldId);
  51.         $(tlhButton).insertAfter(link.lastElementChild);
  52.         $("input[id=tlh_button_{0}]".format(hldId)).click(tlhButtonClick);
  53.     }
  54. }
  55.  
  56. // callback for when the user clicks on TLH
  57. function tlhButtonClick() {
  58.     let callerId = $(this).attr('id');
  59.     let hldId = callerId.replace("tlh_button_", "");
  60.  
  61.     let labelZero = $(SHOW_DETAILS_DIV_SELECTOR_FMT.format(hldId)).find('label')[0];
  62.     labelZero.click();
  63.  
  64.     console.log("tlhButtonClick with hldId: " + hldId);
  65.     waitUntilVisible(LOTS_TABLE_SELECTOR_FMT.format(hldId), onLotsLoaded);
  66. }
  67.  
  68. function onLotsLoaded(lotsTable) {
  69.     if (lotsTable == null) {
  70.         console.log("couldn't find lotsTable :(");
  71.         return;
  72.     }
  73.  
  74.     let rows = $(lotsTable).find("> tbody > tr");
  75.     var lots = [];
  76.  
  77.     for (var i = 0; i < rows.length; i++) {
  78.         let tds = $(rows[i]).find("> td");
  79.         if (tds.length == 8) {
  80.             var lot = {};
  81.  
  82.             lot.date = tds[0].textContent;
  83.             lot.quantity = tds[1].textContent;
  84.             lot.cost_per_share = sanitizeDollarAmount(tds[2].textContent);
  85.             lot.total_cost = sanitizeDollarAmount(tds[3].textContent);
  86.             lot.market_value = sanitizeDollarAmount(tds[4].textContent);
  87.             lot.short_term = sanitizeDollarAmount(tds[5].textContent);
  88.             lot.long_term = sanitizeDollarAmount(tds[6].textContent);
  89.             lot.total_gain_lost = sanitizeDollarAmount(tds[7].textContent);
  90.  
  91.             lots.push(lot);
  92.         }
  93.     }
  94.  
  95.     processLots(lots);
  96. }
  97.  
  98. function processLots(lots) {
  99.  
  100.     // Check short term
  101.     var shortTermTotal = 0.0;
  102.     for (var i = 0; i < lots.length; i++) {
  103.         let lot = lots[i];
  104.         if (isNaN(lot.short_term)) {
  105.             continue;
  106.         }
  107.  
  108.         if (lot.short_term < 0) {
  109.             shortTermTotal += parseFloat(lot.short_term);
  110.         }
  111.     }
  112.  
  113.     console.log("short term total " + shortTermTotal);
  114.  
  115.     // Check long term
  116.     var longTermTotal = 0.0;
  117.     for (var j = 0; j < lots.length; j++) {
  118.         let lot = lots[j];
  119.         if (isNaN(lot.long_term)) {
  120.             continue;
  121.         }
  122.  
  123.         if (lot.long_term < 0) {
  124.             longTermTotal += parseFloat(lot.long_term);
  125.         }
  126.     }
  127.  
  128.     console.log("Long term total " + longTermTotal);
  129.  
  130.     alert("TLH\nShort term total: " + round(shortTermTotal) + "\nLong term total: " + round(longTermTotal));
  131. }
  132.  
  133. function round(num) {
  134.     return Math.round(num * 100) / 100;
  135. }
  136.  
  137. function sanitizeDollarAmount(text) {
  138.     return text.replace("$","").replace("–", "-").replace(/\s/g, "");
  139. }
  140.  
  141. // Helper function to wait until certain element is visible
  142. function waitUntilVisible(selector, callback) {
  143.     var maxTries = 3;
  144.  
  145.     let poller = setInterval(() => {
  146.         let el = $(selector);
  147.         let retry = maxTries === false || maxTries-- > 0;
  148.         if (retry && el.length < 1) {
  149.             return;
  150.         }
  151.  
  152.         clearInterval(poller);
  153.         callback(el[0] || null);
  154.     }, 2000);
  155. }
  156.  
  157. // Helper function to get instrument id from url
  158. function getHldId(element) {
  159.     let ahref = $('a[id^=unrealized]', element)[0];
  160.     let httpLink = ahref.getAttribute('href');
  161.     let splits = httpLink.split("=");
  162.  
  163.     return splits[splits.length-1];
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement