From 54020a66b3ad6ebe9dbefc3877c49de3df0e0d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Andreas=20Nilsen?= Date: Fri, 16 Aug 2024 13:50:35 +0200 Subject: [PATCH] added yr script --- yr.user.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 yr.user.js diff --git a/yr.user.js b/yr.user.js new file mode 100644 index 0000000..bcc4dd8 --- /dev/null +++ b/yr.user.js @@ -0,0 +1,63 @@ +// ==UserScript== +// @name YR Hotkeys +// @namespace http://tampermonkey.net/ +// @version 1.0 +// @description Hotkeys 'alt+l', 'alt+r', or 'alt+v' for yr.no +// @author Your Name +// @match https://www.yr.no/* +// @grant none +// ==/UserScript== + +(function() { + 'use strict'; + + // Add event listener for keydown events + document.addEventListener('keydown', function(event) { + if (event.altKey && (event.key === 'l' || event.key === 'r' || event.key === 'v')) { + // Get the current URL + const url = window.location.href; + const pattern = /https:\/\/(?:www\.)?yr\.no\/([a-z]{2,3})\/(.+)\/([0-9-]+)\//; + const match = url.match(pattern); + var view = ''; + + if (match) { + console.log("Full match:", match[0]); + console.log("Language code:", match[1]); + console.log("Path:", match[2]); + console.log("ID:", match[3]); + + if (match[1] === 'nb') { + console.log("This is the Norwegian site."); + if (event.key === 'l') { + view = `21-dagersvarsel`; + } else if (event.key === 'r') { + view = `kart/radar`; + } else if (event.key === 'v') { + view = `værvarsel/daglig-tabell`; + } + } else if (match[1] === 'en') { + console.log("This is the English site."); + if (event.key === 'l') { + view = `21-day-forecast`; + } else if (event.key === 'r') { + view = `map/radar`; + } else if (event.key === 'v') { + view = `forecast/daily-table`; + } + } else { + console.log("This is another language site."); + } + + if (view) { + const newUrl = `https://www.yr.no/${match[1]}/${view}/${match[3]}/`; + window.location.href = newUrl; + //console.log(`Redirecting to ${newUrl}`); + } + } else { + console.log("No match found."); + } + + //alert(`You pressed the '${event.key}' key. The url is ${url}`); + } + }); +})(); \ No newline at end of file