GBT gallery downloader (GBTGD)

Creates a button to download all images from gallery in a single .zip file.

Per 03-12-2024. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name         GBT gallery downloader (GBTGD)
// @namespace    _pc
// @version      5.0
// @license      MIT
// @description  Creates a button to download all images from gallery in a single .zip file.
// @author       verydelight
// @match        *://*.gayboystube.com/galleries/*
// @icon         https://www.gayboystube.com/favicon.ico
// @run-at       document-end
// @grant        GM.xmlHttpRequest
// @grant        GM_download
// @grant        GM_xmlHttpRequest
// @grant        GM.download
// @require      https://update.greasyfork.org/scripts/473358/1237031/JSZip.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/FileSaver.min.js
// ==/UserScript==
'use strict';
window.onload = function(){
	const zip = new JSZip();
	const button = document.createElement("button");
	const downloadStatus = document.createElement("div");
	const zipFileName = document.querySelector('h1.title').innerText
	.replace(/[^a-zA-Z0-9-_ ]/g, '')
	.replace(/\s+/g, '_')
	.substring(0, 35)
	.replace(/^_+|_+$/g, '');
	const finalZipFileName = `GBT_${zipFileName}`;
	button.textContent = "Download Gallery";
	button.type = "button";
	button.addEventListener ("click", fullSize, false);
	document.getElementById('tab_video_info').append(button);
	async function downloadFile(url, filename,iCurr,iMax) {
		try {
			const response = await GM.xmlHttpRequest({
				method: 'GET',
				responseType: 'blob',
				url: url,
				headers: {
					"Content-Type": "image/jpeg", "Accept": "image/jpeg"
				},
			});
			const blob = new Blob([response.response],{type: 'image/jpeg'});
			zip.file(filename, blob, {binary: true})
		} catch (err) {
			console.error("GBTGD: Error in fetching and downloading file: ",iCurr," " , err);
		}
		if(iCurr==iMax-1){
			downloadStatus.innerHTML = "Now zipping images, creating zip-file and sending for download. Please wait...";
            const content = await zip.generateAsync({ type: "blob", compression: "DEFLATE", compressionOptions: { level: 6 } });
            saveAs(content, finalZipFileName);
			downloadStatus.innerHTML = "Download complete!";
		}
	}
	async function fullSize() {
		button.style.visibility = 'hidden';
		document.getElementById('tab_video_info').append(downloadStatus);
		const images = document.querySelectorAll('#album_view_album_view img');
		const iMax = images.length;
		let downloadTime = 0;
		let downloadEstimate = 0;
		const pattern1 = /\/thumbs/;
		const pattern2 = /\/main\/\d{1,4}x\d{1,4}/;
		for (let i = 0; i < iMax; i++) {
			const timeRemaining = downloadEstimate > 0 ? formatTime(downloadEstimate) : '';
			downloadStatus.innerHTML = `Downloading image ${i+1}/${iMax}${timeRemaining ? ` (ca.: ${timeRemaining} remaining)` : ''}`;
			let currentSrc = images[i].getAttribute('data-src').replace(pattern1, '').replace(pattern2, '/sources');
			const fileName = currentSrc.split('/').pop();
			const downloadStart = Date.now();
			await downloadFile(currentSrc, fileName, i, iMax);
			const downloadEnd = Date.now();
			downloadTime += downloadEnd - downloadStart;
			downloadEstimate = Math.round((((downloadTime / (i + 1)) * iMax) - downloadTime) / 1000);
		}
	}
	function formatTime(seconds) {
		const minutes = Math.floor(seconds / 60);
		const secs = seconds % 60;
		const formattedMinutes = minutes > 0 ? (minutes < 10 ? "0" + minutes : minutes) : "00";
		const formattedSeconds = secs < 10 ? "0" + secs : secs;
		return minutes > 0 ? `${formattedMinutes}:${formattedSeconds} minutes` : `${formattedSeconds} seconds`;
	}
};