Skip to content

added hotkey CTRL+ALT+C to copy topdesk issue numbers #22

Merged
merged 1 commit into from
Jul 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 56 additions & 26 deletions topdesk.user.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// ==UserScript==
// @name TopDesk - Copy Issuenumber
// @namespace https://git.ntnu.no/M365-Drift/MonkeyMagic/
// @version 1.1.4
// @version 1.2.0
// @author Øyvind Nilsen (on@ntnu.no)
// @description Copies the issue number if you click it in TopDesk. It copies plain text and a clickable HTML version. So if the place you paste supports HTML, you can paste a clickable link to the issue.
// @description Copies the issue number if you click it in TopDesk or with the hotkey `ctrl` + `alt` + `c`. It copies plain text and a clickable HTML version. So if the place you paste supports HTML, you can paste a clickable link to the issue.
// @match https://hjelp.ntnu.no/tas/secure/mango/window/*
// @grant none
// @icon https://www.google.com/s2/favicons?sz=64&domain=topdesk.com
Expand Down Expand Up @@ -64,6 +64,43 @@
}, 900);
}

function copyIssueNumber() {
// Get the content from the target div when the function is called
const firstDiv = document.querySelector('div');
if (!firstDiv || !firstDiv.id) return;

const modifiedId = firstDiv.id.slice(0, -1) + '8';
const targetDiv = document.getElementById(modifiedId);
if (!targetDiv) return;

const content = targetDiv.textContent || targetDiv.innerText;
let newContent = content;

// Replace incident matches
if (incidentRegex.test(content)) {
newContent = newContent.replace(incidentRegex, (match) => {
return `<a href="https://hjelp.ntnu.no/tas/secure/incident?action=lookup&lookup=naam&lookupValue=${match}" target="_blank">${match}</a>`;
});
}

// Replace change matches
if (changeRegex.test(content)) {
newContent = newContent.replace(changeRegex, (match) => {
return `<a href="https://hjelp.ntnu.no/tas/secure/newchange?action=lookup&lookup=number&lookupValue=${match}" target="_blank">${match}</a>`;
});
}

// Only update and copy if there were changes
if (newContent !== content) {
// Copy the updated content to clipboard
copyToClipboard(newContent, content);
} else {
copyToClipboard(null, content);
}
showMessage('\'' + content + '\' copied to clipboard!');
}


// Find the first div tag
const firstDiv = document.querySelector('div');

Expand All @@ -76,32 +113,25 @@
// Only add the event listener if the content contains an incident or change number:
if (incidentRegex.test(content) || changeRegex.test(content)) {
targetDiv.style.cursor = "copy";
targetDiv.addEventListener('click', function() {

let newContent = content;

// Replace incident matches
if (incidentRegex.test(content)) {
newContent = newContent.replace(incidentRegex, (match) => {
return `<a href="https://hjelp.ntnu.no/tas/secure/incident?action=lookup&lookup=naam&lookupValue=${match}" target="_blank">${match}</a>`;
});
}

// Replace change matches
if (changeRegex.test(content)) {
newContent = newContent.replace(changeRegex, (match) => {
return `<a href="https://hjelp.ntnu.no/tas/secure/newchange?action=lookup&lookup=number&lookupValue=${match}" target="_blank">${match}</a>`;
});
}
targetDiv.addEventListener('click', copyIssueNumber);

// Only update and copy if there were changes
if (newContent !== content) {
// Copy the updated content to clipboard
copyToClipboard(newContent, content);
} else {
copyToClipboard(null, content);
// Add keyboard event listener for Ctrl+Alt+C
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.altKey && event.key === 'c') {
// Check if there's an issue number to copy
const firstDiv = document.querySelector('div');
if (firstDiv && firstDiv.id) {
const modifiedId = firstDiv.id.slice(0, -1) + '8';
const targetDiv = document.getElementById(modifiedId);
if (targetDiv) {
const content = targetDiv.textContent || targetDiv.innerText;
if (incidentRegex.test(content) || changeRegex.test(content)) {
event.preventDefault(); // Prevent default behavior
copyIssueNumber();
}
}
}
showMessage('\'' + content + '\' copied to clipboard!');
}
});
}
}
Expand Down