From 8ddc8911a2eca59efad5a4608dee3e61be0f2bed Mon Sep 17 00:00:00 2001 From: Turbosnute Date: Thu, 22 Aug 2024 12:25:10 +0200 Subject: [PATCH] TopDesk script now generates html links to incidents and changes. It will append both plaintext and html to clipboard. So if you paste it into teams or confluence it will be a link and if you paste it into notepad it will be plain text. --- topdesk.user.js | 65 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/topdesk.user.js b/topdesk.user.js index 43cac69..d079822 100644 --- a/topdesk.user.js +++ b/topdesk.user.js @@ -1,11 +1,12 @@ // ==UserScript== // @name TopDesk - Copy Issuenumber // @namespace http://tampermonkey.net/ -// @version 1.0.2 -// @author on@ntnu.no +// @version 1.1.0 +// @author Øyvind Nilsen (on@ntnu.no) // @description Copies the issue number if you click it in TopDesk // @match https://hjelp.ntnu.no/* // @grant none +// @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 // ==/UserScript== @@ -13,14 +14,29 @@ (function() { 'use strict'; + // Regex patterns + const incidentRegex = /(NTNU[0-9]{7})/gi; + const changeRegex = /(NTNU_C[0-9]{7})/gi; + // Function to copy text to clipboard - function copyToClipboard(text) { - const textarea = document.createElement('textarea'); - textarea.value = text; - document.body.appendChild(textarea); - textarea.select(); - document.execCommand('copy'); - document.body.removeChild(textarea); + 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 @@ -37,7 +53,7 @@ messageBox.style.borderRadius = '5px'; messageBox.style.zIndex = '10000'; messageBox.style.opacity = '1'; - messageBox.style.transition = 'opacity 1s ease-out'; + messageBox.style.transition = 'opacity 0.8s ease-out'; document.body.appendChild(messageBox); setTimeout(() => { @@ -54,10 +70,33 @@ const modifiedId = firstDiv.id.slice(0, -1) + '8'; const targetDiv = document.getElementById(modifiedId); if (targetDiv) { + targetDiv.addEventListener('click', function() { - const content = targetDiv.textContent || targetDiv.innerText; - copyToClipboard(content); - showMessage('\'' + content + '\' copied to clipboard!'); + 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!'); }); } }