Skip to content

Commit

Permalink
added yr script
Browse files Browse the repository at this point in the history
  • Loading branch information
on committed Aug 16, 2024
1 parent 38c09e9 commit 54020a6
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions yr.user.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
});
})();

0 comments on commit 54020a6

Please sign in to comment.