Difference between revisions of "User:Charlotte/nookipedia-timeless.js"

From Nookipedia, the Animal Crossing wiki
(fixed issue)
Line 2: Line 2:
 
mw.loader.using('mediawiki.util', function () {
 
mw.loader.using('mediawiki.util', function () {
 
     document.addEventListener('keydown', function (event) {
 
     document.addEventListener('keydown', function (event) {
         // Check if the "/" key was pressed
+
        // Get the currently focused element
         if (event.key === '/') {
+
        var activeElement = document.activeElement;
             event.preventDefault();  // Prevent default behavior (e.g., typing "/" in an input)
+
 
 +
         // Check if the "/" key was pressed, and focus is not already on the search bar
 +
         if (event.key === '/' && activeElement.id !== 'searchInput') {
 +
             // Prevent default behavior (e.g., typing "/" in a different input field)
 +
            event.preventDefault();
 +
 
 +
            // Focus the search input field
 
             var searchInput = document.getElementById('searchInput');
 
             var searchInput = document.getElementById('searchInput');
 
             if (searchInput) {
 
             if (searchInput) {

Revision as of 15:19, September 2, 2024

//Go to the search bar when the "/" key is pressed
mw.loader.using('mediawiki.util', function () {
    document.addEventListener('keydown', function (event) {
        // Get the currently focused element
        var activeElement = document.activeElement;

        // Check if the "/" key was pressed, and focus is not already on the search bar
        if (event.key === '/' && activeElement.id !== 'searchInput') {
            // Prevent default behavior (e.g., typing "/" in a different input field)
            event.preventDefault();

            // Focus the search input field
            var searchInput = document.getElementById('searchInput');
            if (searchInput) {
                searchInput.focus();
            }
        }
    });
});