Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

related to 'Click here to edit' in script.js #104

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,21 @@ window.addEventListener('DOMContentLoaded', async () => {
const apiKey = userApiKey || defaultApiKey;
proxyurl = userproxyurl || defaultProxyURL;

var currentUserLocation = savedLocation; // Get saved location
// Try to get the savedLocation or a previously stored location from localStorage
var currentUserLocation = savedLocation || localStorage.getItem("locationQ");

if (!currentUserLocation) {
// Only fetch if there is no saved location
const geoLocation = 'https://ipinfo.io/json/';
const locationData = await fetch(geoLocation);
const parsedLocation = await locationData.json();
currentUserLocation = parsedLocation.ip; // Update to user's IP-based location
// If neither savedLocation nor localStorage has a location, fetch the IP-based location
const geoLocation = 'https://ipinfo.io/json/';
const locationData = await fetch(geoLocation);
const parsedLocation = await locationData.json();
currentUserLocation = parsedLocation.ip; // Update to the fetched IP-based location
localStorage.setItem("locationQ", currentUserLocation); // Store the IP-based location in localStorage
}

var currentLanguage = getLanguageStatus('selectedLanguage') || 'en';

// Weather API call using currentUserLocation, which is either user input or IP address
// Weather API call using currentUserLocation, which is either user input, localStorage, or IP address
const weatherApi = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${currentUserLocation}&aqi=no&lang=${currentLanguage}`;

const data = await fetch(weatherApi);
Expand Down Expand Up @@ -347,7 +351,19 @@ setInterval((updated) => {
if (storedValue) {
userTextDiv.textContent = storedValue;
}
// Remove placeholder text when the user starts editing
userTextDiv.addEventListener("focus", function () {
if (userTextDiv.textContent === "Click here to edit") {
userTextDiv.textContent = ""; // Clear the placeholder when focused
}
});

// Restore placeholder if the user leaves the div empty after editing
userTextDiv.addEventListener("blur", function () {
if (userTextDiv.textContent.trim() === "") {
userTextDiv.textContent = "Click here to edit"; // Show placeholder again if empty
}
});
}, 1000)
const userTextDiv = document.getElementById("userText");
userTextDiv.addEventListener("input", function () {
Expand Down