On a webpage, I want to create a JS code to jump to specific divisions by pressing keyboard shortcuts. I created a tampermonkey script but it doesn’t work. Does anyone have any idea?
// ==UserScript==
// @name Navigation Hotkeys
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Navigation hotkeys for 127.0.0.1:5000/*
// @match http://127.0.0.1:5000/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
document.addEventListener('keydown', function(event) {
if (event.ctrlKey) {
switch (event.code) {
case 'Numpad1':
navigateToElement('series_name');
break;
case 'Numpad5':
navigateToElement('sample_data');
break;
case 'Numpad6':
navigateToElement('shopping_cart');
break;
}
}
});
function navigateToElement(elementId) {
var element = document.getElementById(elementId);
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
element.focus();
}
}
})();