forked from t3dotgg/paycheck-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
189 lines (157 loc) · 6.02 KB
/
main.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
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
function convertToRawCount(internationalInputString) {
const numberPattern = /([\d,.]+)([kmb]*)/i;
const matches = internationalInputString.match(numberPattern);
if (!matches) {
return NaN; // Return NaN if the input doesn't match the expected pattern
}
const numericPart = matches[1];
const multiplier = matches[2].toLowerCase();
let numericValue;
const lastChars = [
numericPart.slice(-1),
numericPart.slice(-2, -1),
numericPart.slice(-3, -2),
];
// Check if second or third to last character are , or . to handle international numbers
if (lastChars.includes(".") || lastChars.includes(",")) {
const parts = numericPart.replace(",", ".").split(".");
const integerPart = parts[0].replace(/[,]/g, "");
const decimalPart = parts[1] ? parts[1] : "0";
numericValue = parseFloat(integerPart + "." + decimalPart);
} else {
numericValue = parseFloat(numericPart.replaceAll(",", ""));
}
let factor = 1;
switch (multiplier) {
case "k":
factor = 1000;
break;
case "m":
factor = 1000000;
break;
case "b":
factor = 1000000000;
break;
}
return Math.round(numericValue * factor);
}
function convertToDollars(number) {
const rawCount = convertToRawCount(number);
const processed = rawCount * 0.000026;
if (processed < 0.1) return processed.toFixed(5);
return processed.toFixed(2);
}
const globalSelectors = {};
globalSelectors.postCounts = `[role="group"][id*="id__"]:only-child`;
globalSelectors.articleDate = `[role="article"][aria-labelledby*="id__"][tabindex="-1"] time`;
globalSelectors.analyticsLink = " :not(.dollarBox)>a[href*='/analytics']";
globalSelectors.viewCount =
globalSelectors.postCounts + globalSelectors.analyticsLink;
const innerSelectors = {};
innerSelectors.dollarSpot = "div div:first-child";
innerSelectors.viewSVG = "div div:first-child svg";
innerSelectors.viewAmount = "div div:last-child span span span";
innerSelectors.articleViewAmount = "span div:first-child span span span";
function doWork() {
const viewCounts = Array.from(
document.querySelectorAll(globalSelectors.viewCount)
);
const articleViewDateSections = document.querySelectorAll(globalSelectors.articleDate);
if (articleViewDateSections.length) {
// the rootDateViewsSection will always be the parent->parent->parent of the last element of the articleDate querySelectorAll result
let rootDateViewsSection = articleViewDateSections[articleViewDateSections.length - 1].parentElement.parentElement.parentElement;
// if there is one child, that means it's an old tweet with no viewcount
// if there are more than 4, we already added the paycheck value
if (rootDateViewsSection?.children?.length !== 1 && rootDateViewsSection?.children.length < 4) {
// clone 2nd and 3rd child of rootDateViewsSection
const clonedDateViewSeparator =
rootDateViewsSection?.children[1].cloneNode(true);
const clonedDateView = rootDateViewsSection?.children[2].cloneNode(true);
// insert clonedDateViews and clonedDateViewsTwo after the 3rd child we just cloned
rootDateViewsSection?.insertBefore(
clonedDateViewSeparator,
rootDateViewsSection?.children[2].nextSibling
);
rootDateViewsSection?.insertBefore(
clonedDateView,
rootDateViewsSection?.children[3].nextSibling
);
// get view count value from 'clonedDateViewsTwo'
const viewCountValue = clonedDateView?.querySelector(
innerSelectors.articleViewAmount
)?.textContent;
const dollarAmount = convertToDollars(viewCountValue);
// replace textContent in cloned clonedDateViews (now 4th child) with converted view count value
clonedDateView.querySelector(
innerSelectors.articleViewAmount
).textContent = "$" + dollarAmount;
// remove 'views' label
clonedDateView.querySelector(`span`).children[1].remove();
}
}
for (const view of viewCounts) {
// only add the dollar box once
if (!view.classList.contains("replaced")) {
// make sure we don't touch this one again
view.classList.add("replaced");
// get parent and clone to make dollarBox
const parent = view.parentElement;
const dollarBox = parent.cloneNode(true);
dollarBox.classList.add("dollarBox");
// insert dollarBox after view count
parent.parentElement.insertBefore(dollarBox, parent.nextSibling);
// remove view count icon
const oldIcon = dollarBox.querySelector(innerSelectors.viewSVG);
oldIcon?.remove();
// swap the svg for a dollar sign
const dollarSpot = dollarBox.querySelector(innerSelectors.dollarSpot)
?.firstChild?.firstChild;
dollarSpot.textContent = "$";
// magic alignment value
dollarSpot.style.marginTop = "-0.6rem";
}
// get the number of views and calculate & set the dollar amount
const dollarBox = view.parentElement.nextSibling.firstChild;
const viewCount = view.querySelector(
innerSelectors.viewAmount
)?.textContent;
if (viewCount == undefined) continue;
const dollarAmountArea = dollarBox.querySelector(innerSelectors.viewAmount);
dollarAmountArea.textContent = convertToDollars(viewCount);
}
}
function throttle(func, limit) {
let lastFunc;
let lastRan;
return function () {
const context = this;
const args = arguments;
if (!lastRan) {
func.apply(context, args);
lastRan = Date.now();
} else {
clearTimeout(lastFunc);
lastFunc = setTimeout(function () {
if (Date.now() - lastRan >= limit) {
func.apply(context, args);
lastRan = Date.now();
}
}, limit - (Date.now() - lastRan));
}
};
}
// Function to start MutationObserver
const observe = () => {
const runDocumentMutations = throttle(() => {
requestAnimationFrame(doWork);
}, 1000);
const observer = new MutationObserver((mutationsList) => {
if (!mutationsList.length) return;
runDocumentMutations();
});
observer.observe(document, {
childList: true,
subtree: true,
});
};
observe();