-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
144 lines (128 loc) · 3.08 KB
/
background.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
/* eslint-disable default-case */
/* eslint-disable complexity */
// var convert = require('convert-units')
chrome.runtime.onMessage.addListener(receiver);
let word = "";
window.enabled = false
window.convertedMeasure = 0
chrome.browserAction.onClicked.addListener(buttonClicked)
function buttonClicked() {
enabled = !enabled
if (enabled) {
chrome.browserAction.setBadgeText({text: 'on'})
// chrome.browserAction.setIcon({path: 'Cooking-icon.png'})
} else {
chrome.browserAction.setBadgeText({text: ''})
// chrome.browserAction.setIcon({path: 'Cooking-icon-bw-32.png'})
}
}
function receiver(request, sender, sendResponse) {
if (!enabled) {
sendResponse('Disabled')
return
}
word = request.text;
let sanitizedString = word.trim().toLowerCase();
let unit = sanitizedString.replace(/[^a-zA-Z]/g, "");
// ¼½¾ have the continuous Unicode values from \u00BC to \u00BE
// ⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞ have the continuous Unicode values from \u2150 to \u215E
let value = sanitizedString.replace(/[^/\d\u00BC-\u00BE\u2150-\u215E]/g, "");
if (!value) {
convertedMeasure = "Can't convert";
console.log('inside value')
sendResponse(convertedMeasure);
return
}
// value = parseFloat(value);
let split = value.split('/');
if (split.length > 1) {
value = split[0] / split[1];
}
switch (value) {
case '¾':
value = 0.75;
break;
case '½':
value = 0.5;
break;
case '¼':
value = 0.25
break;
case '⅐':
value = 0.14
break;
case '⅑':
value = 0.11
break;
case '⅒':
value = 0.1
break;
case '⅓':
value = 0.33
break;
case '⅔':
value = 0.66
break;
case '⅕':
value = 0.2
break;
case '⅖':
value = 0.4
break;
case '⅗':
value = 0.6
break;
case '⅘':
value = 0.8
break;
case '⅙':
value = 0.16
break;
case '⅚':
value = 0.83
break;
case '⅛':
value = 0.125
break;
case '⅜':
value = 0.375
break;
case '⅝':
value = 0.625
break;
case '⅞':
value = 0.875
break;
}
switch (unit) {
case "cup":
case "cups":
convertedMeasure = Math.round(value * 236.588);
convertedMeasure = `${convertedMeasure} mL`;
break;
case "pound":
case "pounds":
case "lb":
case "lbs":
convertedMeasure = Math.round(value * 453.592);
convertedMeasure = `${convertedMeasure} grams`;
break;
case "ounce":
case "ounces":
convertedMeasure = Math.round(value * 28.3495);
convertedMeasure = `${convertedMeasure} grams`;
break;
case "quart":
case "quarts":
convertedMeasure = Math.round(value * 946.353);
convertedMeasure = `${convertedMeasure} mL`;
break;
default:
convertedMeasure = "Can't convert";
}
sendResponse(convertedMeasure);
}
// let the popup get the converted measure
// function getConvertedMeasure() {
// return convertedMeasure
// }