Skip to content

Instantly share code, notes, and snippets.

@niikoo
Last active June 28, 2021 17:09
Show Gist options
  • Save niikoo/a49658ed816b35522831031f6a80992b to your computer and use it in GitHub Desktop.
Save niikoo/a49658ed816b35522831031f6a80992b to your computer and use it in GitHub Desktop.
Sharepoint: Add an action button to go 'Up' one folder in the file browser
// ==UserScript==
// @name Sharepoint: Go Up One Folder Action Button
// @namespace https://github.com/niikoo
// @version 0.4.1
// @description Add an action button to go 'Up' one folder in the file browser
// @author niikoo
// @homepage https://gist.github.com/niikoo/a49658ed816b35522831031f6a80992b
// @match https://*.sharepoint.com/*
// @updateURL https://gist.github.com/niikoo/a49658ed816b35522831031f6a80992b/raw/sharepoint-up-actionbtn.user.js
// @downloadURL https://gist.github.com/niikoo/a49658ed816b35522831031f6a80992b/raw/sharepoint-up-actionbtn.user.js
// @supportURL https://gist.github.com/niikoo/a49658ed816b35522831031f6a80992b
// @run-at document-body
// @grant none
// @noframes
// ==/UserScript==
/*
* Changelog
* -
* 0.4.1 * Fix update & download URL
* 0.4.0 * Fix so that it works on more sites
* 0.3.0 * Auto updating; better metadata incl. script run-at.
* 0.2.0 * Fix error message because of missing jQuery by waiting for it. Update metadata.
* 0.1.0 * Initial build
*/
(function() {
'use strict';
function checkIfCustomButtonExists() {
if(window.$("body").find("#UpOneLevelBtn_Custom").length != 0) return true; // Do not create multiple up buttons
return false;
}
function setupCustomUpButton() {
if(checkIfCustomButtonExists()) return;
console.log('Try to add up button');
var currentUrl = window.location.protocol + "//" + window.location.host + window.location.pathname;
if(window.location.search == null) return;
var queryParams = decodeURIComponent(window.location.search).substring(1).split('&');
try {
let pathnameArr = location.pathname.split('/SitePages');
let rootFolderPath = '/';
if(pathnameArr.length === 2) {
rootFolderPath = pathnameArr[0];
}
var rootFolder = queryParams.filter(el => el.startsWith("RootFolder="))[0].substr(11).split('/');
queryParams = queryParams.filter(el => !el.startsWith("RootFolder="));
if(rootFolder.length == 0) {
console.log('Not possible to go more up');
return;
}
rootFolder.pop();
var rootFolderOneUp = "RootFolder=" + rootFolder.join('/');
queryParams.push(rootFolderOneUp);
var newQueryParams = '?' + queryParams.join('&');
var newOneUpUrl = currentUrl + newQueryParams;
var $toolbar = window.$("div#t-container").find("div.ms-wpContentDivSpace[webpartid]").find("div.ms-listview-qcbContainer").find(".ms-qcb-root > ul:eq(0)");
var $liUpOneLevel = window.$("<li class='ms-qcb-item' />");
var $btnUpOneLevel = window.$("<button class='ms-qcb-button ms-qcb-buttons-alignmentfix js-listview-qcbUploadButton js-callout-body js-qcb-button ' type='button' title='Go Up One Level' id='UpOneLevelBtn_Custom' role='button' aria-expanded='false' accesskey='backspace' />");
$btnUpOneLevel.click(function() {
window.location.href = newOneUpUrl;
});
$btnUpOneLevel.text("Up");
var $spanIconUpOneLevel = window.$("<span class='ms-qcb-glyph ms-listview-glyph-withmargin ms-core-form-heading' />");
$spanIconUpOneLevel.html("&#xE047;");
$btnUpOneLevel.prepend($spanIconUpOneLevel);
$liUpOneLevel.append($btnUpOneLevel);
if(checkIfCustomButtonExists()) return;
$toolbar.prepend($liUpOneLevel);
} catch (ex) {
console.warn('Could not go up futher because of: ' + ex);
}
}
let jQueryWaitFunction = function(waitRetries) {
if (window.$ == null) {
if(waitRetries < 0) {
throw new Error("jQuery never appeared...");
}
setTimeout(jQueryWaitFunction(--waitRetries), 100);
} else {
window.$(function() {
setupCustomUpButton();
});
return;
}
}
jQueryWaitFunction(30); // Run on page load
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment