Sauvegarde / SubscribeStar helper

// ==UserScript==
// @name         SubscribeStar helper
// @namespace    https://openuserjs.org/users/Sauvegarde
// @version      0.2
// @description  Adds a direct download link to picture slideshow for convenience
// @author       Sauvegarde
// @match        https://subscribestar.adult/*
// @icon         https://i.imgur.com/FpnxaH5.png
// @grant        GM_download
// @updateURL    https://openuserjs.org/meta/Sauvegarde/SubscribeStar_helper.meta.js
// @downloadURL  https://openuserjs.org/install/Sauvegarde/SubscribeStar_helper.user.js
// @copyright    2020, Sauvegarde (https://openuserjs.org/users/Sauvegarde)
// @license		 MIT
// ==/UserScript==

/* jshint esversion: 6 */

(function() {
    'use strict';

	const DDL_BTN_ID = "custom-ddl-button";

	function doTheThing() {
		const imgLinks = document.querySelectorAll("a.gallery-image_original_link");
		if(imgLinks.length === 1) {
			const imgLink = imgLinks[0];
			const existing = document.getElementById(DDL_BTN_ID);
			if(!existing && GM_download) {
				const txt = document.createElement("span");
				txt.innerHTML = "Direct download";
				const emo = document.createElement("span");
				const btn = document.createElement("a");
				btn.appendChild(txt);
				btn.appendChild(emo);
				btn.id = DDL_BTN_ID;
				btn.className = imgLink.className;
				btn.style.display = "inline-block";
				btn.style.cursor = "pointer";
				btn.addEventListener("click", () => {
					console.log(imgLink.href, imgLink.href.split("/").pop());
					GM_download({
						url: imgLink.href,
						name: imgLink.href.split("/").pop().split("?")[0],
						saveAs: false,
						ontimeout: () => { emo.innerHTML = " 🚩"; handleTimeout(); },
						onerror: error => { emo.innerHTML = " 🚩"; handleError(error); },
						onload: () => { emo.innerHTML = " 👍"; },
					});
					emo.innerHTML = " ⌛";
				});
				imgLink.style.display = "inline-block";
				const sep = document.createElement("span");
				sep.innerHTML = " | ";
				sep.style.display = "inline-block";
				imgLink.parentElement.appendChild(sep);
				imgLink.parentElement.appendChild(btn);
			}
		}
	}

	function handleTimeout() {
		alert("The download target has timed out :(");
	}

	function handleError(error) {
		switch(error.error) {
			case "not_enabled":
				alert("GM_download is not enabled.");
				break;
			case "not_permitted":
				alert("GM_download permission has not been granted.");
				break;
			case "not_supported":
				alert("GM_download is not supported by the browser/version.");
				break;
			case "not_succeeded":
				console.error(error);
				alert("GM_download has vulgarly failed. Please retry.");
				break;
			case "not_whitelisted":
				// https://github.com/Tampermonkey/tampermonkey/issues/643
				alert("The requested file extension is not whitelisted.\n\n"
					  +"You have to add it manually (see 'Downloads' in Tampermonkey settings).");
				break;
			case "Download canceled by the user":
				// User just clicked "Cancel" on the prompt
				break;
			default:
				console.error(error);
				alert("GM_download has unexpectedly failed with the following error: " + error.error);
				break;
		}
	}

	document.body.addEventListener('click', () => setTimeout(doTheThing, 500), true);
})();