-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_popup.js
266 lines (239 loc) · 10.7 KB
/
main_popup.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
console.log("This is the js file for main popup")
const KWG_PER_GB = 1.805;
const RETURNING_VISITOR_PERCENTAGE = 0.75;
const FIRST_TIME_VIEWING_PERCENTAGE = 0.25;
const PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD = 0.02;
const CARBON_PER_KWG_GRID = 475;
const CARBON_PER_KWG_RENEWABLE = 33.4;
const PERCENTAGE_OF_ENERGY_IN_DATACENTER = 0.1008;
const PERCENTAGE_OF_ENERGY_IN_TRANSMISSION_AND_END_USER = 0.8992;
const CO2_GRAMS_TO_LITRES = 0.5562;
//########################################################\\
// \\
// WEEKLY GRAPH \\
// \\
//########################################################\\
function display_graph() {
if(document.getElementById('weekly_data').style.display === 'none') {
document.getElementById('weekly_data').style.display = 'inline'
document.getElementById('site_metrics').style.display = 'none'
}
else {
document.getElementById('weekly_data').style.display = 'none'
document.getElementById('site_metrics').style.display = 'inline'
}
}
document.getElementById("report").addEventListener("click", display_graph);
//########################################################\\
// \\
// GREEN WEB FOUNDATION API \\
// \\
//########################################################\\
const greenfoundation = 'https://api.thegreenwebfoundation.org/api/v3/greencheck/'
const getHostname = (url) => {
return new URL(url).hostname;
}
async function update_status(){
var current_url = null
chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
function(tabs){
var complete_url = tabs[0].url
current_url = getHostname(complete_url)
document.getElementById("URL").innerHTML = "URL: " + current_url;
var url = greenfoundation + current_url;
fetch(url)
.then(response => response.json())
.then(json => {
console.log(json);
//document.getElementById("API_RESPONSE").innerHTML = JSON.stringify(json);
if(json.green === true) {
console.log("Eco friendly servers")
document.getElementById("green_server").innerHTML = "green";
document.getElementById("HOSTING_GREEN").innerHTML = "Yayy! This website is hosted on a green server.";
document.body.style.backgroundImage = 'url("/icons/wavegreen.svg")';
}
else if(json.green === false) {
console.log("Non eco friendly servers")
document.getElementById("green_server").innerHTML = "red";
document.getElementById("HOSTING_GREEN").innerHTML = "Nay! This website is NOT hosted on a green server.";
//document.body.style.backgroundColor = '#FF3800';
document.body.style.backgroundImage = 'url("/icons/wavered.svg")';
}
chrome.storage.sync.set({total_co2: total}, function() {
document.getElementById('TOTAL_CO2').innerHTML = "Total Co2: " + total
});
}
)
.catch(err => console.log('Request Failed', err));
}
);
}
document.getElementById("green_server").addEventListener("click", update_status);
//########################################################\\
// \\
// CHROME PAGE SPEED INSIGHTS API \\
// \\
//########################################################\\
const page_speed_api = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url="
var r;
function page_speed(){
chrome.tabs.query({'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT},
function(tabs){
var complete_url = page_speed_api + tabs[0].url
fetch(complete_url)
.then(response => response.json())
.then(obj => {
r = obj
console.log(obj);
var result = obj.lighthouseResult.audits["total-byte-weight"].numericValue / 1000
var energy_consumption = (result * 1000) * KWG_PER_GB / 1073741824
var co2 = energy_consumption * CARBON_PER_KWG_GRID
document.getElementById("CO2_EMITTED").innerHTML = "Co2 produced by this url: " + co2.toString() + "gm";
document.getElementById("DATA_CONSUMED").innerHTML = "Data consumed by this url: " + result.toString() + " kb";
update_carbon_footprint(co2);
}
)
.catch(err => console.log('Request Failed', err));
});
}
document.getElementById("green_server").addEventListener("click", page_speed);
//########################################################\\
// \\
// DAY-WISE CARBON FOOTPRINT \\
// \\
//########################################################\\
function update_carbon_footprint(carbon_to_add) {
console.log("updating co2.....")
var d = new Date();
if(d.getDay() === 0) {
chrome.storage.sync.get(['sunday'], function(result) {
var updated_value = result.sunday + carbon_to_add
chrome.storage.sync.set({sunday: updated_value}, function() {
document.getElementById('sunday').innerHTML = updated_value.toString().substring(0, 5)
});
chrome.storage.sync.get(['total_co2'], function(result) {
var total = result.total_co2 + carbon_to_add
chrome.storage.sync.set({total_co2: total}, function() {
document.getElementById('TOTAL_CO2').innerHTML = "Total Co2: " + total.toString().substring(0, 5)
});
});
});
}
if(d.getDay() === 1) {
chrome.storage.sync.get(['monday'], function(result) {
var updated_value = result.monday + carbon_to_add
chrome.storage.sync.set({monday: updated_value}, function() {
document.getElementById('monday').innerHTML = updated_value
});
});
chrome.storage.sync.get(['total_co2'], function(result) {
var total = result.total_co2 + carbon_to_add
chrome.storage.sync.set({total_co2: total}, function() {
document.getElementById('TOTAL_CO2').innerHTML = total
});
});
}
if(d.getDay() === 2) {
chrome.storage.sync.get(['tuesday'], function(result) {
var updated_value = result.tuesday + carbon_to_add
chrome.storage.sync.set({tuesday: updated_value}, function() {
document.getElementById('tuesday').innerHTML = updated_value
});
});
chrome.storage.sync.get(['total_co2'], function(result) {
var total = result.total_co2 + carbon_to_add
chrome.storage.sync.set({total_co2: total}, function() {
document.getElementById('TOTAL_CO2').innerHTML = total
});
});
}
if(d.getDay() === 3) {
chrome.storage.sync.get(['wednesday'], function(result) {
var updated_value = result.wednesday + carbon_to_add
chrome.storage.sync.set({wednesday: updated_value}, function() {
document.getElementById('wednesday').innerHTML = updated_value
});
});
chrome.storage.sync.get(['total_co2'], function(result) {
var total = result.total_co2 + carbon_to_add
chrome.storage.sync.set({total_co2: total}, function() {
document.getElementById('TOTAL_CO2').innerHTML = total
});
});
}
if(d.getDay() === 4) {
chrome.storage.sync.get(['thursday'], function(result) {
var updated_value = result.thursday + carbon_to_add
chrome.storage.sync.set({thursday: updated_value}, function() {
document.getElementById('thursday').innerHTML = updated_value
});
});
chrome.storage.sync.get(['total_co2'], function(result) {
var total = result.total_co2 + carbon_to_add
chrome.storage.sync.set({total_co2: total}, function() {
document.getElementById('TOTAL_CO2').innerHTML = total
});
});
}
if(d.getDay() === 5) {
chrome.storage.sync.get(['friday'], function(result) {
var updated_value = result.friday + carbon_to_add
chrome.storage.sync.set({friday: updated_value}, function() {
document.getElementById('friday').innerHTML = updated_value
});
});
chrome.storage.sync.get(['total_co2'], function(result) {
var total = result.total_co2 + carbon_to_add
chrome.storage.sync.set({total_co2: total}, function() {
document.getElementById('TOTAL_CO2').innerHTML = total
});
});
}
if(d.getDay() === 6) {
chrome.storage.sync.get(['saturday'], function(result) {
var updated_value = result.saturday + carbon_to_add
chrome.storage.sync.set({saturday: updated_value}, function() {
document.getElementById('saturday').innerHTML = updated_value
});
});
chrome.storage.sync.get(['total_co2'], function(result) {
var total = result.total_co2 + carbon_to_add
chrome.storage.sync.set({total_co2: total}, function() {
document.getElementById('TOTAL_CO2').innerHTML = total
});
});
}
}
//########################################################\\
// \\
// WEEKLY DATA GRAPH \\
// \\
//########################################################\\
// const labels = [
// 'Monday',
// 'Tuesday',
// 'Wednesday',
// 'Thursday',
// 'Friday',
// 'Saturday',
// 'Sunday',
// ];
// const data = {
// labels: labels,
// datasets: [{
// label: 'Data usage everyday',
// backgroundColor: 'rgb(0, 0, 0)',
// borderColor: 'rgb(0, 0, 0)',
// data: [0, 10, 5, 2, 20, 30, 45, 50],
// }]
// };
// const config = {
// type: 'line',
// data: data,
// options: {}
// };
// // === include 'setup' then 'config' above ===
// var myChart = new Chart(
// document.getElementById('myChart'),
// config
// );