etc / E-Hentai Flags on Gallery Thumbnails

// ==UserScript==
// @name           E-Hentai Flags on Gallery Thumbnails
// @description    Adds some fancy-ass flag icons above gallery thumbnails.
// @include        http://g.e-hentai.org/*
// @include        https://exhentai.org/*
// @run-at         document-end
// @version        0.1
// @locale         en
// @namespace      https://sleazyfork.org/en/users/2168-etc/flags
// ==/UserScript==

var LANGUAGES = [
	{ name: 'Japanese', regex: '' },
	{ name: 'English', regex: 'eng(?:lish)?' },
	{ name: 'Chinese', regex: 'chinese|中国翻訳' },
	{ name: 'Dutch', regex: 'dutch' },
	{ name: 'French', regex: 'french' },
	{ name: 'German', regex: 'german' },
	{ name: 'Hungarian', regex: 'hungarian' },
	{ name: 'Italian', regex: 'italian' },
	{ name: 'Korean', regex: [ 'korean', '[\\u1100-\\u11FF\\u3130-\\u318F\\uA960-\\uA97F\\uAC00-\\uD7AF\\uD7B0-\\uD7FF]' ] },
	{ name: 'Polish', regex: 'polish' },
	{ name: 'Portuguese', regex: 'portugu.se?(?:-br)?' },
	{ name: 'Russian', regex: 'russian' },
	{ name: 'Spanish', regex: 'spanish|espa.ol' },
	{ name: 'Thai', regex: 'thai[\\u0e00-\\u0e7f\\s]*' },
	{ name: 'Vietnamese', regex: 'vietnamese[a-z\\u00c0-\\u00ff\\u1ea0-\\u1eff\\s]*' },
];

var FLAG_ICONS = '' +
	'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApQAAAAeCAMAAAClpX6ZAAABXFBMVEXtVWX19/pXqGNKidzuVGXzusLtVWXtVWXtVWVHWKkAAAD5/P1u' +
	'fsJDSlTyrrell1ut07YnNFL/zlT/2FJOh4r21WD19/r19/rsQVbg4OX1nl9XqGPD0uxIqGSaptI4hN9KidzxhZEtQJ0/nEz75mFHWKlDSlTtVWX19/pXqGNHWKlKid' +
	'z/////zlRDSlT2///21mDtPU/tUGX3+/7rQmftSlv/4FL009n5oVotQJ0+gts/nEzr7fU/Uac3j+r5wMY2fNjzgY3sSWd8isPP3/RwoeOay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1+O6RfaZs5InETt+QSafv+/RCSQGgVXoXYc82G6vbTztQAtDucTHddh0on3z0eheaJ5spm20om04+U0bV51qmrEdHrOjMbf' +
	'2pw7/3dRKJ6Ntu3/ephZJCwi9Aa7W0kluZnk3qiTKZDAL49xIA7eNiG/CNVPmz2iQnmim9Vb1Ek+Sq18mKjvo8m+0rvPFNQl/6tL4xQNM9TnT6vk+gcfcfhVKARLgE' +
	'MJucXV//lkwa3l+iAHvhRNFPh4aSUcmahIhNk9Arbs3pjeeLEivgpEXvvJNQNrPVeiAbiI7YeG4BjzhA/X2dZODt7Aq+5lbQASjRN8YwjecVYS9MQThcYrVuw/ntbS' +
	'1RForsB/BQYtopLyHEoNRqTnJUXVyYdxJKEghIUakZIIS3m1G27GZEZBNUgOfi3JpzCsq3CA0BZaugD72A1uFSssPkOgAcVNgPpc5C6X7HLbfR9+2dX2QvOyjvL3Hr' +
	'vsV30xtDCfeAjDVjQIbzUOKfeZyC8sFLbj0YakBGgWpDL6B1aWlpm2npfRComSgdhRJzXAalTuWC135AxipCDgxdw7ej/WtQTjzh1sQwUELlWB96ARBcPTw8XF1PAK' +
	'VaoqwcAB0jKMd06NpAnTmL0H8o+wb5JkpaUgSARCJBAfR9/qK2b4ygxAzyHQsoMe1o/6Hsmw5RocclzZYCE9WrP/uJ8DHQcYISMx3i3gq37qGgHIdJRRgonZo4hocS' +
	'P3HMb/BnCsB/4Ieh4ouA0rEJXpgkhZo4Ng7TLzFQOjXFFg3l6FNsjaRJ6VDxxUyxHYdpsxhb1zhMVL/LD+VdpxYjuM0P5e0TXIzA5BQZX8xiBOOwwADG1jUOS3r4Jr' +
	'kTpc+pZVt8E9yJ0odZtsWp+GKWbRmHpVgwtq6xWPyILZzFpbs+5xa4YgtGcem2D7XAlUPxxSxwNRaLVmFsfwGgVgvFbUowvQAAAABJRU5ErkJggg==';

//----- Regex -----

var LANGUAGES_REGEX = null;

var getLanguageRegex = function() {
	if (LANGUAGES_REGEX) return LANGUAGES_REGEX;
	var temp = [ ];
	LANGUAGES.forEach(function(language) {
		if (language.regex.constructor == String)
			temp.push('[{[(]' + language.regex + '[)\\]}]');
		else {
			var regexes = [ ];
			regexes.push('[{[(]' + language.regex[0] + '[)\\]}]');
			language.regex.slice(1).forEach(function(value) { regexes.push(value); });
			temp.push(regexes.join('|'));
		}
	});
	LANGUAGES_REGEX = new RegExp('(' + temp.join(')|(') + ')', 'i');
	return LANGUAGES_REGEX;
};

var matchLanguage = function(title) {
    var matches = title.match(getLanguageRegex());
    if (!matches) return 0;
    for (var i=2;i<matches.length;++i) {
        if (matches[i])
            return (i - 1);
    }
    return 0;
};

//----- DOM -----

var walk = function() {
    [].slice.call(document.querySelectorAll('.id1'), 0).forEach(function(target) {
        if (target.hasAttribute('processed')) return;
		var title = target.querySelector('.id2 > a');
		if (!title) return;
        target.setAttribute('processed', true);
        var language = matchLanguage(title.textContent);
        var flag = document.createElement('div');
        flag.className = 'eh-flag';
        flag.style.backgroundPosition = (language * -44) + 'px 0';
        target.querySelector('.id3 > a').appendChild(flag);
    });
};

//----- Init -----

var style = document.createElement('style');
style.innerHTML = '.eh-flag { background: url(%FLAGS); position: absolute; top: 0; right: 0; width: 44px; height: 30px; }';
style.innerHTML = style.innerHTML.replace(/%FLAGS/, FLAG_ICONS.replace(/\s/g,''));
document.head.appendChild(style);

var debouncer = null;
document.body.addEventListener('DOMNodeInserted', function(e) {
    if (debouncer !== null) clearTimeout(debouncer);
    debouncer = setTimeout(walk, 100);
});

walk();