-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.html
165 lines (150 loc) · 5.17 KB
/
popup.html
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
<html>
<head>
<meta charset="UTF-8">
<script>
var conversion_list = {
length: [
{imp: "inches",si: "meters", convert: function(x) {return 0.0254*x;}},
{imp: "feet",si: "meters", convert: function(x) {return 0.3048*x;}},
{imp: "yards",si: "meters", convert: function(x) {return 0.9144*x;}},
{imp: "miles",si: "meters", convert: function(x) {return 1609.34*x;}}
],
volume:
[
{imp: "teaspoons",si: "liters", convert: function(x) {return 0.004929*x;}},
{imp: "tablespoons",si: "liters", convert: function(x) {return 0.01479*x;}},
{imp: "fluid ounces",si: "liters", convert: function(x) {return 0.02957*x;}},
{imp: "cups",si: "liters", convert: function(x) {return 0.2366*x;}},
{imp: "pints",si: "liters", convert: function(x) {return 0.4732*x;}},
{imp: "quarts",si: "liters", convert: function(x) {return 0.9464*x;}},
{imp: "gallons",si: "liters", convert: function(x) {return 3.785*x;}}
],
weight:
[
{imp: "ounces",si: "grams", convert: function(x) {return 28.35*x;}},
{imp: "pounds",si: "grams", convert: function(x) {return 453.6*x;}},
{imp: "tons",si: "grams", convert: function(x) {return 907185*x;}}
],
temperature:
[
{imp: "fahrenheit",si: "°c", convert: function(x) {return (x-32)*(5.0/9);}}
],
speed:
[
{imp: "mph",si: "kph", convert: function(x) {return 1.609*x;}}
]
};
//Reset everything! 2 textboxes, three selectors
function ResetValues() {
var a = document.getElementById("categories");
a.value = "All";
document.getElementById("input").value="";
document.getElementById("output").value="";
var imperial = document.getElementById("imperialContainer");
var metric = document.getElementById("metricContainer");
imperial.innerHTML = ""; metric.innerHTML = "";
var len = Object.keys(conversion_list).length;
for(var key in conversion_list)
{
var metricEntry = document.createElement("option");
var val = conversion_list[key][0].si;
metricEntry.value = val; metricEntry.text = val;
metric.add(metricEntry);
var temp = document.createElement("option");
var val = conversion_list[key];
for(var i=0; i< val.length; i++)
{
var vale = conversion_list[key][i].imp;
var impEntry = document.createElement("option");
impEntry.value = vale; impEntry.text = vale;
imperial.add(impEntry);
}
}
}
//If the container button is hit we adjust.
function updateContainers() {
var empire = document.getElementById("imperialContainer");
var meter = document.getElementById("metricContainer");
var cats = document.getElementById("categories");
if(cats.value=="All")
{
ResetValues();
}
else
{
empire.innerHTML = "";
meter.innerHTML = "";
var len = conversion_list[cats.value].length;
for(var i = 0; i<len; i++)
{
var temp = document.createElement("option");
var val = conversion_list[cats.value][i].imp;
temp.value = val; temp.text = val;
empire.add(temp);
}
var temp = document.createElement("option");
var val = conversion_list[cats.value][0].si;
temp.value = val; temp.text = val;
meter.add(temp);
}
}
function updateFromInput()
{
var empire = document.getElementById("imperialContainer");
var meter = document.getElementById("metricContainer");
var value = empire.value;
var len = conversion_list.length;
for(var key in conversion_list)
{
var lent = conversion_list[key].length;
for(var t =0; t<lent; t++)
{
if(conversion_list[key][t].hasOwnProperty(value))
{
meter.value = conversion_list[key][t].si;
return;
}
}
}
}
function calculate()
{
var empire = document.getElementById("imperialContainer");
var empVal = empire.value;
var si = document.getElementById("output");
var input = document.getElementById("input");
var stringToInt = parseFloat(this.value);
var len = conversion_list.length;
for(var key in conversion_list)
{
var lent = conversion_list[key].length;
for(var t =0; t<lent; t++)
{
if(conversion_list[key][t].imp===empVal)
{
si.value = conversion_list[key][t].convert(input.value);
return;
}
}
}
}
</script>
</head>
<body>
<input type="text" id="input" onchange="calculate(this)"> </input> <input type="text" id="output" readonly> </input>
<select id="categories" onchange="updateContainers()">
<option value="All">--</option>
<option value="length">Length</option>
<option value="volume">Volume</option>
<option value="weight">Weight</option>
<option value="temperature">temperature</option>
<option value="speed">speed</option>
</select>
<select id="imperialContainer" onchange="updateFromInput()">
</select>
<select id="metricContainer" >
</select>
<script>document.addEventListener('DOMContentLoaded',function(){ResetValues()});
</script>
</body>
</html>