-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.js
More file actions
222 lines (191 loc) · 7.11 KB
/
main.js
File metadata and controls
222 lines (191 loc) · 7.11 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
let OBSERVATIONS = { childList: true, subtree: true };
let OPTIONS = {};
let LOAD_TIMEOUT_MS = 5000;
let OPTIONS_URL = chrome.runtime.getURL('options.html');
chrome.storage.sync.get("options", function (obj) {
if (obj.options) {
OPTIONS = obj.options;
}
});
// Get all the sections in the sidebar.
function getAccountSections(sidebarElement) {
return sidebarElement.querySelectorAll('.sc\\:transition-\\[height\\].sc\\:ease-in-out');
}
// Get all the accounts in a section of the sidebar
function getAccounts(sectionElement) {
return sectionElement.querySelectorAll(':scope > div');
}
function getAccountValue(accountElement) {
const valueText = accountElement.querySelector(".sc\\:sr-only").textContent;
return Number(valueText.replace(/[^-0-9\.]+/g, ""));
}
// Hide an element. Permanent unless temporary is specified.
function hideElement(element, { temporary = false } = {}) {
if (element) {
element.style.setProperty('display', 'none', temporary ? '' : 'important');
}
}
// Show a hidden element.
function showElement(element) {
if (element) {
element.style.setProperty('display', 'block');
}
}
function addOptionsLink(menuElement) {
const ul = menuElement.querySelector('ul');
if (!ul) return;
if (ul.querySelector(`a[href="${OPTIONS_URL}"]`)) return;
const optionsLi = ul.querySelector('li').cloneNode(true);
const optionsA = optionsLi.querySelector('div a')
optionsA.href = OPTIONS_URL;
optionsA['aria-label'] = 'Extension';
optionsA.textContent = 'Extension';
optionsA.target = '_blank';
ul.appendChild(optionsLi);
}
function sortBalances(sidebarElement) {
getAccountSections(sidebarElement).forEach(function(sectionElement) {
let accounts = Array.from(getAccounts(sectionElement));
let accountData = accounts.map(accountElement => {
const value = getAccountValue(accountElement);
return { accountElement, value };
});
let sorted = accountData.every((item, idx, arr) => {
return idx === 0 || arr[idx-1].value >= item.value;
});
if (sorted) return;
accountData.sort((a, b) => b.value - a.value);
accountData.forEach(({ accountElement }) => sectionElement.appendChild(accountElement));
});
}
function condenseBalances(sidebarElement) {
const OLD_HEIGHT = 68;
const NEW_HEIGHT = 56;
const SECTION_HEIGHT = 38;
getAccountSections(sidebarElement).forEach(function(sectionElement) {
if (sectionElement.style.height !== '0px') {
let accounts = Array.from(getAccounts(sectionElement)).filter(div => getComputedStyle(div).display !== 'none');
let height = accounts.length * NEW_HEIGHT;
sectionElement.style.height = `${height}px`;
}
});
sidebarElement.querySelectorAll(`span.sc\\:h-\\[${OLD_HEIGHT}px\\]`).forEach(function(element) {
element.classList.replace(`sc:h-[${OLD_HEIGHT}px]`,`sc:h-[${SECTION_HEIGHT}px]`);
element.classList.replace('sc:p-4','sc:p-2');
});
sidebarElement.querySelectorAll(`div.sc\\:h-\\[${OLD_HEIGHT}px\\]`).forEach(function(element) {
element.classList.replace(`sc:h-[${OLD_HEIGHT}px]`,`sc:h-[${NEW_HEIGHT}px]`);
});
}
function hideZeroBalances(sidebarElement) {
getAccountSections(sidebarElement).forEach(function(sectionElement) {
getAccounts(sectionElement).forEach(function(accountElement) {
if (getAccountValue(accountElement) === 0) {
hideElement(accountElement);
}
});
});
}
function hideAccountChanges(sidebarElement) {
// Hide net worth changes
let netWorthChangeSymbold = sidebarElement.querySelector('svg')
let netWorthChangeContainer = netWorthChangeSymbold.parentElement;
let netWorthChangeButtons = netWorthChangeContainer.nextElementSibling;
hideElement(netWorthChangeContainer);
hideElement(netWorthChangeButtons);
// Hide change arrows
sidebarElement.querySelectorAll('svg[aria-label]:not([aria-label=""])').forEach(function(element) {
hideElement(element);
});
getAccountSections(sidebarElement).forEach(function(sectionElement) {
// Hide section changes
let sectionChangeContainer = sectionElement.previousElementSibling.firstChild;
if (sectionChangeContainer.childElementCount > 1) {
let valuesContainer = sectionChangeContainer.lastChild;
if (valuesContainer.childElementCount > 1) {
let sectionChangeElement = valuesContainer.lastChild;
hideElement(sectionChangeElement);
}
}
// Hide account changes
getAccounts(sectionElement).forEach(function(accountElement) {
accountElement.querySelectorAll('[data-testid="performance-change-text"]').forEach(function(element) {
hideElement(element);
});
});
});
}
function hideNetWorth(sidebarElement) {
let netWorthElement, netWorthBlockerElement;
if (!sidebarElement.querySelector('#pcp_networth_blocker')) {
netWorthElement = sidebarElement.querySelector('#networth-balance');
netWorthBlockerElement = netWorthElement.cloneNode()
netWorthBlockerElement.id = 'pcp_networth_blocker';
netWorthBlockerElement.innerText = netWorthElement.innerText.replace(/\d/g, '*')
netWorthElement.parentNode.insertBefore(netWorthBlockerElement, netWorthElement.nextSibling);
hideElement(netWorthElement, { temporary: true });
netWorthBlockerElement.onmouseover = function () {
hideElement(netWorthBlockerElement, { temporary: true });
showElement(netWorthElement);
};
netWorthElement.onmouseout = function () {
hideElement(netWorthElement, { temporary: true });
showElement(netWorthBlockerElement);
};
}
}
function handleSidebarChange(sidebarElement) {
if (OPTIONS.hideNetWorth) {
hideNetWorth(sidebarElement);
}
if (OPTIONS.hideZeroBalances) {
hideZeroBalances(sidebarElement);
}
if (OPTIONS.sortBalances) {
sortBalances(sidebarElement);
}
if (OPTIONS.condenseBalances) {
condenseBalances(sidebarElement);
}
if (OPTIONS.hideAccountChanges) {
hideAccountChanges(sidebarElement);
}
}
/*
When an element matching the provided selector is loaded, add an observer to
it that runs the provided function every time the element changes. The
element may not exist even when the page is first loaded.
Waits up to $LOAD_TIMEOUT_MS milliseconds.
*/
async function addObserverToElement(selector, fn) {
const element = await Promise.race([
new Promise(resolve => {
const element = document.querySelector(selector);
if (element) return resolve(element);
const observer = new MutationObserver(() => {
const element = document.querySelector(selector);
if (element) {
observer.disconnect();
resolve(element);
}
});
observer.observe(document, OBSERVATIONS);
}),
new Promise(resolve => setTimeout(() => resolve(null), LOAD_TIMEOUT_MS)),
]);
if (element) {
const observer = new MutationObserver(() => {
observer.disconnect();
fn(element);
observer.observe(element, OBSERVATIONS);
});
observer.observe(element, OBSERVATIONS);
}
}
/*
Primary entry point when the page loads
*/
window.addEventListener('load', function() {
addObserverToElement('#dropdown-menu-user-profile', addOptionsLink);
addObserverToElement('#sidebar-container', handleSidebarChange);
});