Video / Gif downloader from the new design of redgifs.com (v3)

Gather all information about video / gif and create a download button in the sidebar of videos / gif

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// 
// @name Video / Gif downloader from the new design of redgifs.com (v3)
// @name:en Video / Gif downloader from the new design of redgifs.com (v3)
// @name:ru Загрузка видео / гиф с нового дизайна redgifs.com(v3)
// @name:de Lädt Videos / Gifs von dem neuen Design der Seite redgifs.com(v3)
// 
// @description Gather all information about video / gif and create a download button in the sidebar of videos / gif
// @description:en Gather all information about video / gif and create a download button in the sidebar of videos / gif
// @description:ru Собирает информацию о видео / гифке и предоставляет в боковом меню видео / гифки ссылку для загрузки
// @description:de Sammelt die nötigen Informationen über das Video / Gif in dem neuen redgifs.com (v3) Design
// 
// @match https://*.redgifs.com
// @match https://*.redgifs.com/*
// 
// @namespace RedGifsDownloader
// @author Maxim Harder (2023 @ DevCraft.club)
// @license MIT
// @version 1.0.0
// @run-at document-start
// ==/UserScript==
function getVideoDownloadMod() {
	let videos_tall = document.querySelectorAll('[class="active player preview tall"] video');
	let videos_wide = document.querySelectorAll('[class="active player preview wide"] video');
	let videos = [];

	for (let i = 0, max = videos_tall.length; i < max; i++) {
		videos.push(videos_tall[i]);
	}

	for (let i = 0, max = videos_wide.length; i < max; i++) {
		videos.push(videos_wide[i]);
	}

	if (videos.length > 0) {
		let video_src = videos[0].src;
		let sidebar_elements = document.querySelectorAll('[class="active player preview tall"] div[class="sideBar"]>*');
		if (sidebar_elements.length == 0) sidebar_elements = document.querySelectorAll('[class="active player preview wide"] div[class="sideBar"]>*');
		let last_sb_el = sidebar_elements[sidebar_elements.length - 1];
		let sidebar = document.querySelectorAll('[class="active player preview tall"] div[class="sideBar"]')[0];
		if (sidebar == undefined) sidebar = document.querySelectorAll('[class="active player preview wide"] div[class="sideBar"]')[0];

		let active_download_tall = document.querySelectorAll('[class="active player preview tall"] div[class="sideBar"] .download-mod');
		let active_download_wide = document.querySelectorAll('[class="active player preview wide"] div[class="sideBar"] .download-mod');

		let video_div = document.querySelectorAll('[class="active player preview tall"]')[0];
		if (video_div == undefined) video_div = document.querySelectorAll('[class="active player preview wide"]')[0];
		let video_id = video_div.id;

		let storage_id = localStorage.getItem('download-mod-id');

		let download_btn = `<div class="download-mod"><img src="https://www.svgrepo.com/download/489722/download.svg" style="width: 100%;height: 100%; color: white;filter: invert(100%) sepia(100%) saturate(0%) hue-rotate(86deg) brightness(116%) contrast(83%); cursor:pointer;" onclick="window.open('${video_src}')"></div>`;

		if (video_id != storage_id) {
			for (let i = 0, max = active_download_tall.length; i < max; i++) {
				let el = active_download_tall[i];
				el.parent.removeChild(el);
			}
			for (let i = 0, max = active_download_wide.length; i < max; i++) {
				let el = active_download_wide[i];
				el.parent.removeChild(el);
			}

			last_sb_el.insertAdjacentHTML('afterend', download_btn);

			localStorage.setItem('download-mod-id', video_id);

		}
	}
}


const download_mod_video = (path, filename) => {
	// Create a new link
	const anchor = document.createElement('a');
	anchor.href = path;
	anchor.download = filename;

	// Append to the DOM
	document.body.appendChild(anchor);

	// Trigger `click` event
	anchor.click();

	// Remove element from DOM
	document.body.removeChild(anchor);
};


setInterval(() => {
	getVideoDownloadMod();
}, 1000);