-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
50 lines (47 loc) · 1.64 KB
/
content.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
/**
* Converts a monetary cost into the amount of time the user needs to work to earn this much money (in minutes).
* @param {number} monetary_cost - The cost of the item.
* @param {number} hourly_wage - The user's hourly wage.
* @returns {number} The amount of minutes the user needs to work to earn the specified amount of money (rounded to the nearest integer).
*/
function money_to_time(monetary_cost, hourly_wage) {
// Ensure valid input
if (
isNaN(monetary_cost) ||
isNaN(hourly_wage) ||
monetary_cost <= 0 ||
hourly_wage <= 0
) {
return null; // Invalid input, return null or handle the error appropriately
}
// Calculate minutes required to earn the money
const minutesToWork = Math.round((monetary_cost / hourly_wage) * 60);
return minutesToWork;
}
function replaceCurrency(text, wage) {
let regex = /(₹|\$|€|£|RS|USD|EUR)\s?([,\d]+)(\.(\d{2}))?/g;
let money = regex.exec(text)?.[2]?.replace(',', '');
// console.log(money);
let time = `${money_to_time(parseInt(money), wage)} mins`;
return text.replace(regex, time);
}
function replaceText(ReplacementText) {
let walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
null,
false
);
let node;
while ((node = walker.nextNode())) {
node.textContent = replaceCurrency(node.textContent, ReplacementText);
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log(request);
if (request.time !== undefined) {
replaceText(request.data.hourlyWage);
} else {
console.log(request);
}
});