// ==UserScript==
// @name Download github repo sub-folder
// @version 0.2.2
// @author Saiya
// @description download github directory via one click, copy the single file's source code easily
// @supportURL https://github.com/oe/download-git-userscript/issues
// @match https://github.com/*
// @match https://gist.github.com/*
// @namespace https://app.evecalm.com
// @homepageURL https://github.com/oe/download-git-userscript
// @connect raw.githubusercontent.com
// @grant GM_download
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// @noframes
// ==/UserScript==
!function(e){var t={};function n(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([function(e,t,n){"use strict";function o(){return document.getElementById("raw-url")}Object.defineProperty(t,"__esModule",{value:!0}),t.openLink=t.getCurrentUrlPath=t.getRawBtn=t.getUrlTextResponse=t.isTextBasedSinglePage=t.isRepoRootDir=t.isPrivateRepo=t.isRepo=t.isGist=void 0,t.isGist=function(){return"gist.github.com"===location.hostname},t.isRepo=function(){return document.querySelector(".repository-content")},t.isPrivateRepo=function(){const e=document.querySelector("#js-repo-pjax-container .hide-full-screen .Label");return e&&"Private"===e.textContent},t.isRepoRootDir=function(){return!!document.querySelector(".repository-content get-repo")},t.isTextBasedSinglePage=function(){if(!o())return;return!!document.getElementById("readme")||!!document.querySelector("table.highlight")},t.getUrlTextResponse=function(e){return new Promise((t,n)=>{GM_xmlhttpRequest({method:"GET",url:e,ontimeout:n,onabort:n,onerror:n,onload:e=>{e.responseText?t(e.responseText):n(e)}})})},t.getRawBtn=o,t.getCurrentUrlPath=function(){return(location.origin+location.pathname).replace(/\/$/,"")},t.openLink=function(e){const t=document.createElement("a");t.target="_blank",t.href=e,t.click()}},function(e,t,n){"use strict";var o=this&&this.__createBinding||(Object.create?function(e,t,n,o){void 0===o&&(o=n),Object.defineProperty(e,o,{enumerable:!0,get:function(){return t[n]}})}:function(e,t,n,o){void 0===o&&(o=n),e[o]=t[n]}),r=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&o(t,e,n);return r(t,e),t};Object.defineProperty(t,"__esModule",{value:!0});const u=i(n(0)),c=n(0);!function(){function e(){u.isRepo()&&(function(){let e=document.querySelector(".repository-content .file-navigation");if(e){const n=t(e);return n.className+=" ml-2",void e.appendChild(n)}const n=document.querySelector("#blob-more-options-details");if(!n)return;e=n.parentElement;const o=t(e);o.className+=" mr-2",n.insertAdjacentElement("beforebegin",o)}(),function(){if(!u.isTextBasedSinglePage()||document.getElementById("xiu-copy-btn"))return;const e=c.getRawBtn(),t=document.createElement("a");t.setAttribute("role","button"),t.className="btn btn-sm BtnGroup-item",t.href="#",t.id="xiu-copy-btn",t.textContent="Copy",t.onclick=async t=>{t.preventDefault();try{const t=await u.getUrlTextResponse(e.href);GM_setClipboard(t)}catch(e){console.warn(e)}},e.insertAdjacentElement("beforebegin",t)}())}function t(e){let t=document.getElementById("xiu-download-btn");t||(t=document.createElement("a"),t.id="xiu-download-btn"),t.className="btn d-none d-md-block",t.target="_blank";let n="";if(u.isRepoRootDir()){n=e.querySelector('get-repo a[href$=".zip"]').href}else if(u.getRawBtn()){const e=u.getRawBtn();n=e.href,t.onclick=function(t){t.preventDefault();const n=e.href.split("/").pop();GM_download({url:e.href,name:n,onerror:e=>{confirm(e.error+`: you may need add extension ".${n.split(".").pop()}" to Tampermonkey's whitelist. Click OK to see how.`)&&u.openLink("https://github.com/oe/download-git-userscript#configure-the-script-manager")}})}}else n="https://minhaskamal.github.io/DownGit/#/home?url="+encodeURIComponent(u.getCurrentUrlPath());return t.textContent="Download",t.href=n,t}e(),function(){let t;t=u.isGist()?document.querySelector("#gist-pjax-container"):document.querySelector("#js-repo-pjax-container");if(!t)return;new MutationObserver((function(t){console.log("mutation change",t),e()})).observe(t,{childList:!0,subtree:!1})}()}()}]);