Skip to content

Commit

Permalink
updated doc. Fixed username auto-focus issue
Browse files Browse the repository at this point in the history
  • Loading branch information
on committed Aug 5, 2025
1 parent d2e7454 commit 003e6af
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions bas-search.user.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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;
});
Expand All @@ -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';
Expand All @@ -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
Expand All @@ -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');
}
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -215,19 +213,18 @@
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)}`;
} else {
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();
Expand All @@ -247,7 +244,6 @@
} else if (event.key === 'ArrowUp') {
event.preventDefault();
if (selectedIndex === 0) {
// Go back to input box
inputBox.focus();
selectedIndex = -1;
} else {
Expand All @@ -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();
}
Expand Down

0 comments on commit 003e6af

Please sign in to comment.