|
1 | 1 | // ==UserScript== |
2 | 2 | // @name Cryptohopper Absolute Values |
3 | 3 | // @namespace https://github.com/markrickert/cryptohopper-dashboard-watchlist |
4 | | -// @version 0.2 |
| 4 | +// @version 0.3 |
5 | 5 | // @description Adds absolute value for your open positions after the percentage change. |
6 | 6 | // @author @eatsleepcoderepeat-gl, @markrickert |
7 | 7 | // @homepage https://github.com/markrickert/cryptohopper-dashboard-watchlist |
|
11 | 11 | // @grant GM_addStyle |
12 | 12 | // ==/UserScript== |
13 | 13 |
|
14 | | -(function () { |
15 | | - "use strict"; |
| 14 | +try { |
| 15 | + // Only run this code on the intended page(s) (useful when @required in a parent script) |
| 16 | + if(['/dashboard'].includes(window.location.pathname)) (function () { |
| 17 | + "use strict"; |
16 | 18 |
|
17 | | - function addStyles() { |
18 | | - GM_addStyle(` |
19 | | - span[class*="rate_"]>span { |
20 | | - margin-left: 0.5em; |
21 | | - } |
22 | | - `); |
23 | | - } |
| 19 | + function addStyles() { |
| 20 | + GM_addStyle(` |
| 21 | + span[class*="rate_"]>span { |
| 22 | + margin-left: 0.5em; |
| 23 | + } |
| 24 | + `); |
| 25 | + } |
24 | 26 |
|
25 | | - // Add absolute value to the Result column |
26 | | - function addAbsoluteResult(targets) { |
27 | | - // get unique currencyPairs: |
28 | | - var currencyTicketUpdatePairs = Object.keys(targets).map((cv, i) => |
29 | | - // Some coin pairs come back as 1INCH/USDT instead of 1INCHUSDT |
30 | | - cv.replace("/", "_") |
31 | | - ); |
32 | | - var spanSelectors = currencyTicketUpdatePairs.map((currentValue, index) => { |
33 | | - return `span.rate_${currentValue}`; |
34 | | - }); |
| 27 | + // Add absolute value to the Result column |
| 28 | + function addAbsoluteResult(targets) { |
| 29 | + // get unique currencyPairs: |
| 30 | + var currencyTicketUpdatePairs = Object.keys(targets).map((cv, i) => |
| 31 | + // Some coin pairs come back as 1INCH/USDT instead of 1INCHUSDT |
| 32 | + cv.replace("/", "_") |
| 33 | + ); |
| 34 | + var spanSelectors = currencyTicketUpdatePairs.map((currentValue, index) => { |
| 35 | + return `span.rate_${currentValue}`; |
| 36 | + }); |
35 | 37 |
|
36 | | - spanSelectors.forEach((currencyPairSelector, i) => { |
37 | | - $(`${currencyPairSelector}`).each(function () { |
38 | | - var el = $(this); // Turn it into a jquery object |
39 | | - var tx = el.text().split("(")[0]; // gets the percentage changed text |
40 | | - var change = tx.trim().replace("%", ""); // gets the raw number without % |
41 | | - let td = $(this).closest("td").prev(); // We think the previous td is the cost. |
42 | | - // Setup vars |
43 | | - let cost = false; |
44 | | - var thText; |
| 38 | + spanSelectors.forEach((currencyPairSelector, i) => { |
| 39 | + $(`${currencyPairSelector}`).each(function () { |
| 40 | + var el = $(this); // Turn it into a jquery object |
| 41 | + var tx = el.text().split("(")[0]; // gets the percentage changed text |
| 42 | + var change = tx.trim().replace("%", ""); // gets the raw number without % |
| 43 | + let td = $(this).closest("td").prev(); // We think the previous td is the cost. |
| 44 | + // Setup vars |
| 45 | + let cost = false; |
| 46 | + var thText; |
45 | 47 |
|
46 | | - // Try to find the cost of the position by searching the |
47 | | - // table headers for the word "Cost". |
48 | | - while (cost === false) { |
49 | | - thText = td |
50 | | - .closest("tbody") |
51 | | - .prev("thead") |
52 | | - .find("> tr > th:eq(" + td.index() + ")"); |
53 | | - if (thText.length) { |
54 | | - // If we found a header element |
55 | | - if (thText.text().toLowerCase() === "cost") { |
56 | | - // We know the current <td> contains the cost |
57 | | - cost = td.text().trim(); |
| 48 | + // Try to find the cost of the position by searching the |
| 49 | + // table headers for the word "Cost". |
| 50 | + while (cost === false) { |
| 51 | + thText = td |
| 52 | + .closest("tbody") |
| 53 | + .prev("thead") |
| 54 | + .find("> tr > th:eq(" + td.index() + ")"); |
| 55 | + if (thText.length) { |
| 56 | + // If we found a header element |
| 57 | + if (thText.text().toLowerCase() === "cost") { |
| 58 | + // We know the current <td> contains the cost |
| 59 | + cost = td.text().trim(); |
| 60 | + } else { |
| 61 | + // Go back one <td> and look at the header again. |
| 62 | + td = td.prev(); |
| 63 | + } |
58 | 64 | } else { |
59 | | - // Go back one <td> and look at the header again. |
60 | | - td = td.prev(); |
| 65 | + break; |
61 | 66 | } |
62 | | - } else { |
63 | | - break; |
64 | 67 | } |
65 | | - } |
66 | 68 |
|
67 | | - if (cost) { |
68 | | - try { |
69 | | - // Update the span with the change |
70 | | - const difference = ((change / 100) * cost).toFixed(2); |
71 | | - const elHTML = el.text().split("(")[0]; |
72 | | - el.html(elHTML + "<span>(" + difference + ")</span>"); |
73 | | - } catch (e) { |
74 | | - console.log( |
75 | | - "absolute-value.user.js - error setting absolute change value." |
76 | | - ); |
| 69 | + if (cost) { |
| 70 | + try { |
| 71 | + // Update the span with the change |
| 72 | + const difference = ((change / 100) * cost).toFixed(2); |
| 73 | + const elHTML = el.text().split("(")[0]; |
| 74 | + el.html(elHTML + "<span>(" + difference + ")</span>"); |
| 75 | + } catch (e) { |
| 76 | + console.log( |
| 77 | + "absolute-value.user.js - error setting absolute change value." |
| 78 | + ); |
| 79 | + } |
77 | 80 | } |
78 | | - } |
| 81 | + }); |
79 | 82 | }); |
80 | | - }); |
81 | | - } |
| 83 | + } |
82 | 84 |
|
83 | | - // This function listens for network requests and intercepts the target list to turn their icon on and off. |
84 | | - function watchTicker() { |
85 | | - setTimeout(() => { |
86 | | - socket.on("message", function (a) { |
87 | | - var parsed = JSON.parse(a); |
88 | | - if ("ticker" == parsed.type) { |
89 | | - // console.log("result", parsed.result) |
90 | | - addAbsoluteResult(parsed.result); |
91 | | - } |
92 | | - }); |
93 | | - }, 1000); // delay accessing the socket variable so we know it's initialized. |
94 | | - } |
| 85 | + // This function listens for network requests and intercepts the target list to turn their icon on and off. |
| 86 | + function watchTicker() { |
| 87 | + setTimeout(() => { |
| 88 | + socket.on("message", function (a) { |
| 89 | + var parsed = JSON.parse(a); |
| 90 | + if ("ticker" == parsed.type) { |
| 91 | + // console.log("result", parsed.result) |
| 92 | + addAbsoluteResult(parsed.result); |
| 93 | + } |
| 94 | + }); |
| 95 | + }, 1000); // delay accessing the socket variable so we know it's initialized. |
| 96 | + } |
95 | 97 |
|
96 | | - jQuery(() => { |
97 | | - addStyles(); |
98 | | - watchTicker(); |
99 | | - }); |
100 | | -})(); |
| 98 | + jQuery(() => { |
| 99 | + addStyles(); |
| 100 | + watchTicker(); |
| 101 | + }); |
| 102 | + })(); |
| 103 | +} |
| 104 | +catch(err) { |
| 105 | + console.log(`Error in script absolute-value.user.js: ${err.name}: ${err.message}`); |
| 106 | +} |
0 commit comments