From e97bee87e0425e23b4a4e04ab05430a1e2010546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Andreas=20Nilsen?= Date: Wed, 21 Aug 2024 12:22:16 +0200 Subject: [PATCH] It's now possible to navigate the horizontal navbar using ctrl-left/right --- yr.user.js | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/yr.user.js b/yr.user.js index 74a17da..f6b3498 100644 --- a/yr.user.js +++ b/yr.user.js @@ -1,8 +1,8 @@ // ==UserScript== -// @name YR Hotkeys +// @name YR.no - Hotkeys // @namespace http://tampermonkey.net/ -// @version 1.1.2 -// @description Hotkeys 'alt+l', 'alt+r', or 'alt+v' for yr.no +// @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 @@ -13,9 +13,36 @@ (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.altKey || event.metaKey) && (event.key === 'l' || event.key === 'r' || event.key === 'v')) { + 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-]+)\//; @@ -62,13 +89,8 @@ 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