SankakuDLNamer

Help with DL naming

Versione datata 09/03/2021. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        SankakuDLNamer
// @namespace   SankakuDLNamer
// @description Help with DL naming
// @author      SlimeySlither
// @match       http*://chan.sankakucomplex.com/*
// @match       http*://idol.sankakucomplex.com/*
// @match       http*://beta.sankakucomplex.com/*
// @run-at      document-end
// @version     1.0.1.4
// @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);
		fileName = fileName + fileName.hashCode();

		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(' ') + ') '}drawn 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;
	}

	// https://stackoverflow.com/a/7616484
	String.prototype.hashCode = function () {
		var hash = 0, i, chr;
		if(this.length === 0) return hash;
		for(i = 0; i < this.length; i++) {
			chr = this.charCodeAt(i);
			hash = ((hash << 5) - hash) + chr;
			hash |= 0; // Convert to 32bit integer
		}
		return hash;
	};

})();