Copy explorer path of Box folder

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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

})();