SSS automatic download

Download videos automatically from SSS websites like ssstik.io and ssstwitter.com

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name SSS automatic download
// @namespace https://github.com/anytngv2/sss-automatic-download
// @supportURL https://github.com/anytngv2/sss-automatic-download
// @version 1.1
// @description Download videos automatically from SSS websites like ssstik.io and ssstwitter.com
// @author AnytngV2
// @match https://ssstik.io/*
// @match https://ssstwitter.com/*
// @match https://ssscdn.io/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=ssstik.io
// @license MIT
// @compatible chrome
// @compatible edge
// @compatible firefox
// @compatible waterfox
// @compatible librewolf
// @compatible safari
// @compatible brave
// @grant none
// ==/UserScript==

(function() {
	'use strict';

	const CONFIG = [
		"ssstik",
		"ssstwitter"
	];

	// check if domain is in the config and do custom action
	const domain = window.location.hostname;
	console.log(`[ANTG2] Current domain: ${domain}`);

	// check if domain contains value in config
	if (CONFIG.some(configDomain => domain.includes(configDomain))) {
		console.log(`[ANTG2] Automatic download script is running on ${domain}`);
		// check if domain is ssstik
		if (domain.includes("ssstik")) {
			toast("Automatic download script can be running on this page.", null, 3000);
			ssstik();
		} else if (domain.includes("ssstwitter")) {
			toast("Automatic download script can be running on this page.", null, 3000);
			ssstwitter();
		} else {
			console.error('[ANTG2] Input element not found');
		}
	}

	function ssstwitter() {
		const inputElement = document.querySelector('#main_page_text');
		if (inputElement) {
			inputElement.addEventListener('input', function() {
				console.log('[ANTG2] Input changed:', this.value);

				// if the value is a valid url, click on #submit
				if (this.value && this.value.startsWith('http')) {
					const submitButton = document.querySelector('#submit');
					if (submitButton) {
						submitButton.click();
						console.log('[ANTG2] Submit button clicked');

						const observer = new MutationObserver(function(mutations) {
							mutations.forEach(function(mutation) {
								const downloadButton = document.querySelector('.download_link');
								if (downloadButton) {
									downloadButton.click();
									console.log('[ANTG2] Download button clicked : ' + downloadButton.dataset.directurl);

									// go to url data-directurl
									if(downloadButton.dataset.directurl) {
										window.location.href = downloadButton.dataset.directurl;
										toast('The video is being downloaded, please check your download folder.', "/", 3500);
									} else{
										toast('Err:1, download url not found, please try again.', null, 5000);
									}

									// toast('The video is being downloaded, please check your download folder.', "/", 3500);
									observer.disconnect();
								}
							});
						});
						observer.observe(document.body, { childList: true, subtree: true });
					}
				}
			});
		}
	}

	function ssstik() {
		// add event listener for #main_page_text.form-control.input-lg
		const inputElement = document.querySelector('#main_page_text.form-control.input-lg');
		if (inputElement) {
			inputElement.addEventListener('input', function() {
				console.log('[ANTG2] Input changed:', this.value);

				// if the value is a valid url, click on #submit
				if (this.value && this.value.startsWith('http')) {
					const submitButton = document.querySelector('#submit');
					if (submitButton) {
						submitButton.click();
						console.log('[ANTG2] Submit button clicked');

						// now we wait for the button .dl-button.download_link.without_watermark to appear and click it
						const observer = new MutationObserver(function(mutations) {
							mutations.forEach(function(mutation) {
								const downloadButton = document.querySelector('.dl-button.download_link.without_watermark');
								if (downloadButton) {
									downloadButton.click();
									console.log('[ANTG2] Download button clicked');
									toast('The video is being downloaded, please check your download folder.', "/", 3500);
									observer.disconnect();
								}
							});
						});
						observer.observe(document.body, { childList: true, subtree: true });
					}
				}
			});
		}
	}

	/**
		* Creates a toast notification with the given message and options.
		* @param {string} message - The message to be displayed in the toast.
		* @param {string} [returnAfterToast=null] - The URL to redirect to after the toast has been displayed for the given duration.
		* @param {number} [duration=5000] - The duration to display the toast in milliseconds.
		*/
	function toast(message, returnAfterToast = null, duration = 5000) {
		const toast = document.createElement('div');
		toast.style.cssText = `
			position: fixed;
			bottom: 0;
			right: 0;
			background-color: #1a1a1a;
			color: #fff;
			padding: 15px; 
			border-radius: 5px;
			font-size: 20px;
			margin: 20px;
			z-index: 1000;
		`;
		toast.textContent = message;
		document.body.appendChild(toast);
		setTimeout(() => {
			document.body.removeChild(toast);
		}, duration);
		if (returnAfterToast) {
			setTimeout(() => {
				window.location.href = returnAfterToast;
			}, duration);
		}
	}
})();