Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Vanguard TLH
- // @namespace http://google.com
- // @version 0.1
- // @description TLH helper for Vanguard
- // @author Me
- // @match https://personal.vanguard.com/us/XHTML/com/vanguard/costbasisnew/xhtml/CostBasisSummary.xhtml
- // @grant none
- // @require https://code.jquery.com/jquery-2.2.4.min.js
- // ==/UserScript==
- let MAIN_TABLE_SELECTOR = "table[id^=unrealizedTabForm]";
- let SHOW_DETAILS_DIV_SELECTOR_FMT = "div[id*={0}][class=vg-NavboxHeadTxt]:contains('Show details')";
- let LOTS_TABLE_SELECTOR_FMT = "table[id*={0}]";
- let TLH_BUTTON_FMT = `
- <div id="tlh">
- <input type="button" value="TLH" id="tlh_button_{0}">
- </div>
- `;
- // Adding format() method to strings
- String.prototype.format = function() {
- var content = this;
- for (var i=0; i < arguments.length; i++) {
- var replacement = '{' + i + '}';
- content = content.replace(replacement, arguments[i]);
- }
- return content;
- };
- (function() {
- 'use strict';
- $(document).ready(function() {
- // As soon as the page loads, let's wait until the main table is loaded
- waitUntilVisible(MAIN_TABLE_SELECTOR, onMainTableLoaded);
- });
- })();
- // Callback when the main table is loaded
- function onMainTableLoaded(mainTable) {
- // This searchs for the Buy | Sell | Exchange div and adds a TLH at the end
- let links = $("div.linkBarWrapper", mainTable);
- for (var i = 0; i < links.length; i++) {
- let link = links[i];
- let hldId = getHldId(link);
- let tlhButton = TLH_BUTTON_FMT.format(hldId);
- $(tlhButton).insertAfter(link.lastElementChild);
- $("input[id=tlh_button_{0}]".format(hldId)).click(tlhButtonClick);
- }
- }
- // callback for when the user clicks on TLH
- function tlhButtonClick() {
- let callerId = $(this).attr('id');
- let hldId = callerId.replace("tlh_button_", "");
- let labelZero = $(SHOW_DETAILS_DIV_SELECTOR_FMT.format(hldId)).find('label')[0];
- labelZero.click();
- console.log("tlhButtonClick with hldId: " + hldId);
- waitUntilVisible(LOTS_TABLE_SELECTOR_FMT.format(hldId), onLotsLoaded);
- }
- function onLotsLoaded(lotsTable) {
- if (lotsTable == null) {
- console.log("couldn't find lotsTable :(");
- return;
- }
- let rows = $(lotsTable).find("> tbody > tr");
- var lots = [];
- for (var i = 0; i < rows.length; i++) {
- let tds = $(rows[i]).find("> td");
- if (tds.length == 8) {
- var lot = {};
- lot.date = tds[0].textContent;
- lot.quantity = tds[1].textContent;
- lot.cost_per_share = sanitizeDollarAmount(tds[2].textContent);
- lot.total_cost = sanitizeDollarAmount(tds[3].textContent);
- lot.market_value = sanitizeDollarAmount(tds[4].textContent);
- lot.short_term = sanitizeDollarAmount(tds[5].textContent);
- lot.long_term = sanitizeDollarAmount(tds[6].textContent);
- lot.total_gain_lost = sanitizeDollarAmount(tds[7].textContent);
- lots.push(lot);
- }
- }
- processLots(lots);
- }
- function processLots(lots) {
- // Check short term
- var shortTermTotal = 0.0;
- for (var i = 0; i < lots.length; i++) {
- let lot = lots[i];
- if (isNaN(lot.short_term)) {
- continue;
- }
- if (lot.short_term < 0) {
- shortTermTotal += parseFloat(lot.short_term);
- }
- }
- console.log("short term total " + shortTermTotal);
- // Check long term
- var longTermTotal = 0.0;
- for (var j = 0; j < lots.length; j++) {
- let lot = lots[j];
- if (isNaN(lot.long_term)) {
- continue;
- }
- if (lot.long_term < 0) {
- longTermTotal += parseFloat(lot.long_term);
- }
- }
- console.log("Long term total " + longTermTotal);
- alert("TLH\nShort term total: " + round(shortTermTotal) + "\nLong term total: " + round(longTermTotal));
- }
- function round(num) {
- return Math.round(num * 100) / 100;
- }
- function sanitizeDollarAmount(text) {
- return text.replace("$","").replace("–", "-").replace(/\s/g, "");
- }
- // Helper function to wait until certain element is visible
- function waitUntilVisible(selector, callback) {
- var maxTries = 3;
- let poller = setInterval(() => {
- let el = $(selector);
- let retry = maxTries === false || maxTries-- > 0;
- if (retry && el.length < 1) {
- return;
- }
- clearInterval(poller);
- callback(el[0] || null);
- }, 2000);
- }
- // Helper function to get instrument id from url
- function getHldId(element) {
- let ahref = $('a[id^=unrealized]', element)[0];
- let httpLink = ahref.getAttribute('href');
- let splits = httpLink.split("=");
- return splits[splits.length-1];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement