-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunrealized-profits.js
122 lines (115 loc) · 4.67 KB
/
unrealized-profits.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// ==UserScript==
// @name Polymarket unrealized profits calculator
// @namespace http://tampermonkey.net/
// @version 2024-10-19
// @description The script ammends profile profit with unrealized profits figure and profile portfolio with portfolio MLE value
// @author Aleksandr Makarov
// @license Unlicense
// @match https://polymarket.com/*
// @icon https://polymarket.com/icons/favicon-32x32.png
// @grant none
// ==/UserScript==
function waitForElement(selector) {
if (document.querySelector(selector))
return Promise.resolve();
return new Promise((resolve) => {
const observer = new MutationObserver((_, observerInstance) => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observerInstance.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
const observeDOM = (fn, e = document.documentElement, config = { attributes: 1, childList: 1, subtree: 1 }) => {
const observer = new MutationObserver(fn);
observer.observe(e, config);
return () => observer.disconnect();
};
const observeURL = async (fn) => {
await fn();
let url = window.location.href;
observeDOM(() => {
if (window.location.href !== url) {
if (debug)
console.log("URL CHANGED");
url = window.location.href;
fn();
}
});
};
const debug = false;
(async function () {
'use strict';
if (debug)
console.log("STARTING POLYMARKET SCRIPT");
await observeURL(runScript);
})();
async function runScript() {
const url = new URL(window.location.href);
const pathSegments = url.pathname.split('/');
if (debug)
console.log("CHECKING POLYMARKET SCRIPT");
if (!pathSegments.includes('profile'))
return;
if (debug)
console.log("WAITING FOR ELEMENT");
const selector = "#__pm_layout > div > div:nth-child(3) > div:nth-child(2)";
await waitForElement(selector);
await new Promise(r => setTimeout(r, 850));
if (debug)
console.log("STARTING TAMPER MONKEY JOB");
const user_id = pathSegments[pathSegments.indexOf('profile') + 1];
if (!user_id)
return;
const positionsResponse = await fetch(`https://data-api.polymarket.com/positions?user=${user_id}`);
const positions = await positionsResponse.json();
const unrealizedProfit = positions.reduce((acc, p) => acc + p.cashPnl, 0);
const numberFormatter = new Intl.NumberFormat('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
const existingUnrealizedProfitsCell = document.querySelector("#unrealized-profits");
const unrealizedProfitString = unrealizedProfit < 0
? `(-$${numberFormatter.format(Math.abs(unrealizedProfit))})`
: `($${numberFormatter.format(unrealizedProfit)})`;
if (existingUnrealizedProfitsCell) {
existingUnrealizedProfitsCell.innerHTML = unrealizedProfitString;
}
else {
(document.querySelector(selector)).innerHTML += `<p id="unrealized-profits" style="margin: 0">${unrealizedProfitString}</p>`;
}
const portfolioSelector = "#__pm_layout > div > div:nth-child(3) > div:nth-child(1)";
const portfolioMLE = positions.reduce((acc, p) => acc + (p.curPrice > 0.5 ? p.size : 0), 0);
const existingPortfolioMLE = document.querySelector("#portfolio-mle");
const portfolioMLEString = `($${numberFormatter.format(portfolioMLE)})`;
if (existingPortfolioMLE) {
existingPortfolioMLE.innerHTML = portfolioMLEString;
}
else {
(document.querySelector(portfolioSelector)).innerHTML += `<p id="portfolio-mle" style="margin: 0">${portfolioMLEString}</p>`;
}
const volumeSelector = "#__pm_layout > div > div:nth-child(3) > div:nth-child(3)";
const volume = positions.reduce((acc, p) => acc + p.initialValue, 0);
const existingVolume = document.querySelector("#volume");
const volumeString = `($${numberFormatter.format(volume)})`;
if (existingVolume) {
existingVolume.innerHTML = volumeString;
}
else {
(document.querySelector(volumeSelector)).innerHTML += `<p id="volume" style="margin: 0">${volumeString}</p>`;
}
const marketsSelector = "#__pm_layout > div > div:nth-child(3) > div:nth-child(4)";
const markets = positions.length;
const existingMarkets = document.querySelector("#markets");
if (existingMarkets) {
existingMarkets.innerHTML = markets;
}
else {
(document.querySelector(marketsSelector)).innerHTML += `<p id="markets" style="margin: 0">(${markets})</p>`;
}
}