Skip to content

Commit

Permalink
It's now possible to navigate the horizontal navbar using ctrl-left/r…
Browse files Browse the repository at this point in the history
…ight
  • Loading branch information
on committed Aug 21, 2024
1 parent 8a24595 commit e97bee8
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions yr.user.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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-]+)\//;
Expand Down Expand Up @@ -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}`);
}
});
})();

0 comments on commit e97bee8

Please sign in to comment.