Greasy Fork

atlassian license bulk copy

Inserts a textarea with basic license info in csv format for easy export

目前为 2024-12-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         atlassian license bulk copy
// @namespace    http://tampermonkey.net/
// @version      2024.12.11.5
// @description  Inserts a textarea with basic license info in csv format for easy export
// @author       You
// @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;
            }
            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(',', '');
            let t = lic.querySelector('textarea').textContent.trim().replaceAll(',', '');
            let split_name = name.split(' ');
            let user_count_index = split_name.lastIndexOf('Users:')-1;
            var user_count = '';
            if (user_count_index > 0) {
                user_count = split_name.at(split_name.lastIndexOf('Users:')-1);
            }

            let line = `${user_count},${org},${name},${expiry},${sen},${t}`.replaceAll('\n', '');
            document.getElementById('tsx-csv').textContent += 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);
        }
        let form = document.querySelector("#your-licenses > section.product-list.show-paid > form");
        let d = document.createElement("div");
        d.id = 'tsx-div';
        let csv = document.createElement("textarea");
        let btn = document.createElement("button");
        let btn2 = document.createElement("button");
        csv.id = 'tsx-csv';
        csv.textContent = 'user_count,owner,app,expiry,sen,license\n';
        btn.textContent = 'Get Licenses';
        btn.addEventListener("click", function (e) {
            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()) {
                    let x = (n) => {
                        n.querySelector('td > span').click();
                        setTimeout(printlicense, 1000, n);
                    }

                    window.TSX_license_count +=1 ;
                    if (window.TSX_license_count%2==0) {
                        wait += 1000;
                    }
                    console.log(1000 + wait);
                    setTimeout(x, 1200 * window.TSX_license_count, name);
                };
            };
            setTimeout(licenseDone, 1000);
        });
        btn2.textContent = 'copy csv';
        btn2.addEventListener("click", function (e) {
            navigator.clipboard.writeText(csv.textContent);
        });
        d.appendChild(csv);
        d.appendChild(btn);
        d.appendChild(btn2);
        form.after(d);
        window.TSX_license_count = 0;
    });
})();