-
Notifications
You must be signed in to change notification settings - Fork 2
/
ratio.js
94 lines (82 loc) · 3.56 KB
/
ratio.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
// Calculate the mcap / volume ratio of any crypto
function createRatioElement(ratio, color) {
const pClass = document.body.classList.contains('NIGHT') ? 'sc-4984dd93-0 bpmdQz' : 'sc-4984dd93-0 ihZPK';
const ratioElement = document.createElement('p');
ratioElement.className = pClass;
ratioElement.innerHTML = `Ratio: <span style="color: ${color};"><span style="font-weight: 500;">${ratio}</span></span>`;
ratioElement.classList.add('custom-ratio');
return ratioElement;
}
function updateTextWithRatios() {
document.querySelectorAll('.cmc-table tbody tr').forEach(updateRowWithRatio);
}
function updateRowWithRatio(row) {
const marketCapTd = row.querySelector('td:nth-child(8)');
const volumeTd = row.querySelector('td:nth-child(9)');
if (!marketCapTd || !volumeTd) {
console.log("Market cap or volume cell not found in this row. Skipping.");
return;
}
const marketCap = Number.parseFloat(marketCapTd.innerText.replace(/[$,]/g, ''));
const volume = Number.parseFloat(volumeTd.innerText.replace(/[$,]/g, ''));
const ratio = Math.floor(marketCap / volume);
const ratioColor = getRatioColor(ratio);
const targetDiv = volumeTd.querySelector('div[data-nosnippet="true"]');
if (targetDiv && !targetDiv.querySelector('.custom-ratio')) {
const originalP = targetDiv.querySelector('p');
if (originalP) originalP.style.display = 'none';
const ratioElement = createRatioElement(ratio, ratioColor);
targetDiv.appendChild(ratioElement);
}
}
function getRatioColor(ratio) {
if (ratio <= 50) {
return 'var(--up-color)';
}if (ratio <= 100) {
return '#f6b87e';
}
return 'var(--down-color)';
}
// Improved initial update logic
function initialUpdate() {
const checkAndExecute = () => {
if (document.querySelector('.cmc-table tbody tr')) {
updateTextWithRatios();
} else {
setTimeout(checkAndExecute, 500); // Check again after a delay
}
};
checkAndExecute();
}
const tableObserver = new MutationObserver((mutationsList, observer) => {
updateTextWithRatios();
});
const themeObserver = new MutationObserver((mutationsList, observer) => {
if (mutationsList.some(mutation => mutation.attributeName === 'class')) {
for (const element of document.querySelectorAll('.custom-ratio')) {
element.className = document.body.classList.contains('NIGHT') ? 'sc-4984dd93-0 bpmdQz custom-ratio' : 'sc-4984dd93-0 ihZPK custom-ratio';
}
}
});
// Start with the improved initial update logic
initialUpdate();
// Start observing the table for dynamic content loading
const tableConfig = { childList: true, subtree: true };
const tableElement = document.querySelector('.cmc-table');
if (tableElement) {
tableObserver.observe(tableElement, tableConfig);
} else {
// Fallback or retry logic if table is not immediately available
setTimeout(() => {
const retryTableElement = document.querySelector('.cmc-table');
if (retryTableElement) {
tableObserver.observe(retryTableElement, tableConfig);
}
}, 1000); // Adjust timing based on expected delay for table loading
}
// Delay the theme observer to ensure it only starts after the initial setup is complete
setTimeout(() => {
const bodyConfig = { attributes: true, attributeFilter: ['class'] };
themeObserver.observe(document.body, bodyConfig);
}, 1000); // Adjust this delay based on the expected time for the table to be fully loaded
console.log("Script initialized with dynamic theme update functionality.");