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 c3054d1 commit 8a24595
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions yr-nav.user.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
});
})();

0 comments on commit 8a24595

Please sign in to comment.