GreasyFork: download script button

If you have a script manager and you want to download some script without installing it, this script will help

Ekde 2021/01/29. Vidu La ĝisdata versio.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         GreasyFork: download script button
// @description  If you have a script manager and you want to download some script without installing it, this script will help
// @namespace    https://greatest.deepsurf.us/users/424058
// @version      1.0.0
// @author       Konf
// @match        https://greatest.deepsurf.us
// @include      /^http(s|):\/\/greatest.deepsurf.us\/[a-zA-Z_-]*\/scripts\/\d.*$/
// @run-at       document-end
// @resource     iconDownload https://img.icons8.com/pastel-glyph/64/ffffff/download.png
// @compatible   Chrome
// @compatible   Opera
// @compatible   Firefox
// @grant        GM_getResourceUrl
// @grant        GM.getResourceUrl
// @noframes
// ==/UserScript==

/* jshint esversion: 6 */
/* eslint-disable no-multi-spaces */

(function() {
  'use strict';

  const userLang = location.pathname.split('/')[1];
  const langStringsList = {
    en: {
      Dl: 'Download without installing',
      unableDl: 'Unable to download the script'
    },
    ru: {
      Dl: 'Скачать не устанавливая',
      unableDl: 'Не удалось скачать скрипт'
    }
  };
  const langStrings = langStringsList[userLang] || langStringsList.en;

  (GM.getResourceUrl || GM_getResourceUrl)('iconDownload')
  .then(downloadIconUrl => {
    addCSS(`
      .tm-dsb-downloadBtn {
        width: 43px;
        height: 38px;
        position: relative;
        padding: 0;
        vertical-align: bottom;
        cursor: pointer;
        border: none;
        outline: none;
        background: #0F750F;
        transition: box-shadow 0.2s;
      }

      .tm-dsb-downloadBtn:hover,
      .tm-dsb-downloadBtn:focus {
        box-shadow: 0 8px 16px 0 rgb(0 0 0 / 20%), 0 6px 20px 0 rgb(0 0 0 / 19%);
      }


      .tm-dsb-downloadBtn__icon {
        position: absolute;
      }

      .tm-dsb-downloadBtn__icon--download {
        width: 35px;
        height: 35px;
        top: 1px;
        left: 4px;
        background: url(${downloadIconUrl});
        background-size: contain;
      }

      .tm-dsb-downloadBtn__icon--loading,
      .tm-dsb-downloadBtn__icon--loading:after {
        border-radius: 50%;
        width: 16px;
        height: 16px;
      }

      .tm-dsb-downloadBtn__icon--loading {
        top: 6px;
        left: 8px;
        text-indent: -9999em;
        border-top: 5px solid rgba(255, 255, 255, 0.2);
        border-right: 5px solid rgba(255, 255, 255, 0.2);
        border-bottom: 5px solid rgba(255, 255, 255, 0.2);
        border-left: 5px solid #ffffff;
        -webkit-transform: translateZ(0);
        -ms-transform: translateZ(0);
        transform: translateZ(0);
        -webkit-animation: loading 1.1s infinite linear;
        animation: loading 1.1s infinite linear;
      }

      @-webkit-keyframes loading {
        0% {
          -webkit-transform: rotate(0deg);
          transform: rotate(0deg);
        }
        100% {
          -webkit-transform: rotate(360deg);
          transform: rotate(360deg);
        }
      }

      @keyframes loading {
        0% {
          -webkit-transform: rotate(0deg);
          transform: rotate(0deg);
        }
        100% {
          -webkit-transform: rotate(360deg);
          transform: rotate(360deg);
        }
      }
    `);
  });

  const b = document.createElement('button'); // downloadBtn
  const bIcon = document.createElement('div');
  const bParent = document.body.querySelector('div#install-area');
  const bNeighbour = document.body.querySelector('a.install-help-link');
  const installBtn = document.body.querySelector('a.install-link');

  b.title = langStrings.Dl;
  b.className = 'tm-dsb-downloadBtn';
  bIcon.className = 'tm-dsb-downloadBtn__icon tm-dsb-downloadBtn__icon--download';
  bParent.insertBefore(b, bNeighbour);
  b.appendChild(bIcon);

  let fetchingDelay = false;

  b.addEventListener('click', () => {
    setTimeout(() => {
      if (b === document.activeElement) document.activeElement.blur();
    }, 750);

    if (fetchingDelay) return;
    fetchingDelay = true;
    bIcon.className = 'tm-dsb-downloadBtn__icon tm-dsb-downloadBtn__icon--loading';

    fetch(installBtn.href)
      .then(res => res.blob())
      .then(blob => {
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');

        a.href = url;
        a.download = `${installBtn.dataset.scriptName}.user.js`;
        document.body.appendChild(a); // needed due to firefox bug
        a.click();
        a.remove();

        setTimeout(closeFetchUX, 300);
        window.URL.revokeObjectURL(url);
      })
      .catch(e => {
        setTimeout(closeFetchUX, 300);
        alert(`${langStrings.unableDl}: \n${e}`);
     });
  });


  // utils -------------------------------------------------------------

  function closeFetchUX() {
    fetchingDelay = false;
    bIcon.className = 'tm-dsb-downloadBtn__icon tm-dsb-downloadBtn__icon--download';
  }

  function addCSS(cssCode) {
    const styleEl = document.createElement("style");
    styleEl.type = "text/css";

    if (styleEl.styleSheet) {
      styleEl.styleSheet.cssText = cssCode;
    } else {
      styleEl.appendChild(document.createTextNode(cssCode));
    }

    document.getElementsByTagName("head")[0].appendChild(styleEl);

    return styleEl;
  }

  // --------------------------------------------------------------------

})();