diff --git a/topdesk.user.js b/topdesk.user.js index e50f5f6..00228f9 100644 --- a/topdesk.user.js +++ b/topdesk.user.js @@ -1,11 +1,12 @@ // ==UserScript== // @name TopDesk - Copy Issuenumber // @namespace https://git.ntnu.no/M365-Drift/MonkeyMagic/ -// @version 1.2.0 +// @version 1.2.3 // @author Øyvind Nilsen (on@ntnu.no) // @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 +// @run-at document-end // @icon https://www.google.com/s2/favicons?sz=64&domain=topdesk.com // @updateURL https://git.ntnu.no/M365-Drift/MonkeyMagic/raw/main/topdesk.user.js // @downloadURL https://git.ntnu.no/M365-Drift/MonkeyMagic/raw/main/topdesk.user.js @@ -17,6 +18,7 @@ // Regex patterns const incidentRegex = /(NTNU[0-9]{7})/gi; const changeRegex = /(NTNU_C[0-9]{7})/gi; + //const issueRegex = /(NTNU(?:_C)?[0-9]{7})/gi; // Function to copy text to clipboard function copyToClipboard(html, text) { @@ -42,40 +44,60 @@ // Function to show a fading message box function showMessage(message) { const messageBox = document.createElement('div'); - messageBox.textContent = message; - messageBox.style.position = 'fixed'; + //messageBox.style.position = 'fixed'; messageBox.style.top = '20px'; messageBox.style.left = '50%'; - messageBox.style.transform = 'translateX(-50%)'; messageBox.style.backgroundColor = '#333'; - messageBox.style.color = '#fff'; + 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'; // Prevent overflow on small screens + messageBox.style.textAlign = 'center'; // Center the text + messageBox.style.display = 'block'; + messageBox.style.boxShadow = '0 2px 10px rgba(0,0,0,0.3)'; + + // Create spans with reset styles too + 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 = ' copied to clipboard!'; + + messageBox.appendChild(boldSpan); + messageBox.appendChild(normalSpan); + document.body.appendChild(messageBox); setTimeout(() => { messageBox.style.opacity = '0'; setTimeout(() => { - document.body.removeChild(messageBox); + if (messageBox.parentNode) { + document.body.removeChild(messageBox); + } }, 900); }, 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); + function copyIssueNumber(targetDiv) { + //const targetDiv = document.querySelector('div[id$="8"]'); + if (!targetDiv) return; - + const content = targetDiv.textContent || targetDiv.innerText; let newContent = content; - + // Replace incident matches if (incidentRegex.test(content)) { newContent = newContent.replace(incidentRegex, (match) => { @@ -97,43 +119,30 @@ } else { copyToClipboard(null, content); } - showMessage('\'' + content + '\' copied to clipboard!'); + showMessage(content); } - // Find the first div tag - 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; - // 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', copyIssueNumber); - - // 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(); - } - } - } - } - }); - } + const targetDiv = document.querySelector('div[id$="8"]'); + + if (targetDiv) { + const content = targetDiv.textContent || targetDiv.innerText; + // Only add the event listener if the content contains an incident or change number: + if (incidentRegex.test(content) || changeRegex.test(content)) { + //event.preventDefault(); + //event.stopPropagation(); + targetDiv.style.cursor = "copy"; + targetDiv.addEventListener('click', function () { + copyIssueNumber(this); + }); + + // Add keyboard event listener for Ctrl+Alt+C + document.addEventListener('keydown', function(event) { + if (event.ctrlKey && event.altKey && event.key === 'c') { + copyIssueNumber(targetDiv); + } + }); } } + })(); \ No newline at end of file