Copy explorer path of Box folder

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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{
        ;
    }

})();