Greasy Fork is available in English.

MyAnimeList (MAL) Tags Cloud

Displays tags cloud in anime and manga lists.

Version au 26/06/2015. Voir la dernière version.

// ==UserScript==
// @name		MyAnimeList (MAL) Tags Cloud
// @namespace	https://greasyfork.org/users/7517
// @description	Displays tags cloud in anime and manga lists.
// @icon		http://i.imgur.com/b7Fw8oH.png
// @version		1.5.1
// @author		akarin
// @include		/^http:\/\/myanimelist\.net\/(anime|manga)list/
// @grant		none
// @noframes
// ==/UserScript==

(function($) {

var mal = {
	fontMin: 9,
	fontMax: 70
};

function main() {
	mal.cloud = $('<div id="tags_cloud"></div>');
	
	mal.header = $('<div class="header_title"><span>Tags Cloud</span><sup></sup> </div>')
		.append($('<small></small>').append(
			$('<a href="javascript:void(0);">refresh</a>').click(function() { 
				remap();
			})		
		))
		.appendTo(mal.cloud);

	mal.content = $('<div id="tags_list"></div>')
		.append('<h2 id="tags_not_found">No tags found.</h2>')
		.appendTo(mal.cloud);
	
	$('<div style="display: none;"></div>').append(mal.cloud).appendTo('body');

	$('<span id="tc_links"></span>')
		.append('<span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>')
		.append($('<a href="#tags_cloud">Tags Cloud</a>').fancybox({			
			'autoScale': false,
			'autoDimensions': false,
			'width': '80%',
			'height': '80%',
			'hideOnContentClick': false,
			'hideOnOverlayClick': true,
			'transitionIn': 'none', 
			'transitionOut': 'none',
			'centerOnScroll': true,
			'titleShow': false,
			'onStart': function() {
				if ($('h2#tags_not_found', mal.content).length > 0) {
					remap();
				}
			}
		}))
		.appendTo('#mal_cs_otherlinks div:last');
}

function remap() {
	var tags = {};
	mal.content.empty();

	$('#list_surround span[id^="tagLinks"] > a').each(function() {
		var tag = $(this).text().trim();
		if (tag.length > 0) {
			tags[tag] = tags.hasOwnProperty(tag) ? tags[tag] + 1 : 1;
		} else {
			console.log('empty tag: ' + $(this).parent().prop('id'));
		}
	});
	
	var keys = Object.keys(tags).sort(function(a, b) {
			return a.toLowerCase().localeCompare(b.toLowerCase());
		}),
		len = keys.length;
		
	if (len === 0) {
		mal.content.append('<h2 id="tags_not_found">No tags found.</h2>');
		return;
	}
	
	$('sup', mal.header).text(len > 1 ? ' ' + len : '');
	
	var max = 0,
		min = Number.MAX_VALUE;
	
	$.each(tags, function(tag, freq) {
		if (freq > max) {
			max = freq;
		}
		if (freq < min) {
			min = freq;
		}
	});
	
	var sizeDif = max - min,
		fontDif = mal.fontMax - mal.fontMin,
		tagsLink = document.URL.replace(/^http:\/\/myanimelist\.net/, '').replace(/&tag=(.*)&/, '&').replace(/&tag=(.*)$/, '') + '&tag=';
	
	$.each(keys, function(i, tag) {
		var freq = tags[tag],
			fontSize = mal.fontMin + (fontDif * ((freq - min) / sizeDif));
		
		var span = $('<span class="cloud_tag"></span>')
			.css({
				'font-size': Math.round(fontSize) + 'px',
				'margin': '0 ' + Math.round(fontSize / 4) + 'px'
			})
			.append('<a href="' + tagsLink + tag + '">' + tag + '</a>')
			.appendTo(mal.content);
			
		if (freq > 1) {
			span.append('<sup> ' + freq + '</sup>');
		}
		
		if (i + 1 < len) {
			mal.content.append(', ');
		}
	});
}

$('<style type="text/css"></style>').html(
	'div#tags_cloud { text-align: center; padding: 5px; }' +
	'div#tags_cloud sup { color: #90a0b0; font-weight: lighter; }' +
	'div#tags_cloud .header_title { text-align: center; }' +
	'div#tags_cloud .header_title span { padding: 0; }' +
	'div#tags_cloud .header_title small { font-size: ' + (mal.fontMin + 1) + 'px; font-weight: normal; text-transform: lowercase; }' +
	'div#tags_cloud .header_title:after { content: ""; display: block; position: relative; width: 100%; height: 8px; margin: 0.5em 0 0;	padding: 0;	border-top: 1px solid #ebebeb; background: center bottom no-repeat radial-gradient(#f6f6f6, #fff 70%); background-size: 100% 16px; }' +
	'div#tags_list { min-width: 200px; min-height: 150px; color: #666; font-size:' + mal.fontMin + 'px; }' +
	'div#tags_list .cloud_tag { white-space: nowrap; }' +
	'div#tags_list h2#tags_not_found { font-weight: normal; }'
).appendTo('head');

main();

})(jQuery);