Yavos / Google Drive Direct Links

// ==UserScript==
// @name        Google Drive Direct Links
// @namespace   Yavos' Toolbox
// @include     https://drive.google.com/drive/*
// @version     1
// @grant       none
// @description Adds a little text field to the details tab, showing a direct link to your file. (Will only work if you shared the containing folder for everybody in the internet.)
// @copyright   2014, Yavos
// @oujs:author Yavos
// ==/UserScript==


config = { attributes: true, childList: false, characterData: false, subtree: true };

var observer1 = new MutationObserver(function(mutations) { //wait for page to be loaded
    mutations.forEach(function(mutation) {
        
        if (mutation.target.getAttribute("role") && mutation.target.getAttribute("role") == "main" && mutation.target.style.display != "none") {
            details = document.getElementsByClassName("a-oa-ib-jd-Y-H a-oa-ib-jd-Y-H-ql a-oa-ib-Qh-H a-l-Kd-n");
            for (i = 0; i < details.length; i++) {
                if (details[i].parentNode.parentNode.parentNode.parentNode.parentNode.style.display != "none" && details[i].style.display != "none") innerDetails = details[i].childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[0];
            }
            
            detailsHeader = innerDetails.childNodes[0];
            detailsBody = innerDetails.childNodes[1];

            insertDiv = detailsBody.childNodes[0].childNodes[0].childNodes[0];

            label = detailsHeader.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[1].innerHTML;

            copy = document.createElement("input");
            copy.type = "text";
            copy.style = "width: 90%";
            folderID = document.location.href.split("/").reverse()[0];
            copy.value = "https://googledrive.com/host/" + folderID + "/" + label;
            copy.addEventListener("click", function(e) {e.target.setSelectionRange(0, e.target.value.length);});
            newDiv = document.createElement("div");
            newDiv.style = "width: 100%";
            
            if (innerDetails.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].getAttribute("aria-label") != "Freigegebene Google Drive-Ordner-Datei") {                
                newDiv.appendChild(document.createTextNode("Copy this for direct link:"));
                newDiv.appendChild(document.createElement("br"));
                newDiv.appendChild(copy);
                
                insertDiv.appendChild(newDiv);
            }
            target = details[0].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;

            observer2.observe(target, config);
            observer1.disconnect();
        }
  });    
});

var observer2 = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        if (mutation.attributeName == "style") {
            if (mutation.target.getAttribute("aria-hidden")) { // <- tested to be the most reliable so far
                details = document.getElementsByClassName("a-oa-ib-jd-Y-H a-oa-ib-jd-Y-H-ql a-oa-ib-Qh-H a-l-Kd-n");
                for (i = 0; i < details.length; i++) {
                    if (details[i].parentNode.parentNode.parentNode.parentNode.parentNode.style.display != "none" && details[i].style.display != "none") {
                        innerDetails = details[i].childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[0];
                        break;
                    }
                }
                if (insertDiv && insertDiv.childNodes[insertDiv.childNodes.length-1] == newDiv) {
                    insertDiv.removeChild(newDiv);
                }
                if (innerDetails.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].getAttribute("aria-label") != "Freigegebene Google Drive-Ordner-Datei") {
                    detailsHeader = innerDetails.childNodes[0];
                    detailsBody = innerDetails.childNodes[1];
                    
                    insertDiv = detailsBody.childNodes[0].childNodes[0].childNodes[0];
                    
                    label = detailsHeader.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[1].innerHTML;

                    folderID = document.location.href.split("/").reverse()[0];
                    copy.value = "https://googledrive.com/host/" + folderID + "/" + label;
                    
                    newDiv = document.createElement("div");
                    newDiv.appendChild(document.createTextNode("Copy this for direct link:"));
                    newDiv.appendChild(document.createElement("br"));
                    newDiv.appendChild(copy);

                    insertDiv.appendChild(newDiv);
                }
            }
        }
  });    
});

observer1.observe(document.getElementById("drive_main_page"), config);