Skip to content

Commit 2633130

Browse files
Merge pull request #4 from Nullifiers/improvements
Improvements
2 parents f27e2ea + 3f78af4 commit 2633130

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 0.0.2
2+
- Detect number after decimal places
3+
- Detect formatted price (ex. $ 1,000,000)
4+
- Format price after converting
5+
- add new currency (EURO)
6+
- Extension works when the extension icon is clicked
7+
8+
# 0.0.1
9+
- Initial version

src/js/background.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
chrome.browserAction.onClicked.addListener((tab) => {
2+
chrome.tabs.executeScript(tab.id, {
3+
runAt: 'document_end',
4+
allFrames: true,
5+
file: 'js/contentScript.js',
6+
});
7+
});

src/js/contentScript.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
11
(function() {
2-
console.log('content script loaded');
3-
4-
const usdRegex = /\$( ?[0-9]+)/g;
5-
const usdToRupeeRatio = 69.58;
6-
7-
document.body.innerHTML = document.body.innerHTML.replace(usdRegex, (_, val) => {
8-
const numval = Number(val);
9-
return Number.isNaN(numval) ? `$ ${val}` : `₹ ${usdToRupeeRatio * numval}`;
2+
function replace($node, regex, replaceMethod) {
3+
const queue = [$node];
4+
while (queue.length) {
5+
let top = queue.shift();
6+
if (top.childNodes.length) {
7+
queue.push(...top.childNodes);
8+
continue;
9+
}
10+
11+
// If a text node is only child then replace currency using innerHTML
12+
if (top.nodeType === Node.TEXT_NODE && top.parentElement.childNodes.length === 1) {
13+
top = top.parentElement;
14+
}
15+
16+
if (top.nodeType === Node.ELEMENT_NODE && regex.test(top.innerHTML)) {
17+
top.innerHTML = top.innerHTML.replace(regex, replaceMethod.bind(top));
18+
}
19+
else if (top.nodeType === Node.TEXT_NODE && regex.test(top.nodeValue)) {
20+
top.nodeValue = top.nodeValue.replace(regex, replaceMethod.bind(top));
21+
}
22+
}
23+
}
24+
25+
const toRupeeRatio = {
26+
'$': 69.58,
27+
'€': 77.82,
28+
};
29+
const currencySymbols = ['\\$', '€'];
30+
const currencyRegex = new RegExp(`(${currencySymbols.join('|')})\\s?(\\d+(?:,\\d{2,3})*(?:\\.\\d+)?)`, 'g');
31+
32+
const INR = new Intl.NumberFormat('en-IN', {style: 'currency', currency: 'INR'});
33+
34+
const spanStyle = 'background: rgba(220, 220, 220, 0.7); color: #333; padding: 2px 5px; border-radius: 3px';
35+
const format = price => `<span style="${spanStyle}">${INR.format(price)}</span>`;
36+
37+
replace(document.body, currencyRegex, function (_, currency, val) {
38+
const numval = Number(val.replace(/,/g, ''));
39+
if (Number.isNaN(numval)) return `${currency} ${val}`;
40+
41+
return this.nodeType === Node.ELEMENT_NODE ?
42+
format(toRupeeRatio[currency] * numval) :
43+
INR.format(toRupeeRatio[currency] * numval);
1044
});
1145
})();

src/manifest.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
22
"manifest_version": 2,
33
"name": "Realtime Currency Converter",
4-
"version": "0.0.1",
4+
"version": "0.0.2",
55
"description": "Browser Extenstion to convert foreign currency to local currency, realtime.",
6-
"content_scripts": [{
7-
"matches": ["<all_urls>"],
8-
"all_frames": true,
9-
"js": ["js/contentScript.js"],
10-
"run_at": "document_end"
11-
}],
6+
"browser_action": {},
7+
"background": {
8+
"scripts": ["js/background.js"],
9+
"persistent": false
10+
},
1211
"permissions": ["activeTab", "storage"]
1312
}

0 commit comments

Comments
 (0)