Skip to content

Instantly share code, notes, and snippets.

@nuest
Forked from LukasLohoff/README.md
Created March 27, 2017 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuest/f49ff0cd46583acf36f9f5e8a2f0ca8f to your computer and use it in GitHub Desktop.
Save nuest/f49ff0cd46583acf36f9f5e8a2f0ca8f to your computer and use it in GitHub Desktop.
Userscript adding a "upload to o2r" button to sciebo(owncloud)

"Upload to O2R" - Userscript for Sciebo

Requirements

  • Firefox or Chrome Web Browser

Installation

  1. Install a browser extension to support userscripts

  2. Create a new Userscript and replace it with the code from below

Alternative

  • open your browser's console (F12 and switch to the console tab)
  • copy and paste the JavaScript code into the console

Known issues

  • sometimes a page refresh is necessary to add the O2R button
// ==UserScript==
// @name Sciebo o2r integration
// @namespace o2r
// @author Lukas Lohoff
// @include https://uni-muenster.sciebo.de/*
// @grant none
// @version 0.10
// ==/UserScript==
var list;
function addButton() {
var menu = document.getElementsByClassName("fileActionsMenu popovermenu bubble open menu")[0];
list = menu.getElementsByTagName("UL")[0];
var node = document.createElement("LI");
var link = document.createElement('a');
link.setAttribute('href', "#");
link.className = "menuitem action action-download permanent";
var imageSpan = document.createElement("SPAN");
imageSpan.className = "icon icon-download";
var textSpan = document.createElement("SPAN");
textSpan.innerHTML = "Open as ERC";
link.appendChild(imageSpan);
link.appendChild(textSpan);
node.appendChild(link);
list.appendChild(node);
link.addEventListener("click", function(event) {
getPublicShare();
event.preventDefault();
});
}
function findParentNodeWithName(name, node) {
if (node.parentNode.nodeName === "BODY")
console.log("Did not find parent node with name " + name);
if (node.parentNode.nodeName === name){
return node.parentNode;
} else {
return findParentNodeWithName(name, node.parentNode);
}
}
function getPublicShare() {
// 1st, get clicked folder name:
//var x = document.getElementById("myLI").parentNode.nodeName;
var tempURL = window.location.href;
var folder = findParentNodeWithName('TR', list);
var folderName = folder.getAttribute('data-file');
var fPath = decodeURIComponent(window.location.href.split("dir=")[1]);
var folderPath = '';
if (fPath === '/' || fPath === undefined || fPath === "undefined" ) { // if currently in base direcotry you just need the filename
folderPath = folderName;
} else { //otherwise you need the full path of that folder
folderPath = fPath + '/' + folderName;
}
console.log("folderpath: " + folderPath);
// get other attributes such as id, size, type, shareType, ... here
// throw error if share type is wrong
// 2nd, do http request to get the shareURL and path:
var requestURL = "https://uni-muenster.sciebo.de/ocs/v2.php/apps/files_sharing/api/v1/shares?format=json&path=" + encodeURIComponent("/" + folderPath);
requestURL += "&reshares=true";
console.log(requestURL);
console.log("token: " + oc_requesttoken);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
var result = JSON.parse(xhr.responseText);
if (result.ocs.data.length === 0) {
alert('Could not get public share for this folder, please make sure to enable sharing ("Share link")');
throw new Error('Could not get public share for this folder, please make sure to enable sharing ("Share link")');
}
var shareURL = result.ocs.data[0].url;
var path = '/'; //clicking on a
var finalURL = "https://o2r.uni-muenster.de/#!/home?shareURL=" + shareURL + "&path=" + path;
console.log(finalURL);
openO2RPage(finalURL);
}
};
xhr.open('GET', requestURL, true);
xhr.setRequestHeader('requesttoken', oc_requesttoken);
xhr.setRequestHeader('accept', '*/*');
xhr.setRequestHeader('OCS-APIREQUEST', true);
xhr.send(null);
}
function openO2RPage(url) {
//var hlink = document.createElement('a');
//hlink.setAttribute('href', url);
//hlink.click();
window.open(url);
//window.location.href = url;
}
function addClickEvent() {
var anchors = document.getElementsByClassName("action action-menu permanent");
for(var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.onclick = function() {
addButton();
};
}
}
window.addEventListener('load', function() {
addClickEvent();
}, false);
// wait for async elements to load
setTimeout(addClickEvent, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment