From 63e0f89c5829579c0316b324692d56af754af20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Andreas=20Nilsen?= Date: Thu, 2 Oct 2025 15:03:47 +0200 Subject: [PATCH] primary account link is added to top table on person sites. Notices in modern-bas should be red --- bas-search.user.js | 110 ++++++++++++++++++++++++++++++++++++++++++--- modern-bas.css | 4 ++ 2 files changed, 107 insertions(+), 7 deletions(-) diff --git a/bas-search.user.js b/bas-search.user.js index 46023bd..2ad16d3 100644 --- a/bas-search.user.js +++ b/bas-search.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name BAS - Quick Search // @namespace http://tampermonkey.net/ -// @version 1.7.11 +// @version 1.8.5 // @description Quick search using `Ctrl` + `Shift` + `F` hotkey, group search with "g:" and person search with "p:" prefix. Navigate tabs with Ctrl + Left/Right keys. Navigate search results with up and down keys. // @author Øyvind Nilsen (on@ntnu.no) // @match https://bas.ntnu.no/* @@ -60,6 +60,7 @@ let selectedResultIndex = -1; let searchResultsActive = false; let searchTimeout = null; + let currentPageType = null; // 'person', 'group', 'account', or null // Function to setup title click handler function setupTitleClickHandler() { @@ -79,6 +80,7 @@ // Check if it's a group or account page if (titleText.startsWith('Group ') || titleText.startsWith('Account ') || titleText.startsWith('Person ')) { var objectType = titleText.split(' ')[0].toLowerCase(); + currentPageType = objectType; // Set the global page type variable const objectName = titleText.substring(titleText.indexOf(' ') + 1); if (objectType === 'group') { @@ -117,6 +119,64 @@ } } + + function findFirstBoldAccountAndUserId() { + // Find the Accounts info box + const accountsDiv = Array.from(document.querySelectorAll('div.info.box')) + .find(div => div.querySelector('h3')?.textContent.trim() === 'Accounts'); + + if (!accountsDiv) return null; + + // Find the first row with a bold account name + const boldAccountRow = accountsDiv.querySelector('table tr:not(.headers) td:first-child b')?.closest('tr'); + + if (!boldAccountRow) return null; + + const accountName = boldAccountRow.querySelector('td:first-child b').textContent.trim(); + + // Find the Edit link in the Actions column (last column) + const editLink = boldAccountRow.querySelector('td:last-child a[href*="/account/view?id="]'); + if (!editLink) return null; + + // Extract account ID from the Edit link URL + const urlMatch = editLink.href.match(/id=(\d+)/); + if (!urlMatch) return null; + + const accountId = urlMatch[1]; + + return `${accountName}`; + } + + function addTableRow(name, value, rowid, valueElement = null) { + const table = document.querySelector('table'); + if (!table) return; + + const lastRow = table.querySelector('tr:last-child'); + const newRowClass = lastRow?.classList.contains('odd') ? 'even' : 'odd'; + + const newRow = document.createElement('tr'); + newRow.id = rowid; + newRow.className = newRowClass; + + const nameCell = document.createElement('td'); + nameCell.className = 'name'; + nameCell.textContent = name; + + const valueCell = document.createElement('td'); + valueCell.className = 'value'; + + if (valueElement) { + valueCell.appendChild(valueElement); + } else { + valueCell.innerHTML = value; + } + + newRow.appendChild(nameCell); + newRow.appendChild(valueCell); + + table.appendChild(newRow); + } + // Function to copy text to clipboard function copyToClipboard(html, text) { const textBlob = new Blob([text], { type: 'text/plain' }); @@ -474,25 +534,61 @@ tabs[nextIndex].click(); } + function initializeAddedData() { + if (currentPageType === 'person') { + const accountResult = findFirstBoldAccountAndUserId(); + if (accountResult) { + // Extract the username from the account link + const tempDiv = document.createElement('div'); + tempDiv.innerHTML = accountResult; + const username = tempDiv.textContent; + + // Create a container div for the account link and button + const container = document.createElement('div'); + container.innerHTML = accountResult; + + // Create copy button + const copyButton = document.createElement('button'); + copyButton.textContent = 'copy username'; + copyButton.style.marginLeft = '10px'; + copyButton.style.padding = '2px 6px'; + copyButton.style.fontSize = '11px'; + copyButton.style.backgroundColor = '#007acc'; + copyButton.style.color = 'white'; + copyButton.style.border = 'none'; + copyButton.style.borderRadius = '3px'; + copyButton.style.cursor = 'pointer'; + + copyButton.addEventListener('click', function(e) { + e.preventDefault(); + e.stopPropagation(); + copyToClipboard(null, username); + showMessage(username); + }); + + // Add button to container + container.appendChild(copyButton); + + addTableRow('Primary Account', null, 'primary-account-row', container); + } + } + } + // Initialize on page load if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => { focusUsernameInput(); initializeSearchResultNavigation(); setupTitleClickHandler(); + initializeAddedData(); }); } else { focusUsernameInput(); initializeSearchResultNavigation(); setupTitleClickHandler(); + initializeAddedData(); } - setTimeout(() => { - focusUsernameInput(); - initializeSearchResultNavigation(); - setupTitleClickHandler(); - }, 500); - // Global keyboard event handlers document.addEventListener('keydown', function(event) { // Show search box on Ctrl+Shift+F diff --git a/modern-bas.css b/modern-bas.css index c96eb28..b9d3cb9 100644 --- a/modern-bas.css +++ b/modern-bas.css @@ -55,6 +55,10 @@ body { line-height: 1.5; } +.notice { + background-color: #f7b3ba; +} + #pagewrap { max-width: 1600px; margin: 0 auto;