dailyhwm / Text_Counter*

// ==UserScript==
// @name Text_Counter*
// @author Demin, bonArt
// @namespace Counter
// @description Подсчёт количества введенных символов в полях ввода: форума, персональной информации, личной почты, блокнота, описания клана, рассылки по клану.
// @homepage https://greasyfork.org/ru/scripts/6799-hwm-text-counter
// @icon http://i.imgur.com/LZJFLgt.png
// @version 1.3.2
// @encoding utf-8
// @include https://*.heroeswm.*/*
// @include https://*.lordswm.com/*

// @exclude */rightcol.php*
// @exclude */ch_box.php*
// @exclude */chat*
// @exclude */ticker.html*
// @exclude */frames*
// @exclude */brd.php*

// @grant GM_deleteValue
// @grant GM_getValue
// @grant GM_listValues
// @grant GM_setValue
// @grant GM_addStyle
// @grant GM_log
// @grant GM_openInTab

// @license MIT
// ==/UserScript==
(function() {
	if (typeof GM_getValue != 'function') {
		this.GM_getValue=function (key,def) {return localStorage[key] || def;};
		this.GM_setValue=function (key,value) {return localStorage[key]=value;};
		this.GM_deleteValue=function (key) {return delete localStorage[key];};
	}

	var url_cur = location.href;
	var url = 'http://'+location.hostname+'/';

// Page detection
	var url_reply = "forum_messages.php";
	var url_newmsg = "new_topic.php";
	var url_sett = "pers_settings.php";
	var url_sms = "sms-create.php";
	var url_note = "sms.php?notebook";
	var url_clan = "clan_control.php";
	var url_clan_sms = "clan_broadcast.php";

	if ( (url_cur.indexOf(url_clan)==-1) && (url_cur.indexOf(url_note)==-1) && (url_cur.indexOf(url_reply)==-1) && (url_cur.indexOf(url_newmsg)==-1) && (url_cur.indexOf(url_sms)==-1) && (url_cur.indexOf(url_clan_sms)==-1) && (url_cur.indexOf(url_sett)==-1) ) return;

	var script_num = 6799;
	var script_name = "hwm_text_counter: Подсчет количества введенных символов в полях ввода: форума, персональной информации, личной почты, блокнота, описания клана, рассылки по клану (by Demin)";

// Text blocks
	if ( url.match('lordswm') ) {
		var msg_head = "Subject: "; //head
		var msg_body_sms = "Message: "; //message body
		var msg_body = "";
		var used_smb = "Used "; //used
		var chars_of = " chars of "; //chars of
	} else {
		var msg_head = "Тема: "; //head
		var msg_body_sms = "Тело сообщения: "; //message body
		var msg_body = "";
		var used_smb = "Введено "; //used
		var chars_of = " символов из "; //chars of
	}

// Nod generation
	if ( (url_cur.indexOf(url_sms)!=-1) || (url_cur.indexOf(url_clan_sms)!=-1) || (url_cur.indexOf(url_newmsg)!=-1) ) {
		var tr_th = document.createElement('tr');
		var td_th = document.createElement('td');
		var sp_th = document.createElement('span');

		tr_th.appendChild(td_th);
		td_th.appendChild(sp_th);
	}

	var tr_ta = document.createElement('tr');
	var td_ta = document.createElement('td');
	var sp_ta = document.createElement('span');

	tr_ta.appendChild(td_ta);
	td_ta.appendChild(sp_ta);

// Pages parameters
	var t_col = 0;
	var msg_ta = document.querySelector("textarea");
	if ( !msg_ta ) return;
	var msg_th = document.querySelector("form");
//subj.maxLength = maxLength;

	if ( url_cur.indexOf(url_reply)!=-1 ) {
		var max_chars_ta = 4029;

		t_col = 2;
		msg_ta.rows = 15;
	}

	if ( url_cur.indexOf(url_newmsg)!=-1 ) {
        msg_th = msg_th.querySelector("input[type='text']");

		var max_chars_th = 80;
		var max_chars_ta = 3000;
		msg_body = msg_body_sms;

		t_col = 2;
		msg_ta.rows = 10;
	}

	if ( url_cur.indexOf(url_sett)!=-1 ) {
		var max_chars_ta = 4019;

		t_col = 2;
		msg_ta.rows = 12;
	}

	if ( url_cur.indexOf(url_sms)!=-1 ) {
		msg_th = msg_th.querySelectorAll("input[type='text']")[1];

		var max_chars_th = 70;
		var max_chars_ta = 3900;
		msg_body = msg_body_sms;

		t_col = 2;
		msg_ta.rows = 20;
		msg_ta.style.width = 900;
	}

	if ( url_cur.indexOf(url_note)!=-1 ) {
		var max_chars_ta = 3063;

		td_ta.className = "wblight";
		td_ta.style.borderTopWidth = "0px";
		msg_ta.parentNode.className = "wblight";
		msg_ta.parentNode.style.borderBottomWidth = "0px";
		msg_ta.parentNode.parentNode.parentNode.parentNode.cellSpacing = 0;

		t_col = 1;
		msg_ta.rows = 30;
	}

	if ( url_cur.indexOf(url_clan)!=-1 ) {
		var max_chars_ta = 9800;

		td_ta.className = "wbwhite";
		td_ta.style.borderTopWidth = "0px";
		msg_ta.parentNode.style.borderBottomWidth = "0px";

		t_col = 1;
		msg_ta.rows = 30;
	}

	if ( url_cur.indexOf(url_clan_sms)!=-1 ) {
		msg_th = msg_th.querySelector("input[type='text']");
		msg_th.style.width = "100%";

		var max_chars_th = 70;
		var max_chars_ta = 3900;
		msg_body = msg_body_sms;

		t_col = 2;
		msg_ta.rows = 20;
		msg_ta.style.width = 900;
	}

// Text generation
	if ( (url_cur.indexOf(url_sms)!=-1) || (url_cur.indexOf(url_clan_sms)!=-1) || (url_cur.indexOf(url_newmsg)!=-1) ) {
		sp_th.innerHTML = msg_head + used_smb + "0" + chars_of + max_chars_th;
		td_th.colSpan = t_col;
		sp_th.style.marginLeft = "25";
		sp_th.style.fontSize = "12px";
	}

	sp_ta.innerHTML = msg_body + used_smb + "0" + chars_of + max_chars_ta;
	td_ta.colSpan = t_col;
	sp_ta.style.marginLeft = "25";
	sp_ta.style.fontSize = "12px";

// Inserting nod blocks into a document
	if ( (url_cur.indexOf(url_sms)!=-1) || (url_cur.indexOf(url_clan_sms)!=-1) || (url_cur.indexOf(url_newmsg)!=-1) ) {
		insertAfter(msg_ta, tr_ta, tr_th);
		countCharsChange_th();
	} else {
		insertAfter(msg_ta, tr_ta);
	}

	countCharsChange_ta();

// Keypress listener
	if ( (url_cur.indexOf(url_sms)!=-1) || (url_cur.indexOf(url_clan_sms)!=-1) || (url_cur.indexOf(url_newmsg)!=-1) ) {
		addEvent( msg_th, "keydown", countChars_th );
		addEvent( msg_th, "click", countChars_th );
		addEvent( msg_th, "change", countChars_th );
		addEvent( msg_th, "mouseout", countChars_th );
		addEvent( msg_th, "mouseover", countChars_th );
	}

	addEvent( msg_ta, "keydown", countChars_ta );
	addEvent( msg_ta, "click", countChars_ta );

// Functions
	function insertAfter(parent, node, node2) {
		if ( url_cur.indexOf(url_sett)==-1 ) {
			while ( parent.tagName != 'TR' ) { parent = parent.parentNode; }
		}
		parent.parentNode.insertBefore(node, parent.nextSibling);

		if ( node2 ) { parent.parentNode.insertBefore(node2, parent.nextSibling); }
	}

	function countChars_th() {
		setTimeout( function() { countCharsChange_th(); }, 100 ); // workaround for pasting from clipboard
	}

	function countChars_ta() {
		setTimeout( function() { countCharsChange_ta(); }, 100 );
	}

	function countCharsChange_th() {
		sp_th.innerHTML = msg_head + used_smb + msg_th.value.length + chars_of + max_chars_th;
		sp_th.style.backgroundColor = ( msg_th.value.length > max_chars_th ) ? "#fcc" : "";
	}

	function countCharsChange_ta() {
		sp_ta.innerHTML = msg_body + used_smb + msg_ta.value.length + chars_of + max_chars_ta;
		sp_ta.style.backgroundColor = ( msg_ta.value.length > max_chars_ta ) ? "#fcc" : "";
	}

	function $(id) { return document.querySelector("#"+id); }

	function addEvent(elem, evType, fn) {
		if (elem.addEventListener) {
			elem.addEventListener(evType, fn, false);
		}
		else if (elem.attachEvent) {
			elem.attachEvent("on" + evType, fn);
		}
		else {
			elem["on" + evType] = fn;
		}
	}
})();