Участник:ShinePhantom/highlightLastMessages.js

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
if(typeof highlightMessages === 'undefined')
	var highlightMessages=10;
if(typeof messagesHighlightColor === 'undefined')	
	var messagesHighlightColor="#FFE080";
if(typeof highlightMessagesAfterLastVisit === 'undefined')	
	var highlightMessagesAfterLastVisit=true;
if(typeof messagesHighlightTimeout === 'undefined')	
	var messagesHighlightTimeout=15;
var HLMMonths=new Array();
HLMMonths['января']='01';
HLMMonths['февраля']='02';
HLMMonths['марта']='03';
HLMMonths['апреля']='04';
HLMMonths['мая']='05';
HLMMonths['июня']='06';
HLMMonths['июля']='07';
HLMMonths['августа']='08';
HLMMonths['сентября']='09';
HLMMonths['октября']='10';
HLMMonths['ноября']='11';
HLMMonths['декабря']='12';
var HLMRegExp=/(\d\d):(\d\d), (\d\d?) ([а-я]+) 20(\d\d) \(UTC\)\s*/, HLMDelayed=new Array();
function highlightLastMessages(){
	if(mw.config.get('wgNamespaceNumber')!=4&&!$('body').hasClass('ns-talk'))
		return;
	var messages=$('p:contains("UTC"), li:contains("UTC"), dd:contains("UTC")').get();
	if(highlightMessagesAfterLastVisit){
		var firstTime=parseInt(window.localStorage.getItem('lv1'+mw.config.get('wgNamespaceNumber')+':'+mw.config.get('wgTitle')));
		var now=new Date(), secondTime=0, timeOfLastVisit;
		if(!firstTime)
			window.localStorage.setItem('lv1'+mw.config.get('wgNamespaceNumber')+':'+mw.config.get('wgTitle'), now.getTime());
		else if(now.getTime()-(secondTime=parseInt(window.localStorage.getItem('lv2'+mw.config.get('wgNamespaceNumber')+':'+mw.config.get('wgTitle'))))>
				 messagesHighlightTimeout*60000){
			window.localStorage.setItem('lv1'+mw.config.get('wgNamespaceNumber')+':'+mw.config.get('wgTitle'), secondTime);
			timeOfLastVisit=secondTime;
		}else
			timeOfLastVisit=firstTime;
		window.localStorage.setItem('lv2'+mw.config.get('wgNamespaceNumber')+':'+mw.config.get('wgTitle'), now.getTime());
		if(!firstTime)
			return;
		var lastVisitDate=new Date(timeOfLastVisit);
		var lastVisit=(lastVisitDate.getUTCFullYear()%100*100000000+
					  (lastVisitDate.getUTCMonth()+1)*1000000+
					  lastVisitDate.getUTCDate()*10000+
					  lastVisitDate.getUTCHours()*100+
					  lastVisitDate.getUTCMinutes())+
					  '';
		var HLMMatch=null, messageDate='0', cropped='', lenghtBeforeCropping=0;
		for(i in messages){
			lenghtBeforeCropping=messages[i].innerHTML.length;
			cropped=HLMCrop(messages[i].innerHTML);
			if(HLMMatch=HLMRegExp.exec(cropped)){
				messageDate=HLMMatch[5]+HLMMonths[HLMMatch[4]]+(HLMMatch[3].length==1?'0':'')+HLMMatch[3]+HLMMatch[1]+HLMMatch[2];
				if(messageDate<lastVisit)
					continue;
				if(lenghtBeforeCropping>cropped.length){
					messages[i].id='hlm'+HLMDelayed.length;
					HLMDelayed.push(cropped.length);
				}else{
					messages[i].style.backgroundColor=messagesHighlightColor;
					HLMHighlightPrevBlock(messages[i]);
				}
			}
		}
		HLMHighlightDelayed();
	}else{
		messages=messages.sort(HLMSortCallback);
		if(messages.length==1)
			HLMPrepareOneMessage(messages[0]);
		var i=highlightMessages;
		for(j in messages){
			if(messages[j].HLMType==2||!i--)
				break;
			if(messages[j].HLMType==0){
				messages[j].style.backgroundColor=messagesHighlightColor;
				HLMHighlightPrevBlock(messages[j]);
			}else{
				messages[j].id='hlm'+HLMDelayed.length;
				HLMDelayed.push(messages[j].HLMMessageLength);
			}
		}
		HLMHighlightDelayed();
	}
}
function HLMSortCallback(a, b){
	var HLMMatch=null, dateA='0', dateB='0', cropped='', lenghtBeforeCropping=0;
	lenghtBeforeCropping=a.innerHTML.length;
	cropped=HLMCrop(a.innerHTML);
	if(HLMMatch=HLMRegExp.exec(cropped)){
		dateA=HLMMatch[5]+HLMMonths[HLMMatch[4]]+(HLMMatch[3].length==1?'0':'')+HLMMatch[3]+HLMMatch[1]+HLMMatch[2];
		if(lenghtBeforeCropping>cropped.length){
			a.HLMType=1;
			a.HLMMessageLength=cropped.length;
		}else
			a.HLMType=0;
	}else{
		a.HLMType=2;
		dateA='0'
	}
	lenghtBeforeCropping=b.innerHTML.length;
	cropped=HLMCrop(b.innerHTML);
	if(HLMMatch=HLMRegExp.exec(cropped)){
		dateB=HLMMatch[5]+HLMMonths[HLMMatch[4]]+(HLMMatch[3].length==1?'0':'')+HLMMatch[3]+HLMMatch[1]+HLMMatch[2];
		if(lenghtBeforeCropping>cropped.length){
			b.HLMType=1;
			b.HLMMessageLength=cropped.length;
		}else
			b.HLMType=0;
	}else{
		b.HLMType=2;
		dateB='0'
	}
	if(dateA<dateB)
		return 1;
	if(dateA>dateB)
		return -1;
	return 0;
}
function HLMCrop(str){
	var i;
	if((i=str.indexOf('<dl>'))!=-1)
		str=str.substr(0, i);
	if((i=str.indexOf('<ul>'))!=-1)
		str=str.substr(0, i);
	if((i=str.indexOf('<ol>'))!=-1)
		str=str.substr(0, i);
	return str;
}
function HLMHighlightDelayed(){
	var message=null;
	for(i in HLMDelayed){
		message=document.getElementById('hlm'+i);
		message.innerHTML='<div style="background-color: '+messagesHighlightColor+';">'+
						  message.innerHTML.substr(0, HLMDelayed[i])+
						  '</div>'+
						  message.innerHTML.substr(HLMDelayed[i]);
	}
}
function HLMPrepareOneMessage(message){
	var HLMMatch=null, cropped='', lenghtBeforeCropping=0;
	lenghtBeforeCropping=message.innerHTML.length;
	cropped=HLMCrop(message.innerHTML);
	if(HLMMatch=HLMRegExp.exec(cropped)){
		if(lenghtBeforeCropping>cropped.length){
			message.HLMType=1;
			message.HLMMessageLength=cropped.length;
		}else
			message.HLMType=0;
	}else
		message.HLMType=2;
}
function HLMHighlightPrevBlock(curBlock, lastBlock){
	if(curBlock.nodeName=='LI')
		return;
	if(!lastBlock)
		lastBlock=curBlock;
	var prevBlock=curBlock.previousSibling;
	if(!prevBlock)
		return;
	if(prevBlock.nodeName=='#text')
		HLMHighlightPrevBlock(prevBlock, lastBlock);
	if(prevBlock.nodeName==lastBlock.nodeName&&
	   (prevBlock.nodeName!='DD'||prevBlock.innerHTML.indexOf('<dl>')==-1)&&
	   !HLMRegExp.exec(prevBlock.innerHTML)){
		prevBlock.style.backgroundColor=messagesHighlightColor;
		HLMHighlightPrevBlock(prevBlock);
	}
}

$(highlightLastMessages);