Gist Downloader Plus

Directly download GitHub gists as source files.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Gist Downloader Plus
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Directly download GitHub gists as source files.
// @author       Ahmed Mohamed Abdelaty
// @match        https://gist.github.com/*/*
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @grant        GM_download
// @license      MIT; https://opensource.org/licenses/MIT
// ==/UserScript==

;(function () {
  'use strict'
  // get the name of the gist
  const gistName = document.querySelector('.gist-blob-name').innerText

  // get the raw url of the gist
  const rawUrl = document.querySelector('.file-actions')
  // get the first a element
  const a = rawUrl.querySelector('a')
  // get the href attribute
  const href = a.getAttribute('href')

  // create button next to the raw button
  const button = document.createElement('button')

  // copy the properties of the raw button to the new button
  const rawButton = document.querySelector('.file-actions a')
  const rawButtonStyles = getComputedStyle(a)
  button.textContent = rawButton.textContent
  button.style.cssText = rawButtonStyles.cssText
  button.style.marginLeft = '5px'
  button.style.padding = '5px'
  button.style.backgroundColor = 'green'
  button.innerText = 'Download'
  button.style.borderRadius = '10px'

  // add the button to the rawUrl div
  rawUrl.appendChild(button)

  button.addEventListener('click', () => {
    // get the raw content of the gist
    GM_xmlhttpRequest({
      method: 'GET',
      url: href,
      onload: function (response) {
        const blob = new Blob([response.responseText], {
          type: 'text/plain',
        })
        const url = URL.createObjectURL(blob)
        const a = document.createElement('a')
        a.href = url
        a.download = gistName
        a.click()
      },
    })
  })
})()