-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
416 lines (330 loc) · 14 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// TODO: Fix the altitude function
// TODO: Get gas prices
// TODO: Get precipitation
// TODO: Display in case of precipitation
// TODO: Get the pinpoint location
// TODO: Render the pinpoint on google earth
// TODO: Render the blue route on google earth
// TODO: Create a video?
let transparent_bg = document.createElement("div");
transparent_bg.style.backgroundColor = "rgb(255, 255, 255, 0.5)";
transparent_bg.style.backdropFilter = "blur(20px)";
transparent_bg.style.padding = "20px";
transparent_bg.style.position = "absolute";
transparent_bg.style.top = "90%";
transparent_bg.style.left = "33%";
transparent_bg.style.borderRadius = "20px";
transparent_bg.style.zIndex = 300;
document.body.appendChild(transparent_bg);
async function get_cloudy_status(location) {
const text = "live weather in for " + location + " google";
const googleLink = "https://www.google.com/search?q=" + encodeURIComponent(text.replace(/ /g, "+"));
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ action: 'fetchGoogleData', link: googleLink }, (response) => {
if (response && response.data) {
const webData = document.createElement("div");
webData.innerHTML = response.data;
const classSelectors = "span#wob_dc";
const relevantArray = [];
webData.querySelectorAll(classSelectors).forEach((item) => {
relevantArray.push(item.innerHTML);
});
resolve(relevantArray[0]);
} else {
console.error('Failed to fetch data from Google.');
reject(new Error('Failed to fetch data from Google.'));
}
});
});
}
function extractCoordinates() {
let url = window.location.href;
const urlObj = new URL(url);
const { pathname, searchParams } = urlObj;
let latitude, longitude;
if (pathname.startsWith("/maps/@")) {
// Type 1: https://www.google.com/maps/@32.8964324,-117.2323364,15z?authuser=1&entry=ttu
let coordinate_big_string = pathname.split("/maps/@")[1].split(",")
return [coordinate_big_string[0], coordinate_big_string[1]]
} else if (pathname.startsWith("/maps/place/")) {
// Type 2: https://www.google.com/maps/place/Seventh+College/@32.8879708,-117.2447013,17z/data=!3m1!4b1!4m6!3m5!1s0x80dc07ecf9dab909:0x5cf33bc80a4bd726!8m2!3d32.8879708!4d-117.2421264!16s%2Fg%2F11j5bths62?authuser=1&entry=ttu
const lngLtdStr = pathname.split('@')[1].split(',');
return [lngLtdStr[0], lngLtdStr[1]];
} else if (pathname.startsWith("/maps/dir/")) {
// Type 3: https://www.google.com/maps/dir/Geisel+Library,+Gilman+Drive,+La+Jolla,+CA/Times+Square,+Manhattan,+NY+10036/@28.86987,-139.1407591,3z/data=!3m1!4b1!4m14!4m13!1m5!1m1!1s0x80dc07853cb13045:0x2d2f19f483aaaace!2m2!1d-117.2375659!2d32.881092!1m5!1m1!1s0x89c25855c6480299:0x55194ec5a1ae072e!2m2!1d-73.9855426!2d40.7579747!3e0?authuser=1&entry=ttu
return extractSourceDestinationCoordinates(url);
}
if (latitude && longitude) {
return [parseFloat(latitude), parseFloat(longitude)];
} else {
return null; // Invalid URL format
}
}
function extractSourceDestinationCoordinates() {
let url = window.location.href;
const lngLtdSource = url.split("/data=")[1].split("!1d")[1];
const source_lng = lngLtdSource.split("!")[0];
const source_lat = lngLtdSource.split("!2d")[1].split("!")[0];
source_coords = [source_lat, source_lng];
// return this
return { 'source_coords': source_coords };
// const lngLtdDestination = url.split("/data=")[1].split("!1d")[2];
// const destination_lng = lngLtdDestination.split("!")[0];
// const destination_lat = lngLtdDestination.split("!2d")[1].split("!")[0];
// const destination_coords = [destination_lat, destination_lng];
// return { 'source_coords': source_coords, 'destination_coords': destination_coords };
}
async function getCityAndCountry(latitude, longitude) {
const apiUrl = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}`;
try {
const response = await fetch(apiUrl);
const data = await response.json();
const city = data.address.city;
const country = data.address.country;
return city + ", " + country;
} catch (error) {
console.error('Error:', error);
return "Ran into errors";
}
}
async function get_temperature(location, unit) {
const text = "live weather in " + unit + " for " + location + " google";
const googleLink = "https://www.google.com/search?q=" + encodeURIComponent(text.replace(/ /g, "+"));
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ action: 'fetchGoogleData', link: googleLink }, (response) => {
if (response && response.data) {
const webData = document.createElement("div");
webData.innerHTML = response.data;
const classSelectors = "span.wob_t.q8U8x";
const relevantArray = [];
webData.querySelectorAll(classSelectors).forEach((item) => {
relevantArray.push(item.innerHTML);
});
if (unit === 'F') {
resolve(relevantArray[0] + ' F');
} else {
resolve(relevantArray[0] + ' C');
}
} else {
console.error('Failed to fetch data from Google.');
reject(new Error('Failed to fetch data from Google.'));
}
});
});
}
async function get_precipitation(location) {
const text = "live weather in " + " for " + location + " google";
const googleLink = "https://www.google.com/search?q=" + encodeURIComponent(text.replace(/ /g, "+"));
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ action: 'fetchGoogleData', link: googleLink }, (response) => {
if (response && response.data) {
const webData = document.createElement("div");
webData.innerHTML = response.data;
const classSelectors = "span#wob_pp";
const relevantArray = [];
webData.querySelectorAll(classSelectors).forEach((item) => {
relevantArray.push(item.innerHTML);
});
resolve(relevantArray[0]);
} else {
console.error('Failed to fetch data from Google.');
reject(new Error('Failed to fetch data from Google.'));
}
});
});
}
// async function scrapeGasPrices(state) {
// const url = "https://www.eia.gov/dnav/pet/hist/LeafHandler.ashx?n=pet&s=emm_epm0_pte_nus_dpg&f=m";
// try {
// const response = await fetch(url);
// const html = await response.text();
// const parser = new DOMParser();
// const doc = parser.parseFromString(html, 'text/html');
// // Example: Extracting gas prices from the webpage
// const gasPricesElement = doc.querySelector('.gas-prices'); // Assuming gas prices are inside an element with class "gas-prices"
// const gasPricesText = gasPricesElement.textContent.trim(); // Extracting text content
// return gasPricesText;
// } catch (error) {
// console.error('Error:', error);
// return "Failed to fetch gas prices";
// }
// }
// // Call the function to scrape gas prices
// scrapeGasPrices("Alabama").then(text => console.log(text)).catch(error => console.error(error));
async function appendWeatherStatus(temperature) {
try {
const weatherWidgetText = document.createElement("span");
weatherWidgetText.innerHTML = "Weather: " + temperature;
weatherWidgetText.style.zIndex = 300;
weatherWidgetText.style.paddingRight = "20px";
transparent_bg.appendChild(weatherWidgetText);
} catch (error) {
console.error("Error:", error);
}
}
async function appendPrecipitationStatus(precipitation) {
try {
const weatherWidgetText = document.createElement("span");
weatherWidgetText.innerHTML = "Precipitation: " + precipitation;
weatherWidgetText.style.zIndex = 300;
weatherWidgetText.style.paddingRight = "20px";
transparent_bg.appendChild(weatherWidgetText);
} catch (error) {
console.error("Error:", error);
}
}
async function appendCloudyStatus(cloudyness) {
try {
const weatherWidgetText = document.createElement("span");
weatherWidgetText.innerHTML = "Cloudyness status: " + cloudyness;
weatherWidgetText.style.zIndex = 300;
weatherWidgetText.style.paddingRight = "20px";
transparent_bg.appendChild(weatherWidgetText);
} catch (error) {
console.error("Error:", error);
}
}
async function main() {
try {
// Get latitude and longitude from coordinates
let latitude, longitude;
const current_location = extractCoordinates();
if (current_location instanceof Array) {
latitude = current_location[0];
longitude = current_location[1];
} else if (current_location && current_location.source_coords) {
latitude = current_location.source_coords[0];
longitude = current_location.source_coords[1];
} else {
console.error('Failed to extract coordinates');
return;
}
// Get city and country based on coordinates
const locationInfo = await getCityAndCountry(latitude, longitude);
const temperature = await get_temperature(locationInfo, "F");
const precipitationChances = await get_precipitation(locationInfo);
const cloudyness = await get_precipitation(locationInfo);
// Append weather status to the document
appendWeatherStatus(temperature);
appendPrecipitationStatus(precipitationChances);
appendCloudyStatus(cloudyness);
} catch (error) {
console.error('An error occurred:', error);
}
}
function gasPrices(price) {
const gasPriceText = document.createElement("p");
gasPriceText.innerHTML = "Total gas price: " + price;
gasPriceText.style.backgroundColor = "red";
gasPriceText.style.position = "absolute";
gasPriceText.style.top = "95%";
gasPriceText.style.left = "75%";
gasPriceText.style.zIndex = 300;
// document.body.appendChild(gasPriceText);
}
function addImmersionButton() {
const button = document.createElement("span");
button.style.backgroundColor = "rgb(0, 0, 0, 0.5)";
button.style.borderRadius = "5px";
button.style.backdropFilter = "blur(20px)";
button.style.cursor = "pointer";
button.style.padding = "5px";
button.style.position = "absolute";
button.style.color = "white";
button.style.top = "70%";
button.style.right = "2%";
button.innerHTML = "Immersive View"
button.addEventListener("click", () => {
startImmersion();
});
document.body.appendChild(button);
}
function get_google_earth_url(latitude, longitude) {
var angle = "1.6";
var depth = "850";
var tail = "35y,90.20609555h,68.07985574t,0r/data=OgMKATA"
var url = "https://earth.google.com/web/@" + latitude + "," + longitude + "," + angle + "a" + "," +
depth + "d," + tail;
return url;
}
function convertToDMS(decimal) {
// Check if the input is valid
if (isNaN(decimal)) {
return "Invalid input";
}
// Ensure the input is within the valid range
if (decimal < -180 || decimal > 180) {
return "Value out of range";
}
// Determine direction (N, S, E, W)
var direction = decimal >= 0 ? (decimal >= 0 ? "N" : "E") : (decimal >= 0 ? "S" : "W");
// Convert decimal degrees to degrees, minutes, seconds
var absDecimal = Math.abs(decimal);
var degrees = Math.floor(absDecimal);
var minutesDecimal = (absDecimal - degrees) * 60;
var minutes = Math.floor(minutesDecimal);
var seconds = Math.round((minutesDecimal - minutes) * 60);
// Construct the DMS string
var dmsString = degrees + "° " + minutes + "' " + seconds + "\" " + direction;
return dmsString;
}
// async function scrapeElevation(decimal1, decimal2) {
// /*
// TAKES DECIMALS
// */
// // Construct the URL with the location query
// var url = "https://whatismyelevation.com/location/" + decimal1 + "," + decimal2;
// try {
// const response = await fetch(url);
// const data = await response.json();
// return data;
// return city + ", " + country;
// } catch (error) {
// console.error('Error:', error);
// return "Ran into errors";
// }
// }
// // Example usage
// var elevation = news("32.875573", "-117.2323364"); // Decimal
// alert("Elevation is: ", elevation);
function panCamera() {
// Get latitude and longitude
let latitude, longitude;
const currentCoordinates = extractCoordinates();
if (currentCoordinates instanceof Array) {
latitude = currentCoordinates[0];
longitude = currentCoordinates[1];
} else {
latitude = currentCoordinates.source_coords[0];
longitude = currentCoordinates.source_coords[1];
}
// Get Google Earth URL
const googleEarthUrl = get_google_earth_url(latitude, longitude);
// Open Google Earth in a new window
const googleEarthWindow = window.open(googleEarthUrl, '_blank');
// Add event listener for when the window finishes loading
googleEarthWindow.addEventListener('load', () => {
// Dispatch cmd+right event
alert("Triggered event");
dispatchCmdRightEvent(googleEarthWindow);
});
}
addImmersionButton();
gasPrices(2.3);
main();
function startImmersion() {
panCamera();
}
function getCoordinateArray() {
const source_location = extractCoordinates();
let latitude;
let longitude;
if (source_location instanceof Array) {
latitude = source_location[0];
longitude = source_location[1];
} else {
latitude = source_location.source_coords[0];
longitude = source_location.source_coords[1];
}
}
getCoordinateArray();