Skip to content

Commit

Permalink
added click to copy link
Browse files Browse the repository at this point in the history
  • Loading branch information
on committed Sep 4, 2025
1 parent ad2e78c commit cd323d6
Showing 1 changed file with 118 additions and 1 deletion.
119 changes: 118 additions & 1 deletion 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.4
// @version 1.7.6
// @description Quick search using `Ctrl` + `Shift` + `F` hotkey, group search with "g:" 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,120 @@
let selectedResultIndex = -1;
let searchResultsActive = false;

// Function to setup title click handler
function setupTitleClickHandler() {
const titleDiv = document.getElementById('title');
if (!titleDiv) return;

const h1Element = titleDiv.querySelector('h1');
if (!h1Element) return;

const titleText = h1Element.textContent.trim();

// Check if it's a group or account page
if (titleText.startsWith('Group ') || titleText.startsWith('Account ') || titleText.startsWith('Person ')) {
const objectType = titleText.split(' ')[0].toLowerCase();

if (objectType === 'group') {
var baseUrl = 'https://bas.ntnu.no/group/view?id=';
} else if (objectType === 'account') {
var baseUrl = 'https://bas.ntnu.no/account/view?id=';
} else if (objectType === 'person') {
var baseUrl = 'https://bas.ntnu.no/person/view?id=';
}

h1Element.style.cursor = 'copy';
h1Element.style.userSelect = 'none';

h1Element.addEventListener('click', function(event) {
event.preventDefault();
event.stopPropagation();


const objectId = document.getElementById('entity_id').value;
const objectUrl = baseUrl + objectId;
console.log(`Copying ${objectType} link to clipboard: ${objectUrl}`);

const htmlLink = `<a href="${objectUrl}" target="_blank">${titleText}</a>`;

copyToClipboard(null, objectUrl);

showMessage(titleText);
});
}
}

// Function to copy text to clipboard
function copyToClipboard(html, text) {
const textBlob = new Blob([text], { type: 'text/plain' });
const data = html ? [
new ClipboardItem({
'text/plain': textBlob,
'text/html': new Blob([html], { type: 'text/html' })
})
] : [
new ClipboardItem({
'text/plain': textBlob
})
];

navigator.clipboard.write(data).then(() => {
console.log(html ? 'HTML and text copied to clipboard' : 'Text copied to clipboard');
}).catch(err => {
console.error('Could not copy to clipboard: ', err);
});
}

// Function to show a fading message box
function showMessage(message) {
const messageBox = document.createElement('div');
messageBox.style.position = 'fixed';
messageBox.style.top = '20px';
messageBox.style.left = '50%';
messageBox.style.transform = 'translateX(-50%)';
messageBox.style.backgroundColor = '#333';
messageBox.style.color = '#ddd';
messageBox.style.padding = '10px 20px';
messageBox.style.borderRadius = '5px';
messageBox.style.zIndex = '10000';
messageBox.style.opacity = '1';
messageBox.style.transition = 'opacity 0.8s ease-out';
messageBox.style.maxWidth = '90vw';
messageBox.style.textAlign = 'center';
messageBox.style.display = 'block';
messageBox.style.boxShadow = '0 2px 10px rgba(0,0,0,0.3)';
messageBox.style.fontFamily = 'Arial, sans-serif';

const boldSpan = document.createElement('span');
boldSpan.style.all = 'initial';
boldSpan.style.color = '#fff';
boldSpan.style.fontWeight = 'bold';
boldSpan.style.fontFamily = 'sans-serif';
boldSpan.style.fontSize = 'inherit';
boldSpan.textContent = message;

const normalSpan = document.createElement('span');
normalSpan.style.all = 'initial';
normalSpan.style.color = '#ddd';
normalSpan.style.fontFamily = 'sans-serif';
normalSpan.style.fontSize = 'inherit';
normalSpan.textContent = ' link copied to clipboard!';

messageBox.appendChild(boldSpan);
messageBox.appendChild(normalSpan);

document.body.appendChild(messageBox);

setTimeout(() => {
messageBox.style.opacity = '0';
setTimeout(() => {
if (messageBox.parentNode) {
document.body.removeChild(messageBox);
}
}, 900);
}, 900);
}

// Function to initialize search result navigation
function initializeSearchResultNavigation() {
// Find all account links in the search results table
Expand Down Expand Up @@ -256,15 +370,18 @@
document.addEventListener('DOMContentLoaded', () => {
focusUsernameInput();
initializeSearchResultNavigation();
setupTitleClickHandler();
});
} else {
focusUsernameInput();
initializeSearchResultNavigation();
setupTitleClickHandler();
}

setTimeout(() => {
focusUsernameInput();
initializeSearchResultNavigation();
setupTitleClickHandler();
}, 500);

// Global keyboard event handlers
Expand Down

0 comments on commit cd323d6

Please sign in to comment.