101 lines
3.1 KiB
JavaScript
101 lines
3.1 KiB
JavaScript
|
|
|
|
|
|
|
|
// chatGPT
|
|
function forwardToChatGPT() {
|
|
var query = document.getElementById("chatgptQuery").value;
|
|
var url = "https://chat.openai.com/?q=" + encodeURIComponent(query);
|
|
window.location.href = url;
|
|
}
|
|
|
|
|
|
|
|
// base script
|
|
$( document ).ready( function () {
|
|
console.log('dashboards.js loaded');
|
|
|
|
|
|
|
|
const apiKey = 'b502235c1de669b105047b6896f81eb6'; // Replace with your weather API key
|
|
const geoApiKey = '2c50be5398ec45e79655a590d62d6fd8'; // Replace with your geolocation API key
|
|
|
|
const zipcodeInput = document.getElementById('zipcode');
|
|
const lookupBtn = document.getElementById('lookup-btn');
|
|
const weatherResult = document.getElementById('weather-result');
|
|
|
|
// Get the stored zip code from cookies
|
|
const storedZipCode = getCookie('zipcode');
|
|
if (storedZipCode) {
|
|
zipcodeInput.value = storedZipCode;
|
|
getWeather(storedZipCode);
|
|
} else {
|
|
getZipCodeFromIP();
|
|
}
|
|
|
|
lookupBtn.addEventListener('click', function() {
|
|
const zipcode = zipcodeInput.value;
|
|
if (zipcode) {
|
|
setCookie('zipcode', zipcode, 7); // Store the zip code in a cookie for 7 days
|
|
getWeather(zipcode);
|
|
}
|
|
});
|
|
|
|
function getZipCodeFromIP() {
|
|
fetch(`https://api.ipgeolocation.io/ipgeo?apiKey=${geoApiKey}`)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
const zipcode = data.zipcode;
|
|
zipcodeInput.value = zipcode;
|
|
setCookie('zipcode', zipcode, 7);
|
|
getWeather(zipcode);
|
|
})
|
|
.catch(error => {
|
|
console.error('Error getting location:', error);
|
|
});
|
|
}
|
|
|
|
function getWeather(zipcode) {
|
|
fetch(`https://api.openweathermap.org/data/2.5/weather?zip=${zipcode}&appid=${apiKey}`)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
displayWeather(data);
|
|
})
|
|
.catch(error => {
|
|
console.error('Error fetching weather data:', error);
|
|
});
|
|
}
|
|
|
|
function displayWeather(data) {
|
|
if (data.cod === 200) {
|
|
weatherResult.innerHTML = `
|
|
<h4>Weather for ${data.name}</h4>
|
|
<p>Temperature: ${(data.main.temp - 273.15).toFixed(2)}°C</p>
|
|
<p>Condition: ${data.weather[0].description}</p>
|
|
`;
|
|
} else {
|
|
weatherResult.innerHTML = `<p>Error: ${data.message}</p>`;
|
|
}
|
|
}
|
|
|
|
function setCookie(name, value, days) {
|
|
const d = new Date();
|
|
d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
|
|
const expires = "expires=" + d.toUTCString();
|
|
document.cookie = name + "=" + value + ";" + expires + ";path=/";
|
|
}
|
|
|
|
function getCookie(name) {
|
|
const nameEQ = name + "=";
|
|
const ca = document.cookie.split(';');
|
|
for(let i = 0; i < ca.length; i++) {
|
|
let c = ca[i];
|
|
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
|
|
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
});
|