Skip to content

Commit

Permalink
primary account link is added to top table on person sites. Notices i…
Browse files Browse the repository at this point in the history
…n modern-bas should be red
  • Loading branch information
on committed Oct 2, 2025
1 parent 249ac77 commit 63e0f89
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
110 changes: 103 additions & 7 deletions bas-search.user.js
Original file line number Diff line number Diff line change
@@ -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/*
Expand Down Expand Up @@ -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() {
Expand All @@ -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') {
Expand Down Expand Up @@ -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 `<a href="https://bas.ntnu.no/account/view?id=${accountId}">${accountName}</a>`;
}

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' });
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions modern-bas.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ body {
line-height: 1.5;
}

.notice {
background-color: #f7b3ba;
}

#pagewrap {
max-width: 1600px;
margin: 0 auto;
Expand Down

0 comments on commit 63e0f89

Please sign in to comment.