Copy explorer path of Box folder

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

  1. // ==UserScript==
  2. // @name Copy explorer path of Box folder
  3. // @description Add a button on Box website that can copy explorer path of Box folder.
  4. // @namespace https://github.com/kevinzch/CopyExplorerPathOfBoxFolder
  5. // @version 0.4
  6. // @license MIT
  7. // @author Kevin
  8. // @include https://app.box.com/*
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
  10. // @require https://greasyfork.org/scripts/383527-wait-for-key-elements/code/Wait_for_key_elements.js?version=701631
  11. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  12. // @grant none
  13. // @run-at document-body
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. let itemType = null;
  20.  
  21. const TYPE_FOLDER = 0;
  22. const TYPE_FILE = 1;
  23.  
  24. window.addEventListener('load', () => {
  25. copyExplorerPath();
  26. })
  27.  
  28. function copyExplorerPath() {
  29. try {
  30. // Create button and set button style
  31. let copyBtn = document.createElement('button');
  32.  
  33. let searchBox = document.querySelector('.header-search.prevent-item-deselection.HeaderSearch-isNewQuickSearch');
  34.  
  35. let itemName = document.querySelector('.item-list-date.item-list-cell');
  36.  
  37. // Make basis of url
  38. const apiUrl = 'app-api/enduserapp/folder/';
  39. let appHost = Box.prefetchedData['/app-api/enduserapp/current-user'].preview.appHost;
  40.  
  41. // Request
  42. let request = new XMLHttpRequest();
  43.  
  44. // Empty Variables which will be reused in click event
  45. let folderId = '';
  46. let fullUrl = '';
  47. let explorerPath = '';
  48. let jsonObj = null;
  49. let length = 0;
  50.  
  51. // Set button style
  52. copyBtn.textContent = 'Copy path';
  53. copyBtn.style.backgroundColor = '#4baf4f';
  54. copyBtn.style.color = 'white';
  55. copyBtn.style.borderRadius = '8px';
  56. copyBtn.style.padding = '0px 20px';
  57.  
  58. if ( searchBox != null ){
  59. itemType = TYPE_FOLDER;
  60. }
  61. else if( itemName != null ){
  62. itemType = TYPE_FILE;
  63. }
  64. else{
  65. ;
  66. }
  67.  
  68. // Add button to document
  69. if ( itemType == TYPE_FOLDER ){
  70. searchBox.appendChild(copyBtn);
  71. }
  72. else if( itemType == TYPE_FILE ){
  73. itemName.appendChild(copyBtn);
  74. }
  75. else{
  76. ;
  77. }
  78.  
  79. searchBox = null;
  80. itemName = null;
  81.  
  82. // Add button click listner
  83. copyBtn.addEventListener('click', function(){
  84.  
  85. // Reget folderID and remake full url
  86. if ( itemType == TYPE_FOLDER ){
  87. folderId = document.URL.split('/').pop();
  88. }
  89. else{
  90. folderId = document.querySelector('.parent-name').href.split('/').pop();
  91. }
  92.  
  93. fullUrl = appHost + apiUrl + folderId;
  94.  
  95. // Clear explorer path
  96. explorerPath = 'Box';
  97.  
  98. request.open('GET', fullUrl, false);
  99. request.send();
  100. jsonObj = JSON.parse(request.responseText);
  101.  
  102. length = jsonObj.folder.path.length;
  103.  
  104. for (let i = 0; i < length; i++){
  105. // Skip 'All files'
  106. if (i == 0){
  107. continue;
  108. }
  109. else{
  110. explorerPath += '\\' + jsonObj.folder.path[i].name;
  111. }
  112. }
  113. navigator.clipboard.writeText(explorerPath);
  114. alert("下記のパスをコピーしました:\r\n" + explorerPath);
  115. })
  116.  
  117. }
  118. catch (e) {
  119. setTimeout(() => {
  120. copyExplorerPath();
  121. }, 500);
  122. }
  123. };
  124.  
  125. if ( itemType == TYPE_FOLDER ){
  126. waitForKeyElements(".parent-name", copyExplorerPath());
  127. }
  128. else{
  129. ;
  130. }
  131.  
  132. })();