Skip to content

Commit

Permalink
Merge pull request #21 from M365-Drift/topdesk-bettercopy
Browse files Browse the repository at this point in the history
Fixed issue where a blank issue was copied when you clicked the refre…
  • Loading branch information
on authored and GitHub Enterprise committed Feb 26, 2025
2 parents 2b8e905 + e724a06 commit a5ac916
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions topdesk.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name TopDesk - Copy Issuenumber
// @namespace https://git.ntnu.no/M365-Drift/MonkeyMagic/
// @version 1.1.3
// @version 1.1.4
// @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.
// @match https://hjelp.ntnu.no/tas/secure/mango/window/*
Expand Down Expand Up @@ -72,34 +72,38 @@
const targetDiv = document.getElementById(modifiedId);

if (targetDiv) {
targetDiv.style.cursor = "copy";
targetDiv.addEventListener('click', function() {
const content = targetDiv.textContent || targetDiv.innerText;
let newContent = content;
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', 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 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>`;
});
}
// 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!');
});
// 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!');
});
}
}
}
})();

0 comments on commit a5ac916

Please sign in to comment.