Skip to content

Commit

Permalink
TopDesk script now generates html links to incidents and changes. It …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
Turbosnute committed Aug 22, 2024
1 parent fe9e436 commit 8ddc891
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions topdesk.user.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
// ==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==

(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
Expand All @@ -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(() => {
Expand All @@ -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 `<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!');
});
}
}
Expand Down

0 comments on commit 8ddc891

Please sign in to comment.