From f24e9727ad9c00e3d51a325efdec541eb31698fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Andreas=20Nilsen?= Date: Wed, 16 Jul 2025 10:38:18 +0200 Subject: [PATCH] added hotkey CTRL+ALT+C to copy topdesk issue numbers --- topdesk.user.js | 82 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/topdesk.user.js b/topdesk.user.js index d1ed5b0..e50f5f6 100644 --- a/topdesk.user.js +++ b/topdesk.user.js @@ -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 @@ -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 `${match}`; + }); + } + + // Replace change matches + if (changeRegex.test(content)) { + newContent = newContent.replace(changeRegex, (match) => { + return `${match}`; + }); + } + + // 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'); @@ -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 `${match}`; - }); - } - - // Replace change matches - if (changeRegex.test(content)) { - newContent = newContent.replace(changeRegex, (match) => { - return `${match}`; - }); - } + 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!'); + } }); } }