Google Drive Direct Links

Direct link functionality for Google Drive

当前为 2016-08-25 提交的版本,查看 最新版本

// ==UserScript==
// @name            Google Drive Direct Links
// @version         1.3
// @description     Direct link functionality for Google Drive
// @author          Josh Bjelovuk
// @match           *://drive.google.com/*
// @grant           none
// @namespace       https://greasyfork.org/users/11679
// @contributionURL https://goo.gl/dYIygm
// ==/UserScript==

var isSharedFolder = false;

var observer = new MutationObserver(function(mutations) {

    mutations.forEach(function(mutation) {
        for (var i = 0; i < mutation.addedNodes.length; i++) {
            var node = mutation.addedNodes[i];

            // Folder navigation
            if (node.classList.contains('a-s-tb-pa') && !node.attributes.role) {
                var toggleSharedState = function() {
                    setTimeout(function() {
                        var sharedIcon = node.getElementsByClassName("a-o-H-wc")[0];
                        isSharedFolder = !!sharedIcon && sharedIcon.style.display !== 'none';
                    }, 500);
                };
                toggleSharedState();
                var observer = new MutationObserver(function(mutations) {
                    if (node.style.display !== 'none')
                        toggleSharedState();
                });
                observer.observe(node, { attributes: true });
            }

            if (node.dataset.target === 'linkBubble') {
                var link = node.getElementsByTagName('input')[0];
                var directLink = link.cloneNode(true);
                directLink.classList.remove('H-qa-A-zt');
                if (isSharedFolder) {
                    var folderId = location.href.slice(location.href.lastIndexOf('/') + 1);
                    var file = node.previousSibling;
                    var fileName = (file.getElementsByClassName('l-u-V')[0] || file.getElementsByClassName('l-t-T-V')[0]).attributes['aria-label'].value;
                    directLink.value = 'https://googledrive.com/host/'+ folderId +'/'+ encodeURIComponent(fileName);
                }
                else
                    directLink.value = 'https://googledrive.com/host/'+ node.previousSibling.dataset.id;
                directLink.onclick = function() { this.select(); };
                var label = document.createElement('p');
                label.style.cssText = "margin-top: 0px; margin-bottom: 0px;";
                label.textContent = 'Direct link:';
                link.parentNode.insertBefore(directLink, link.nextSibling);
                link.parentNode.insertBefore(label, link.nextSibling);
                break;
            }
            else
                setClickEvent(node);
        }
    });
});
var content = document.getElementById('drive_main_page');
if (content)
    observer.observe(content, { childList: true, subtree: true, attributes: true });

function setClickEvent(elem) {
    if (elem.classList && (elem.classList.contains('a-u-xb-j') || elem.classList.contains('a-t-J')))
        elem.addEventListener('contextmenu', adjustMenu);
    else {
        for (var i = 0; i < elem.children.length; i++)
            setClickEvent(elem.children[i]);
    }
}

function adjustMenu() {
    var file = this;

    setTimeout(function() {
        var menus = document.getElementsByClassName('h-w');

        for (var i = 0; i < menus.length; i++) {
            var menu = menus[i];
            if (menu.style.display !== 'none') {
                var existing = document.getElementById('DLID');
                if (existing)
                    existing.remove();

                var clone = menu.children[0].cloneNode(true);
                clone.id = 'DLID';
				clone.style.display = 'block';
				clone.className = 'h-v';
                clone.getElementsByClassName('a-v-T')[0].innerHTML = 'Open direct';

                clone.onmouseleave = clone.onmouseenter = function() {
                    this.classList.toggle('h-v-pc');
                };
                clone.onclick = function() {
                    var url;
                    if (isSharedFolder) {
                        var folderId = location.href.slice(location.href.lastIndexOf('/') + 1);
                        var fileName = (file.getElementsByClassName('l-u-V')[0] || file.getElementsByClassName('l-t-T-V')[0]).attributes['aria-label'].value;
                        url = 'https://googledrive.com/host/'+ folderId +'/'+ encodeURIComponent(fileName);
                    }
                    else
                        url = 'https://googledrive.com/host/'+ file.dataset.id;

                    window.open(url);
                };

                menu.insertBefore(clone, menu.children[1]);
                break;
            }
        }
    });
}