From 8a245953cad909bb13f1b9bc202dd9baf768cfd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Andreas=20Nilsen?= Date: Wed, 21 Aug 2024 12:21:57 +0200 Subject: [PATCH] It's now possible to navigate the horizontal navbar using ctrl-left/right --- yr-nav.user.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 yr-nav.user.js diff --git a/yr-nav.user.js b/yr-nav.user.js new file mode 100644 index 0000000..f6b3498 --- /dev/null +++ b/yr-nav.user.js @@ -0,0 +1,96 @@ +// ==UserScript== +// @name YR.no - Hotkeys +// @namespace http://tampermonkey.net/ +// @version 1.2.0 +// @description Navigate the yr.no navbar using Ctrl + Left Arrow and Ctrl + Right Arrow. Navigate to 21-day forecast, radar map or daily table view using Alt + L, Alt + R or Alt + V. +// @author on@ntnu.no +// @match https://www.yr.no/* +// @grant none +// @updateURL https://git.ntnu.no/M365-Drift/MonkeyMagic/raw/main/yr.user.js +// @downloadURL https://git.ntnu.no/M365-Drift/MonkeyMagic/raw/main/yr.user.js +// ==/UserScript== + +(function() { + 'use strict'; + + // Function to navigate to the next or previous menu item + function navigateNavbar(direction) { + const navbar = document.querySelector('#location-header__list'); + if (!navbar) return; + + const items = navbar.querySelectorAll('.location-header__menu-item'); + const activeItem = navbar.querySelector('.location-header__menu-item a[aria-current="page"]'); + let currentIndex = Array.from(items).indexOf(activeItem.parentElement); + + if (direction === 'left' && currentIndex > 0) { + currentIndex--; + } else if (direction === 'right' && currentIndex < items.length - 1) { + currentIndex++; + } + + if (currentIndex >= 0 && currentIndex < items.length) { + const newActiveItem = items[currentIndex].querySelector('a'); + if (newActiveItem) { + window.location.href = newActiveItem.href; + } + } + } + + // Add event listener for keydown events + document.addEventListener('keydown', function(event) { + if (event.ctrlKey && event.key === 'ArrowLeft') { + navigateNavbar('left'); + } else if (event.ctrlKey && event.key === 'ArrowRight') { + navigateNavbar('right'); + } else if ((event.altKey || event.metaKey) && (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 if (match[1] === 'nn') { + console.log("This is the Nynorsk site."); + if (event.key === 'l') { + view = `21-dagarsvarsel`; + } else if (event.key === 'r') { + view = `kart/radar`; + } else if (event.key === 'v') { + view = `vêrvarsel/dagleg-tabell`; + } + } 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; + } + } + } + }); +})(); \ No newline at end of file