OneDrive E3 E5 下载直链获取方法+油猴脚本

OneDrive E3 E5 下载直链获取方法

原理说明

OneDrive的分享链接格式如下:

https://su4097outlook-my.sharepoint.com/:v:/g/personal/code01_loliloli_xyz/IQCCWZzJ-NHiSrB6eY3_K5lMASnqbHj5tQ0sNPsfCKmrerY?nav=eyJyZWZlcnJhbEluZm8iOnsicmVmZXJyYWxBcHAiOiJPbmVEcml2ZUZvckJ1c2luZXNzIiwicmVmZXJyYWxBcHBQbGF0Zm9ybSI6IldlYiIsInJlZmVycmFsTW9kZSI6InZpZXciLCJyZWZlcnJhbFZpZXciOiJNeUZpbGVzTGlua0NvcHkifX0&e=eWi7eK

在链接末尾添加 `&download=1` 参数后,即可转换为直接下载链接:


https://su4097outlook-my.sharepoint.com/:v:/g/personal/code01_loliloli_xyz/IQCCWZzJ-NHiSrB6eY3_K5lMASnqbHj5tQ0sNPsfCKmrerY?nav=eyJyZWZlcnJhbEluZm8iOnsicmVmZXJyYWxBcHAiOiJPbmVEcml2ZUZvckJ1c2luZXNzIiwicmVmZXJyYWxBcHBQbGF0Zm9ybSI6IldlYiIsInJlZmVycmFsTW9kZSI6InZpZXciLCJyZWZlcnJhbFZpZXciOiJNeUZpbGVzTGlua0NvcHkifX0&e=6HrqMB&download=1

预览直链

个人版onedrive 不适用这个方法

油猴脚本

在OneDrive右键菜单添加":inbox_tray:复制分享直链"按钮,一键获取直接下载链接

完整脚本代码

// ==UserScript==
// @name         OneDrive E3 直链获取
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  在OneDrive右键菜单添加"复制分享直链"按钮,一键获取直接下载链接
// @author       code01
// @license      MIT
// @match        https://*.sharepoint.com/*
// @match        https://*.sharepoint.cn/*
// @grant        GM_setClipboard
// @grant        GM_notification
// @grant        GM_readClipboard
// @noframes
// ==/UserScript==

(function() {
    'use strict';
    /**
     * 处理右键菜单,插入我们的按钮
     * @param {HTMLElement} menu 右键菜单元素
     */
    function processContextMenu(menu) {
        // 避免重复添加
        if (menu.querySelector('.od-direct-link-menu-item')) return;
        // 查找"复制链接"按钮
        const copyLinkItem = Array.from(menu.querySelectorAll('[role="menuitem"], button'))
            .find(el => {
                const text = el.textContent.trim();
                return text === '复制链接' || text === 'Copy link' || text.includes('复制链接');
            });
        if (!copyLinkItem) return;

        // 克隆菜单项
        const ourItem = copyLinkItem.cloneNode(true);
        ourItem.classList.add('od-direct-link-menu-item');

        // 修改文本
        const textEl = ourItem.querySelector('.ms-ContextualMenu-itemText') || ourItem.querySelector('span') || ourItem;
        textEl.textContent = '复制分享直链';

        // 修改图标
        const iconEl = ourItem.querySelector('i, [data-icon-name]');
        if (iconEl) {
            iconEl.setAttribute('data-icon-name', 'Download');
            iconEl.innerHTML = '📥';
        }

        // 点击事件
        ourItem.addEventListener('click', async (e) => {
            e.stopPropagation();
            e.preventDefault();

            try {
                // 点击原有的复制链接按钮
                copyLinkItem.click();

                // 等待剪贴板更新
                await new Promise(r => setTimeout(r, 1500));

                // 读取剪贴板
                let shareUrl = await navigator.clipboard.readText();

                // 转换为直链
                let directUrl;
                if (shareUrl.includes('?')) {
                    directUrl = shareUrl + '&download=1';
                } else {
                    directUrl = shareUrl + '?download=1';
                }

                // 复制直链到剪贴板
                await navigator.clipboard.writeText(directUrl);

                // 关闭菜单
                menu.remove();

            } catch (error) {
                console.error('处理失败:', error);
            }
        });

        // 插入到复制链接按钮后面
        copyLinkItem.parentElement.insertBefore(ourItem, copyLinkItem.nextSibling);
    }

    /**
     * 初始化监视器
     */
    function initObserver() {
        const observer = new MutationObserver((mutations) => {
            mutations.forEach(mutation => {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType !== Node.ELEMENT_NODE) return;

                    // 检查是否是右键菜单
                    if (node.matches('.ms-ContextualMenu, [role="menu"], .ms-ContextualMenu-Callout')) {
                        setTimeout(() => processContextMenu(node), 10);
                    }

                    // 检查子元素
                    const menus = node.querySelectorAll('.ms-ContextualMenu, [role="menu"], .ms-ContextualMenu-Callout');
                    menus.forEach(menu => setTimeout(() => processContextMenu(menu), 10));
                });
            });
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    // 页面加载完成初始化
    window.addEventListener('load', () => {
        if (window.location.host.includes('.sharepoint.')) {
            initObserver();
        }
    });

})();

7 个赞

实测有效,分享链接后加&download=1确实能直接下载

1 个赞