-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
115 lines (90 loc) · 3.47 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
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
let exchangeRate = null;
let enabled = true; // Conversion enabled by default
// Get initial settings
chrome.storage.sync.get(["exchangeRate", "enabled"], (data) => {
exchangeRate = data.exchangeRate;
enabled = data.enabled;
if (enabled) {
convertExistingPrices();
}
});
// Listen for changes from the popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "updateExchangeRate") {
exchangeRate = request.exchangeRate;
if (enabled) { // Only convert if enabled
convertExistingPrices();
}
} else if (request.action === "toggleEnabled") {
enabled = request.enabled;
if (enabled) {
convertExistingPrices();
} else {
revertPrices(); // Revert to original prices when disabled
}
}
});
function convertExistingPrices() {
const targetElements = document.querySelectorAll('.price, .product-price, .cost'); // Example selectors
targetElements.forEach(element => processTextNodes(element));
}
function convertUSDToINR(element) {
if (element.dataset.originalText && element.textContent.includes("₹")) return; // Check for converted price
const usdRegex = /\$\s*([\d,.]+)/g;
const usdAmount = parseFloat(element.textContent.replace(/[^0-9.-]+/g, ""));
if (!isNaN(usdAmount)) {
element.dataset.originalText = element.textContent; // Store original price before conversion
const inrAmount = usdAmount * exchangeRate;
element.textContent = element.textContent.replace(usdRegex, `$& (₹${inrAmount.toFixed(2)})`);
}
}
function revertPrices(){
const convertedElements = document.querySelectorAll('[data-original-text]');
convertedElements.forEach(element => {
element.textContent = element.dataset.originalText;
delete element.dataset.originalText;
});
}
function processTextNodes(node) {
const usdRegex = /\$\s*([\d,.]+)/g; // Improved regex
for (const child of node.childNodes) {
if (child.nodeType === Node.TEXT_NODE) {
if (usdRegex.test(child.textContent)) {
const newSpan = document.createElement("span");
newSpan.innerHTML = child.textContent.replace(
usdRegex,
`<span class="usd-to-inr">$1</span>` // Use captured group
);
child.parentNode.replaceChild(newSpan, child);
const usdElements = newSpan.querySelectorAll(".usd-to-inr");
usdElements.forEach((el) => convertUSDToINR(el));
}
} else if (child.nodeType === Node.ELEMENT_NODE) {
processTextNodes(child); // Recursively process child elements
}
}
}
// Debounced Mutation Observer
let timeoutId;
const observer = new MutationObserver((mutations) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
processTextNodes(node);
}
});
});
}, 200); // Debounce delay
});
// Observe relevant elements. You'll need to adapt these selectors:
const targetElements = document.querySelectorAll('.price, .product-price, .cost'); // Or other appropriate selectors
targetElements.forEach(element => {
processTextNodes(element)
observer.observe(element, { childList: true, subtree: true });
});
// Call initially to convert prices on page load if enabled
if (enabled) {
convertExistingPrices();
}