SankakuDLNamer

Help with DL naming

Από την 04/03/2021. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        SankakuDLNamer
// @namespace   SankakuDLNamer
// @description Help with DL naming
// @match       http*://chan.sankakucomplex.com/*
// @match       http*://idol.sankakucomplex.com/*
// @match       http*://beta.sankakucomplex.com/*
// @run-at      document-end
// @version     1.0.0.1
// @grant       GM_download
// ==/UserScript==
(function() {
	'use strict';
	document.addEventListener("DOMContentLoaded", function() {
		const tagsidebar = document.getElementById('tag-sidebar');
		if (tagsidebar == null) { return; }

		let cats = GetSidebarTags(tagsidebar);

		let fileName = GenerateFilename(cats);

		let details = SetUpDLDetails(fileName);

		var dl_me = function() {
			GM_download(details)
		}

		const a = document.createElement('a');
		a.href = '#';
		a.innerText = 'DL_Me';
		a.onclick = dl_me;

		tagsidebar.insertBefore(a, tagsidebar.childNodes[0]);
	});

	function GetSidebarTags(tagsidebar) {
		const tagGroups = tagsidebar.getElementsByTagName('li');

		let cats = {};
		for (let tagGroup of tagGroups) {
			const cat = tagGroup.className.trim();
			const anchs = tagGroup.getElementsByTagName('a');
			let tag = '';
			for (let anch of anchs) {
				if (anch.hasAttribute('id')) {
					tag = anch.innerText.replace(/\s/g, '_').trim();
				}
			}
			if (cat in cats) {
				if (tag != '') {
					cats.cat = cats[cat].push(tag);
				}
			} else {
				if (tag != '') {
					cats[cat] = [];
					cats[cat].push(tag);
				}
			}
		}

		return cats;
	}

	function GenerateFilename(cats) {
		return `${cats['tag-type-character'] == null ? '' :
		cats['tag-type-character'].join('_')}_${cats['tag-type-copyright'] == null ? '' :
		cats['tag-type-copyright'].join('_')}by_${cats['tag-type-artist'] == null ? '' :
		cats['tag-type-artist'].join('_')}`;
	}

	function SetUpDLDetails(fileName) {
		const stat = document.getElementById('stats');
		let url = 'https:' + stat.querySelectorAll('#highres')[0].getAttribute("href").trim();
		let extension = url.split('?')[0].split('.');
		extension = extension[extension.length - 1];
		let details = {
			'url': url
			, 'name': fileName + '.' + extension
			, 'saveAs': true
		}

		return details;
	}
})();