// ==UserScript== // @name GitHub 文件夹下载 // @namespace https://github.com/the-eric-kwok/my_userscripts // @version 0.4 // @description 为 GitHub 文件夹增加一个下载按钮,可以方便地下载某个文件夹 // @author EricKwok // @supportURL https://github.com/the-eric-kwok/my_userscripts/issues // @match *://github.com/* // @icon https://i.loli.net/2021/03/30/ULV9XunaHesqGIR.png // @run-at document-idle // @grant none // @license GPLv3 // @downloadURL none // ==/UserScript== var isFold = false; // 注入下载文件夹按钮 function injectDownloadFolderBtn() { if (document.querySelector('.EricKwok')) return; if (isFold) { let devider = document.querySelector("details.d-block > div > ul > li.dropdown-divider"); if (devider) { let _html = `
  • download-directory
  • DownGit
  • `; devider.insertAdjacentHTML("afterend", _html); } } else { let html = document.querySelector(".btn.mr-2.d-none.d-md-block"); if (html) { let _html = `
    Download folder `; html.insertAdjacentHTML("beforebegin", _html); } } } function removeAllInjectedElem() { document.querySelectorAll(".EricKwok").forEach(elem => elem.remove()); } function foldUnfold() { if (document.body.clientWidth <= 767 && !isFold) { console.log("收起" + document.body.clientWidth); isFold = true; } else if (document.body.clientWidth > 767 && isFold) { console.log("展开" + document.body.clientWidth); isFold = false; } reinject(); } function reinject() { if (document.querySelector(".file-navigation") && document.querySelector('[title="Go to parent directory"]')) { // 当不在仓库的根目录时注入按钮 removeAllInjectedElem(); injectDownloadFolderBtn(); } } (function () { 'use strict'; /** * 在浏览器窗口大小改变时自动重新定位设置菜单 */ window.onresize = function () { // 监听窗口大小改变 foldUnfold(); } document.addEventListener('pjax:success', function () { // 由于 GitHub 使用 pjax 而不是页面跳转的方式在仓库内导航,因此将 main 函数绑定到 pjax 监听器上 foldUnfold(); }); foldUnfold(); })();