// ==UserScript==
// @name atlassian license bulk copy
// @namespace http://tampermonkey.net/
// @version 2024.12.11.14
// @description Inserts a textarea with basic license info in csv format for easy export
// @author gwelch-contegix
// @match https://my.atlassian.com/product
// @match https://my.atlassian.com/products/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=atlassian.com
// @grant none
// @license MIT
// ==/UserScript==
(function () {
"use strict"
window.addEventListener("load", (event) => {
function printlicense(n) {
let lic = n.nextElementSibling
if (
n.querySelector("td span.account-name") == null ||
lic.querySelector("textarea") == null ||
lic.querySelector("#sen") == null
) {
setTimeout(printlicense, 1000, n)
return
}
function getLicenseCountAndPluginName(name) {
let split_name = name.split(" ")
let data = split_name.indexOf("(Data")
var app_name = ""
if (split_name[data + 1] == "Center)") {
let plugin = split_name.lastIndexOf("for", data)
if (plugin < 0) {
app_name = split_name.slice(0, data).join(" ")
} else {
app_name = split_name[plugin + 1]
}
}
var license_count_index = split_name.lastIndexOf("Users:") - 1
var license_count = ""
if (license_count_index > 0) {
license_count = split_name.at(license_count_index)
} else {
license_count_index = split_name.lastIndexOf("Agents:") - 1
if (license_count_index > 0) {
license_count = split_name.at(license_count_index)
if (license_count == "Remote") {
license_count = split_name.at(license_count_index - 1)
}
}
}
return [app_name, license_count]
}
let org = n
.querySelector("td span.account-name")
.textContent.trim()
.replaceAll(",", "")
let name = n
.querySelector("span.desc")
.textContent.trim()
.replaceAll(",", "")
let expiry = n
.querySelector("td.support-expiry-date")
.textContent.trim()
.replaceAll(",", "")
let sen = lic.querySelector("#sen").textContent.trim().replaceAll(",", "")
var product_key = lic.querySelector("table.account-detail").getAttribute('data-product-key')
if (product_key.endsWith(".data-center")) {
product_key = product_key.slice(0, -12)
}
let t = lic
.querySelector("textarea")
.value.trim()
.replaceAll(",", "")
let [app_name, license_count] = getLicenseCountAndPluginName(name)
let line =
`${license_count},${org},${name},${app_name},${product_key},${expiry},${sen},${t}`.replaceAll(
"\n",
"",
)
document.getElementById("tsx-csv").value += line + "\n"
console.log(line)
window.TSX_license_count -= 1
}
function notify(message, time) {
var popup = $(`<div >${message}</div>`)
$("body").append(popup)
// Style the popup (you can customize this)
popup.css({
position: "fixed",
bottom: "0%",
right: "0%",
// 'transform': 'translate(-50%, -50%)',
"background-color": "red",
padding: "10px",
border: "1px solid black",
"z-index": "1000",
})
// Show the popup
popup.fadeIn()
// Hide the popup after a delay (e.g., 3 seconds)
setTimeout(function () {
popup.fadeOut()
setTimeout(function () {
popup.remove()
}, 50)
}, time)
}
function licenseDone() {
if (window.TSX_license_count > 0) {
console.log("waiting for csv to complete " + window.TSX_license_count)
notify(
"waiting for csv to complete " + window.TSX_license_count + " left",
1050,
)
setTimeout(licenseDone, 1000)
return
}
console.log("csv is done")
notify("csv is done", 5000)
let d = document.getElementById("tsx-div")
let p = document.createElement("p")
p.textContent = "csv is done"
d.appendChild(p)
}
function click(n) {
n.querySelector("td > span").click()
}
function download(file, text) {
let element = document.createElement("a")
element.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(text),
)
element.setAttribute("download", file)
document.body.appendChild(element)
element.click()
document.body.removeChild(element)
}
function getlicenses() {
var wait = 0
for (let name of document
.querySelectorAll(
"#your-licenses > section.product-list.show-paid > table > tbody > tr.headingRow",
)
.values()
.toArray()
.reverse()) {
if (name.checkVisibility()) {
window.TSX_license_count += 1
if (window.TSX_license_count % 2 == 0) {
wait += 1000
}
setTimeout(click, 1200 * window.TSX_license_count, name)
printlicense(name)
}
}
licenseDone()
}
let form = document.querySelector(
"#your-licenses > section.product-list.show-paid > form",
)
let div = document.createElement("div")
div.id = "tsx-div"
let csv = document.createElement("textarea")
let btnGetLicenses = document.createElement("button")
let btnDownloadCSV = document.createElement("button")
csv.id = "tsx-csv"
csv.value = "license_count,owner,app,app_name,product_key,expiry,sen,license\n"
$(csv).css({
display: "block",
width: "100%",
})
$(div).css({
margin: "0px 6px 0 0",
})
btnGetLicenses.textContent = "Get Licenses"
btnGetLicenses.addEventListener("click", getlicenses)
btnDownloadCSV.textContent = "download csv"
btnDownloadCSV.addEventListener("click", function (e) {
download("licenses.csv", csv.value)
})
div.appendChild(csv)
div.appendChild(btnGetLicenses)
div.appendChild(btnDownloadCSV)
form.after(div)
window.TSX_license_count = 0
})
})()