←&pm (Rain or Shine.)
I made a little script to check your IP, pass it along to wttr.in, and tell you if it's raining or snowing outside. It's surprisingly accurate, and I could ask for location permissions, but that seems excessive.
script.js
function httpReq(url) { return new Promise((resolve, reject) => { var xmlHttp = new XMLHttpRequest(); xmlHttp.open("GET", url, true); // true for asynchronous xmlHttp.onload = function () { if (xmlHttp.status === 200) { resolve(xmlHttp.responseText); } else { reject(new Error("Request failed: " + xmlHttp.status)); } }; xmlHttp.onerror = function () { reject(new Error("Network error")); }; xmlHttp.send(null); }); } // Start the weather check in the background function updateWeatherDisplay() { // Set initial display state - make only wttr-none visible while waiting document.getElementById("wttr-none").style.display = "block"; document.getElementById("wttr-rain").style.display = "none"; document.getElementById("wttr-snow").style.display = "none"; // First get the IP address httpReq("https://api.ipify.org/") .then((ip) => { // Then get the weather for that IP return httpReq("https://wttr.in/" + ip + "?format=%C"); }) .then((weatherData) => { let weather = weatherData.toLowerCase(); // Update HTML based on weather data if ( weather.includes("rain") || weather.includes("mist") || weather.includes("drizzle") || weather.includes("outbreaks") ) { document.getElementById("wttr-none").style.display = "none"; document.getElementById("wttr-rain").style.display = "block"; document.getElementById("wttr-snow").style.display = "none"; } else if ( weather.includes("snow") || weather.includes("sleet") || weather.includes("blizzard") || weather.includes("ice") ) { document.getElementById("wttr-none").style.display = "none"; document.getElementById("wttr-rain").style.display = "none"; document.getElementById("wttr-snow").style.display = "block"; } else { document.getElementById("wttr-none").style.display = "block"; document.getElementById("wttr-rain").style.display = "none"; document.getElementById("wttr-snow").style.display = "none"; } }) .catch((error) => { console.error("Error fetching weather data:", error); // Show default display in case of error document.getElementById("wttr-none").style.display = "block"; document.getElementById("wttr-rain").style.display = "none"; document.getElementById("wttr-snow").style.display = "none"; }); } // Call the function to start the background process updateWeatherDisplay();
This text file contains the potential names of the weather conditions provided by wttr.in.
names.txt
CLEAR Clear Sunny Partly cloudy Cloudy Overcast Fog Freezing fog RAIN Mist Patchy rain possible Patchy freezing drizzle possible Thundery outbreaks possible Patchy light drizzle Light drizzle Freezing drizzle Heavy freezing drizzle Patchy light rain Light rain Moderate rain at times Moderate rain Heavy rain at times Heavy rain Light freezing rain Moderate or heavy freezing rain Light rain shower Moderate or heavy rain shower Torrential rain shower Patchy light rain with thunder Moderate or heavy rain with thunder SNOW Patchy snow possible Patchy sleet possible Blowing snow Blizzard Light sleet Moderate or heavy sleet Patchy light snow Light snow Patchy moderate snow Moderate snow Patchy heavy snow Heavy snow Ice pellets Light sleet showers Moderate or heavy sleet showers Light snow showers Moderate or heavy snow showers Patchy light snow with thunder Moderate or heavy snow with thunder