From 003e6af1286d4c4332ff3f2af9aacfa8283b15be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Andreas=20Nilsen?= Date: Tue, 5 Aug 2025 14:11:52 +0200 Subject: [PATCH] updated doc. Fixed username auto-focus issue --- bas-search.user.js | 57 +++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/bas-search.user.js b/bas-search.user.js index b776154..20d6e57 100644 --- a/bas-search.user.js +++ b/bas-search.user.js @@ -1,8 +1,8 @@ // ==UserScript== // @name BAS Quick Search // @namespace http://tampermonkey.net/ -// @version 1.6.2 -// @description Enhanced search with `Ctrl` + `Shift` + `F` hotkey, menu with keyboard navigation. Supports group search with "g:" prefix and accounts with no prefix or "a:" prefix. Navigate tabs with `Ctrl` + Arrow keys. Auto-focus username input on login page. +// @version 1.6.4 +// @description Popup quick search menu (Hotkey: `Ctrl` + `Shift` + `F`), Use "g:" prefix for group search and no prefix for account search. Navigate tabs with `Ctrl` + `←` / `→` keys. Auto-focus username input on login page. // @author Øyvind Nilsen (on@ntnu.no) // @match https://bas.ntnu.no/* // @grant none @@ -49,9 +49,8 @@ dropdown.style.borderTop = 'none'; dropdown.style.borderRadius = '0 0 4px 4px'; dropdown.style.boxShadow = '0 4px 8px rgba(0,0,0,0.2)'; - dropdown.style.overflowY = 'visible'; // Changed from 'auto' to 'visible' - dropdown.style.display = 'block'; // Always visible when container is shown - // Removed maxHeight constraint + dropdown.style.overflowY = 'visible'; + dropdown.style.display = 'block'; searchContainer.appendChild(dropdown); let menuItems = []; @@ -70,14 +69,14 @@ const menuItem = document.createElement('div'); menuItem.style.padding = '8px 12px'; menuItem.style.cursor = 'pointer'; - menuItem.style.borderBottom = index < tabs.length - 1 ? '1px solid #eee' : 'none'; // No border on last item + menuItem.style.borderBottom = index < tabs.length - 1 ? '1px solid #eee' : 'none'; menuItem.textContent = tab.textContent.trim(); menuItem.setAttribute('data-href', tab.href); - + menuItem.addEventListener('mouseenter', () => { selectMenuItem(index); }); - + menuItem.addEventListener('click', () => { window.location.href = tab.href; }); @@ -86,36 +85,31 @@ menuItems.push(menuItem); }); - // Auto-adjust container position if it goes off-screen adjustContainerPosition(); } // Function to adjust container position to keep it on screen function adjustContainerPosition() { - // Get viewport dimensions const viewportHeight = window.innerHeight; const containerRect = searchContainer.getBoundingClientRect(); - - // Check if container extends below viewport + if (containerRect.bottom > viewportHeight) { - // Move container up to fit in viewport - const overflowAmount = containerRect.bottom - viewportHeight + 20; // 20px margin + const overflowAmount = containerRect.bottom - viewportHeight + 20; const currentTop = parseInt(searchContainer.style.top); - const newTop = Math.max(10, currentTop - overflowAmount); // Don't go above 10px from top + const newTop = Math.max(10, currentTop - overflowAmount); searchContainer.style.top = newTop + 'px'; } } // Function to select a menu item function selectMenuItem(index) { - // Remove previous selection menuItems.forEach(item => { item.style.backgroundColor = ''; item.style.color = ''; }); selectedIndex = index; - + if (index >= 0 && index < menuItems.length) { menuItems[index].style.backgroundColor = '#007acc'; menuItems[index].style.color = 'white'; @@ -124,14 +118,12 @@ // Function to show search interface function showSearchInterface() { - // Reset position before showing searchContainer.style.top = '10px'; - populateDropdown(); searchContainer.style.display = 'block'; inputBox.focus(); inputBox.select(); - selectedIndex = -1; // Start with no menu item selected + selectedIndex = -1; } // Function to hide search interface @@ -141,11 +133,17 @@ inputBox.value = ''; } - // Function to focus on username input if it exists + // Function to focus on username input if it exists (only on login page) function focusUsernameInput() { - const usernameInput = document.querySelector('input[name="name"]'); - if (usernameInput) { - usernameInput.focus(); + // Only focus username input if we're on the login page + if (window.location.pathname === '/login' || window.location.pathname === '/login/') { + const usernameInput = document.querySelector('input[name="username"]'); + if (usernameInput) { + usernameInput.focus(); + console.log('Username input focused on login page'); + } else { + console.log('Username input not found on login page'); + } } } @@ -175,7 +173,7 @@ tabs[nextIndex].click(); } - // Focus username input after page load + // Focus username input after page load (only on login page) if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', focusUsernameInput); } else { @@ -215,7 +213,7 @@ if (event.key === 'Enter') { const searchQuery = inputBox.value.trim(); let url; - + if (searchQuery.toLowerCase().startsWith('g:')) { const groupName = searchQuery.substring(2).trim(); url = `https://bas.ntnu.no/group/search/?name=${encodeURIComponent(groupName)}`; @@ -223,11 +221,10 @@ const accountName = searchQuery.toLowerCase().startsWith('a:') ? searchQuery.substring(2).trim() : searchQuery; url = `https://bas.ntnu.no/account/search/?name=${encodeURIComponent(accountName)}`; } - + window.location.href = url; } else if (event.key === 'ArrowDown') { event.preventDefault(); - // Move to first menu item if (menuItems.length > 0) { selectMenuItem(0); dropdown.focus(); @@ -247,7 +244,6 @@ } else if (event.key === 'ArrowUp') { event.preventDefault(); if (selectedIndex === 0) { - // Go back to input box inputBox.focus(); selectedIndex = -1; } else { @@ -268,9 +264,8 @@ // Global click handler to focus dropdown when needed document.addEventListener('keydown', function(event) { - if (searchContainer.style.display === 'block' && + if (searchContainer.style.display === 'block' && (event.key === 'ArrowDown' || event.key === 'ArrowUp' || event.key === 'Enter')) { - // If dropdown is visible and arrow keys are pressed, make sure it can receive focus if (document.activeElement !== dropdown && document.activeElement !== inputBox) { dropdown.focus(); }