vplameniraket / Interpals — News icons and flags

// ==UserScript==
// @name         Interpals — News icons and flags
// @namespace    http://tampermonkey.net/
// @version      1.8
// @description  New and shiny :) Should be installed together with the userstyle: userstyles.org/styles/142170/interpals-complete-redesign
// @author       vplameniraket
// @match        https://www.interpals.net/*
// @grant        GM_addStyle
// @run-at       document-start
// @license      MIT
// @copyright    2019, vplameniraket (https://openuserjs.org/users/vplameniraket)
// @updateURL    https://openuserjs.org/meta/vplameniraket/Interpals_—_News_icons_and_flags.meta.js
// ==/UserScript==


GM_addStyle('.fa, .far, .fas { font-family: "Font Awesome 5 Free", Helvetica Neue, Helvetica, sans-serif, Arial !important; } .fa, .fas { font-weight: inherit !important; } .fa::before, .fas::before { font-weight: 900; } #prof-action-links a:not(:last-child) { margin-right: 6px; } #langexForm ~ div a { min-width: calc(33.3% - 12px) !important; }');
//GM_addStyle('.delSelItem { font-family: "Font Awesome 5 Free", Helvetica Neue, Helvetica, sans-serif, Arial !important; font-weight: 600; width: 19px; height: 19px; overflow: hidden; display: inline-block; transform: translateY(4px); } .delSelItem::before { content: "\\f056"; font-size: 19px; } #sCFields .selElem { width: 86% !important; }');
(function() {
	'use strict';
	function injectNewFontAwesome() {
		var link = document.querySelectorAll('link[href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css"]')[0];
		if (link !== undefined) {
			link.setAttribute('href','https://use.fontawesome.com/releases/v5.8.2/css/all.css');
			link.setAttribute('integrity','sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay');
			link.setAttribute('crossorigin','anonymous');
		}
		document.getElementsByTagName('head')[0].insertAdjacentHTML('beforeend', '<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/brands.css" integrity="sha384-i2PyM6FMpVnxjRPi0KW/xIS7hkeSznkllv+Hx/MtYDaHA5VcF0yL3KVlvzp8bWjQ" crossorigin="anonymous">');
	}
	injectNewFontAwesome();
	function replaceIcons() {
		if (document.querySelectorAll('link[href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css"]').length == 1) injectNewFontAwesome();
		var fa = document.getElementsByClassName('fa');
		for (var i = fa.length - 1; i >= 0; i--) {
			fa[i].classList.replace('fa','fas');
		}
		var fbicon = document.getElementsByClassName('fa-facebook-square')[0];
		if (fbicon !== undefined) fbicon.classList.replace('fas','fab');
		var replace = [['.fa-pencil','fa-pencil-alt'],
					   ['.fa-check-square-o','fa-star'],
					   ['.fa-puzzle-piece','fa-bolt'],
					   ['.fa-user','fa-user-circle'],
					   ['#prof-action-links a[title*="from friends"] .fa-user','fa-user-minus'],
					   ['#prof-action-links a[title*="to friends"] .fa-user','fa-user-plus'],
					   ['.fa-music','fa-guitar'],
					   ['.fa-film','fa-video'],
					   ['.fa-tv','fa-desktop'],
					   ['.fa-users','fa-user-friends'],
					   ['.fa-comment','fa-comments'],
					   ['.fa-envelope','fa-envelope-open'],
					   ['.fa-exchange','fa-comments'],
					   ['.fa-trash-o','fa-trash'],
					   ['.fa-globe','fa-globe-americas'],
					   ['.fa-rss','fa-comments'],
					   ['.indexTitleBar .fa-flag','fa-sync-alt'],
					   ['.fa-quote-left','fa-quote-right'],
					   ['.fa-backward','fa-chevron-left'],
					   ['#controls .fa-star','fa-check'],
					   ['.fa-plus-square-o','fa-address-book'],
					   ['.fa-exclamation-triangle','fa-flag'],
					   ['.fa-picture-o','fa-images'],
					   ['.fa-refresh','fa-sync-alt'],
					   ['#sCtlBox .fa-check-square','fa-save']
					   ];
		for (var i = replace.length - 1; i >= 0; i--) {
			var elems = document.querySelectorAll(replace[i][0]);
			for (var j = elems.length - 1; j >= 0; j--) {
				var cl = replace[i][0].split(" ");
				elems[j].classList.replace(cl[cl.length - 1].substring(1),replace[i][1]);
				elems[j].innerText = elems[j].innerText.replace(' -','').replace(/[ ]$/g,'').replace(/^[ ]/g,'');
			}
		}

		//GENDER FIX

		function swapWrongGender(gen1,gen2) {
			var gender = document.querySelectorAll('a.' + gen1 + ':not(.ppThLink)');
			for (var i = gender.length - 1; i >= 0; i--) {
				if (gender[i] !== undefined) {
					var secondParentNode = gender[i].parentNode.parentNode.querySelectorAll('a')[0];
					if (!secondParentNode.classList.contains(gen1)) secondParentNode.classList.replace(gen2,gen1);
				}
			}
		}
		swapWrongGender('male','female');
		swapWrongGender('female','male');


		//REMOVING EMPTY BLOCKS
		var blocks = document.querySelectorAll('.rUsers, .profModules, .rbox_main, .sResCont, .profModuleBox');
		for (var i = blocks.length - 1; i >= 0; i--) {
			if (blocks[i] !== undefined && (blocks[i].querySelectorAll('*').length == 0 || blocks[i].innerText == '' || (blocks[i].childNodes[1] !== undefined && blocks[i].childNodes[1].id == 'membStats' && blocks[i].childNodes.length == 3) || (blocks[i].querySelectorAll('.rUserBox > #rUserData')[0] !== undefined && blocks[i].querySelectorAll('.rUserBox > #rUserData')[0].querySelectorAll('*').length == 0))) blocks[i].remove();
		}
	}
	function convertFlag(input) {
		switch(input) {
			case 'en':
			case 'cy':
				return 'gb';
			case 'gd':
				return 'gb-sct';
			case 'ja':
				return 'jp';
			case 'ko':
				return 'kr';
			case 'zh':
			case 'bo':
			case 'ug':
				return 'cn';
			default:
				return input;
		}
	}
	function convertLang(input) {
		switch(input) {
			case 'en':
				return 'gb';
			case 'gd':
				return 'gb-sct';
			case 'cy':
				return 'gb-wls';
			case 'ja':
				return 'jp';
			case 'ko':
				return 'kr';
			case 'zh':
			case 'bo':
			case 'ug':
				return 'cn';
			case 'aa':
			case 'am':
				return 'et';
			case 'ay':
				return 'bo';
			case 'ar':
				return 'sa';
			case 'af':
			case 'zu':
			case 'xh':
			case 'ts':
				return 'za';
			case 'ab':
			case 'ka':
				return 'ge';
			case 'as':
			case 'aw':
			case 'b1':
			case 'bh':
			case 'dk':
			case 'gu':
			case 'hi':
			case 'ki':
			case 'kn':
			case 'ks':
			case 'ml':
			case 'mr':
			case 'mw':
			case 'or':
			case 'sa':
			case 'te':
				return 'in';
			case 'be':
				return 'by';
			case 'ba':
			case 'ce':
			case 'tt':
				return 'ru';

			case 'bi':
				return 'vu';
			case 'bl':
			case 'pa':
			case 'sd':
			case 'ur':
				return 'pk';
			case 'bn':
				return 'bd';
			case 'br':
			case 'co':
				return 'fr';
			case 'ca':
				return 'es-ct';
			case 'gl':
				return 'es';
			case 'cb':
			case 'tl':
				return 'ph';
			case 'cs':
				return 'cz';
			case 'da':
				return 'dk';
			case 'dz':
				return 'bt';
			case 'el':
				return 'gr';
			case 'et':
				return 'ee';
			case 'eu':
				return 'fr';
			case 'fa':
			case 'tg':
				return 'tj';
			case 'fy':
				return 'nl';
			case 'ga':
				return 'ie';
			case 'gn':
				return 'py';
			case 'ha':
				return 'ne';
			case 'hy':
				return 'am';
			case 'ik':
				return 'um';
			case 'in':
			case 'jw':
			case 'su':
				return 'id';
			case 'iw':
				return 'il';
			case 'kb':
				return 'dz';
			case 'kk':
				return 'kz';
			case 'kl':
				return 'gl';
			case 'km':
				return 'kh';
			case 'ku':
				return 'iq';
			case 'ky':
				return 'kg';
			case 'lb':
				return 'lu';
			case 'lm':
				return 'it';
			case 'ln':
				return 'cd';
			case 'lo':
				return 'la';
			case 'mi':
				return 'nz';
			case 'mo':
				return 'md';
			case 'ms':
				return 'my';
			case 'mu':
				return 'mz';
			case 'my':
				return 'mm';
			case 'na':
				return 'nr';
			case 'ne':
			case 'b2':
				return 'np';
			case 'oc':
				return 'fr';
			case 'om':
				return 'et';
			case 'ps':
				return 'af';
			case 'qu':
				return 'pe';
			case 'ri':
				return 'ma';
			case 'rm':
				return 'ch';
			case 'rn':
				return 'bi';
			case 'sg':
				return 'cf';
			case 'sh':
			case 'sr':
				return 'rs';
			case 'si':
				return 'lk';
			case 'sl':
				return 'si';
			case 'sm':
				return 'ws';
			case 'sn':
				return 'zw';
			case 'sq':
				return 'al';
			case 'ss':
				return 'sz';
			case 'st':
				return 'ls';
			case 'sv':
				return 'se';
			case 'sw':
				return 'ke';
			case 'ta':
				return 'lk';
			case 'ti':
				return 'er';
			case 'tk':
				return 'tm';
			case 'tm':
				return 'ml';
			case 'tn':
				return 'bw';
			case 'tw':
			case 'ak':
				return 'gh';
			case 'tz':
				return 'ma';
			case 'uk':
				return 'ua';
			case 'vi':
				return 'vn';
			case 'wo':
				return 'sn';
			case 'yo':
				return 'ng';
			case 'vo':
			case 'eo':
			case 'la':
			case 'ie':
			case 'ia':
			case 'ji':
				return '???';
			default:
				return input;
		}
	}
	function injectFlagsCss() { document.getElementsByTagName('head')[0].insertAdjacentHTML('beforeend', '<link rel="stylesheet" href="https://nmr1.ru/troot/flags/css/flag-icon.css">'); }
	injectFlagsCss();
	function replaceFlags() {
		if (document.querySelectorAll('link[href="https://nmr1.ru/troot/flags/css/flag-icon.css"]').length == 0) injectFlagsCss();
		function replaceAllFlags(size,isLang) {
			var flags;
			if (!isLang) flags = document.querySelectorAll('img[src*="/images/flags/iso/' + size + '"]');
			else flags = document.querySelectorAll('img[src*="/images/flags/langs"]');
			for (var i = flags.length - 1; i >= 0; i--) {
				var src = flags[i].getAttribute('src'),
					country = src.substring(src.lastIndexOf(size)).substring(3,5),
					newFlag = document.createElement('span'),
					sizeClass = 'flag-icon' + size,
					countryCode;
				if (!isLang) countryCode = 'flag-icon-' + convertFlag(country.toLowerCase());
				else countryCode = 'flag-icon-' + convertLang(country.toLowerCase());
				if (flags[i].parentNode.className == "sResMain") {
					flags[i].parentNode.parentNode.insertAdjacentHTML('beforebegin','<div class="flagwrap"><span class="flag-icon flag-icon16 ' + countryCode + '"></span></div>');
					flags[i].parentNode.parentNode.parentNode.classList.add('withflag');
				}
				newFlag.classList.add('flag-icon', sizeClass, countryCode);
				if (isLang) newFlag.classList.add('flag-icon-squared');
				flags[i].parentNode.replaceChild(newFlag, flags[i]);
			}
		}
		replaceAllFlags(16, false);
		replaceAllFlags(24, false);
		replaceAllFlags(40, true);


		var ages = document.querySelectorAll('.user_info');
		if (ages.length > 0) {
			for (var i = ages.length - 1; i >= 0; i--) {
				var maxWidth = 76;
				if (ages[i].querySelectorAll('.status').length == 0) maxWidth = 90;
				if (ages[i].querySelectorAll('a')[0].getBoundingClientRect().width > maxWidth) {
					if (ages[i].querySelectorAll('.location')[0].getBoundingClientRect().width <= 90) {
						ages[i].innerHTML = ages[i].innerHTML.replace(/[ ]([\d][\d])/g, '<span class="age">$1 </span>');
						var original = ages[i].querySelectorAll('.age')[0];
						if (original !== undefined) {
							ages[i].querySelectorAll('.location')[0].insertAdjacentHTML('afterbegin', original.innerHTML);
							original.remove();
						}
					}
					else ages[i].innerHTML = ages[i].innerHTML.replace(/[ ]([\d][\d])/g, '');
				}
			}
		}
	}
	setInterval(function(){
		if (document.getElementsByClassName('fa').length >= 1) replaceIcons();
		if (document.querySelectorAll('img[src*="/images/flags/"]').length >= 1) replaceFlags();

		function replaceLayout(selector, regex, replacewith) {
			if (document.querySelectorAll(selector + ':not(.new)').length >= 1) {
				var elems = document.querySelectorAll(selector);
				for (var i = elems.length - 1; i >= 0; i--) {
					if (elems[i] !== undefined) {
						for (var j = regex.length - 1; j >= 0; j--) {
							elems[i].innerHTML = elems[i].innerHTML.replace(regex[j],replacewith[j]);
						}
						elems[i].classList.add('new');
					}
				}
			}
		}
		replaceLayout('.mprofSections', [/[|]/g], ['<span class="mprofSectionsSeparator">/</span>']);
		replaceLayout('.settingSections', [/[|]/g], ['<span class="settingSectionsSeparator">/</span>']);
		replaceLayout('#prof-action-links', [/[ ][-]/g,/[\n][-]/g], ['','']);
		replaceLayout('#langexForm ~ div', [/[\&]\w+[;][\n ]/g], ['']);

		if (document.querySelectorAll('.sResThumb img:not(.new)').length >= 1) {
			var profActionLinks = document.querySelectorAll('.sResThumb img');
			for (var i = profActionLinks.length - 1; i >= 0; i--) {
				if (profActionLinks[i] !== undefined) {
					profActionLinks[i].setAttribute('src',profActionLinks[i].getAttribute('src').replace('100x100','180x180'));
					profActionLinks[i].classList.add('new');
				}
			}
		}
	},100);
})();