gabrielsroka / gabrielsroka.github.io

My website, including rockstar: Export Okta Users, Groups, etc. to CSV. Show SAML assertion.
https://gabrielsroka.github.io/
MIT License
89 stars 37 forks source link

Use local authToken for auth and add support for patch policy download #44

Closed pro4tlzz closed 1 year ago

pro4tlzz commented 1 year ago

testing by creating a bookmark with

javascript:(function () {
    const paths = location.pathname.split('/');
    const folder = paths.length > 2 ? '/' + paths[1] : '';

    if (location.pathname.match('/smartComputerGroups.html')) {
        var obj = 'computergroup';
        var path = 'computergroups';
    } else if (location.pathname.match('/policies.html')) {
        obj = 'policy';
        path = 'policies';
    } else if (location.pathname.match('/patchDeployment.html')) {
        obj = 'patch-policy';
        path = 'patchpolicies';
    } else {
        return;
    }

    const id = new URLSearchParams(location.search).get('id');
    if (!id) {
        alert('pick a ' + obj + ' first');
        return;
    }

    function getAccessToken() {
        const authInfo = JSON.parse(localStorage.authToken);
        if (authInfo) {
            return authInfo.token;     
        } else {
            prompt('Couldn\'t find token, maybe refresh page');
        }
    }

    async function getObject(accessToken) {
        url = folder + '/JSSResource/' + path + '/id/' + id;
        headers = {
            'accept': 'application/xml',
            'content-type': 'applicaton/xml',
            'authorization': 'Bearer ' + accessToken
        };
        response = await fetch(url, {headers});
        const str = await response.text();
        const indented = indentXml(str, 4);
        const filename = obj + '-' + id + '.xml';
        download(indented, 'text/xml', filename);
    }

    function addDownloadButton(obj){

        var newButton = document.createElement('download-button');
        newButton.innerHTML = '<div [j-navigation-tooltip]="{ shortcut: \'CMD-D\' }" > <button id="download-button" type="button" class="download-jamf-button-rockstar" translate="DOWNLOAD" onclick=myFunction()>Download item</button></div>';
        if (obj == 'policy') {
            var downloadButton = document.querySelector("#main-body-wrapper > div > div > jp-router-outlet-holder > jp-inner-frame > iframe").contentWindow.document.getElementsByClassName('desktop-buttons')[0].appendChild(newButton);
        } else if (obj == 'computergroup' ) {
            var downloadButton = document.querySelector("#main-ui-view > div > div > jamf-legacy-view > iframe").contentWindow.document.getElementsByClassName('desktop-buttons')[0].appendChild(newButton);
        } else if (obj == 'patch-policy') {
            var downloadButton = document.querySelector("#main-ui-view > div > div > div > jamf-legacy-view > iframe").contentWindow.document.getElementsByClassName('desktop-buttons')[0].appendChild(newButton);
        }
        console.log(downloadButton);
        downloadButton.addEventListener("click", myFunction);
    }

    function indentXml(xml, size) {
        const lines = xml.replace(/></g, '>\n<').split('\n');
        var level = 0;
        for (var i = 0; i < lines.length; i++) {
            var line = lines[i];
            var end = line.match('</');
            var empty = line.match('/>') || line.match(/>.*</);
            if (end && !empty) level--;
            lines[i] = ' '.repeat(size * level) + line;
            if (!end && !empty) level++;
        }
        return lines.join('\n');
    }

    function download(content, type, filename) {
        const a = document.body.appendChild(document.createElement('a'));
        a.href = URL.createObjectURL(new Blob([content], {type}));
        a.download = filename;
        a.click();
    }

    function myFunction() {
        const accessToken = getAccessToken();
        getObject(accessToken);
    }

    addDownloadButton(obj);

})();

Issues:

An exception gets raised from the onclick hander

Uncaught ReferenceError: myFunction is not defined
    at HTMLButtonElement.onclick (patchDeployment.html?softwareTitleId=xx&id=xx&o=r:1:1)

Demo:

https://user-images.githubusercontent.com/22709115/194089808-1a9bc389-2094-476d-88ff-9839c161997a.mov

https://user-images.githubusercontent.com/22709115/194089847-bd95dfef-870d-4737-8ff6-ad6a11e8bbe3.mov

https://user-images.githubusercontent.com/22709115/194089857-d3eb3484-fc2b-4cb3-8f18-73cd14b9e2bf.mov