Copy explorer path of Box folder

Add a button on Box website that can copy explorer path of Box folder.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name Copy explorer path of Box folder
// @description Add a button on Box website that can copy explorer path of Box folder.
// @namespace https://github.com/kevinzch/CopyExplorerPathOfBoxFolder
// @version 0.4
// @license MIT
// @author Kevin
// @include https://app.box.com/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://greasyfork.org/scripts/383527-wait-for-key-elements/code/Wait_for_key_elements.js?version=701631
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @run-at document-body
// ==/UserScript==

(function() {
    'use strict';

    let itemType = null;

    const TYPE_FOLDER = 0;
    const TYPE_FILE = 1;

    window.addEventListener('load', () => {
        copyExplorerPath();
    })

    function copyExplorerPath() {
        try {
            // Create button and set button style
            let copyBtn = document.createElement('button');

            let searchBox = document.querySelector('.header-search.prevent-item-deselection.HeaderSearch-isNewQuickSearch');

            let itemName = document.querySelector('.item-list-date.item-list-cell');

            // Make basis of url
            const apiUrl = 'app-api/enduserapp/folder/';
            let appHost = Box.prefetchedData['/app-api/enduserapp/current-user'].preview.appHost;

            // Request
            let request = new XMLHttpRequest();

            // Empty Variables which will be reused in click event
            let folderId = '';
            let fullUrl = '';
            let explorerPath = '';
            let jsonObj = null;
            let length = 0;

            // Set button style
            copyBtn.textContent = 'Copy path';
            copyBtn.style.backgroundColor = '#4baf4f';
            copyBtn.style.color = 'white';
            copyBtn.style.borderRadius = '8px';
            copyBtn.style.padding = '0px 20px';

            if ( searchBox != null ){
                itemType = TYPE_FOLDER;
            }
            else if( itemName != null ){
                itemType = TYPE_FILE;
            }
            else{
                ;
            }

            // Add button to document
            if ( itemType == TYPE_FOLDER ){
                searchBox.appendChild(copyBtn);
            }
            else if( itemType == TYPE_FILE ){
                itemName.appendChild(copyBtn);
            }
            else{
                ;
            }

            searchBox = null;
            itemName = null;

            // Add button click listner
            copyBtn.addEventListener('click', function(){

                // Reget folderID and remake full url
                if ( itemType == TYPE_FOLDER ){
                    folderId = document.URL.split('/').pop();
                }
                else{
                    folderId = document.querySelector('.parent-name').href.split('/').pop();
                }

                fullUrl = appHost + apiUrl + folderId;

                // Clear explorer path
                explorerPath = 'Box';

                request.open('GET', fullUrl, false);
                request.send();
                jsonObj = JSON.parse(request.responseText);

                length = jsonObj.folder.path.length;

                for (let i = 0; i < length; i++){
                    // Skip 'All files'
                    if (i == 0){
                        continue;
                    }
                    else{
                        explorerPath += '\\' + jsonObj.folder.path[i].name;
                    }
                }
                navigator.clipboard.writeText(explorerPath);
                alert("下記のパスをコピーしました:\r\n" + explorerPath);
            })

        }
        catch (e) {
            setTimeout(() => {
                copyExplorerPath();
            }, 500);
        }
    };

    if ( itemType == TYPE_FOLDER ){
        waitForKeyElements(".parent-name", copyExplorerPath());
    }
    else{
        ;
    }

})();