Last active
July 21, 2018 23:00
-
-
Save DarkMatterMatt/afee24a1597a2db8641820622f851b33 to your computer and use it in GitHub Desktop.
Latest RASP for rasp.nz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Latest RASPs page for rasp.nz | |
// @namespace MattMoran | |
// @version 2.0.1 | |
// @description Allows using rasp.nz/rasp/latest | |
// @updateURL | |
// @author Matt Moran | |
// @copyright Matt Moran 2017 | |
// @match http://rasp.nz/rasp/latest* | |
// ==/UserScript== | |
/** To use: | |
** go to http://rasp.nz/rasp/latest, select your region and day | |
** go to http://rasp.nz/rasp/latest?region=NZNORTH_N, select your day | |
** go to http://rasp.nz/rasp/latest?region=NZNORTH_N&day=0 [day=0 is today, 1 is tomorrow, 2 is the day after] | |
**/ | |
(function() { | |
"use strict"; | |
function getRaspForDay (region, day_offset) { | |
const url = "http://rasp.nz/rasp/forecast.php?region="; | |
const d = new Date(); | |
d.setDate(d.getDate() + day_offset); | |
const day = d.getDate(); | |
const month = d.getMonth() + 1; | |
const year = d.getFullYear(); | |
const date = "&date=" + year + | |
(month < 10 ? "0" : "") + month + | |
(day < 10 ? "0" : "") + day; | |
const mod = ["&mod=%2B0", "&mod=", "&mod=%2B1"]; | |
let modsLoaded = [null, null, null]; | |
const confidenceString = ["high", "above average", "average"]; | |
for (let i = 0; i < mod.length; i++) { | |
const full_url = url + region + date + mod[i]; | |
fetch(full_url) | |
.then(r => r.text()) | |
.then(r => { | |
modsLoaded[i] = r.length > 20000 && full_url; | |
console.log(modsLoaded); | |
if (modsLoaded.every(v => v !== null)) { | |
for (let j = 0; j < modsLoaded.length; j++) { | |
if (modsLoaded[j]) { | |
r = r.replace(/<div[\s\S]*?<\/div>(?:\s|<br\/?>)*/, `<h1>Forecast Confidence: ${confidenceString[j]}</h1>`); | |
console.log(r); | |
document.write(r); | |
break; | |
} | |
} | |
} | |
}); | |
} | |
}; | |
document.title = "DrJack RASP BLIPMAPs for New Zealand"; | |
var div = document.createElement("DIV"); | |
var img = document.createElement("IMG"); | |
var but1 = document.createElement("BUTTON"); | |
var but2 = document.createElement("BUTTON"); | |
var but3 = document.createElement("BUTTON"); | |
var select = document.createElement("SELECT"); | |
var option = document.createElement("OPTION"); | |
var option1 = document.createElement("OPTION"); | |
var option2 = document.createElement("OPTION"); | |
var option3 = document.createElement("OPTION"); | |
var option4 = document.createElement("OPTION"); | |
select.append(option); | |
select.append(option1); | |
select.append(option2); | |
select.append(option3); | |
select.append(option4); | |
div.style = "margin: 25px 0 0 15px; padding: 10px 5px; width: 130px; text-align: center; font-size: 15px; position: fixed;"; | |
div.append(select); | |
div.append(but1); | |
div.append(but2); | |
div.append(but3); | |
document.body.innerHTML = ""; | |
document.body.append(div); | |
document.body.append(img); | |
option1.value = "NZNORTH_N"; | |
option2.value = "NZNORTH_C"; | |
option3.value = "NZSOUTH_N"; | |
option4.value = "NZSOUTH_S"; | |
option.innerText = "-- REGION --"; | |
option1.innerText = "1: NZNORTH_N"; | |
option2.innerText = "2: NZNORTH_C"; | |
option3.innerText = "3: NZSOUTH_N"; | |
option4.innerText = "4: NZSOUTH_S"; | |
select.style = but1.style = but2.style = but3.style = "width: 120px;"; | |
but1.innerText = "Today"; | |
but2.innerText = "Tomorrow"; | |
but3.innerText = "The day after"; | |
but1.addEventListener("click", () => getRaspForDay(select.value, 0)); | |
but2.addEventListener("click", () => getRaspForDay(select.value, 1)); | |
but3.addEventListener("click", () => getRaspForDay(select.value, 2)); | |
img.height = 443; | |
img.width = 320; | |
img.src = "regions.png"; | |
img.alt = "RASP NZ regions"; | |
const url = new URL(window.location.href); | |
let region = url.searchParams.get("region"); | |
let day = url.searchParams.get("day"); | |
if (day !== null && region !== null) { | |
day = parseInt(day); | |
select.value = region; | |
getRaspForDay(region.toUpperCase(), day); | |
} | |
else if (region !== null) { | |
select.value = region.toUpperCase(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment