Transalvador Improver

Adiciona a quantidade de horário e a frequência média entre as linhas no site da Transvaldor; possibilita a busca pelas linhas apertando ENTER; Faz com que apareça o número das linhas filhas ao exibir o resultado da pesquisa

当前为 2017-04-24 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Transalvador Improver
// @namespace   none
// @version     1.03
// @author	Roger Lima ([email protected])
// @grant       none
// @description Adiciona a quantidade de horário e a frequência média entre as linhas no site da Transvaldor; possibilita a busca pelas linhas apertando ENTER; Faz com que apareça o número das linhas filhas ao exibir o resultado da pesquisa
// @include	http://www.transalvador.salvador.ba.gov.br/?pagina=onibus/onibus
// @date	29/mar/2014
// @rewrite	16/apr/2016
// @update	24/apr/2017
// ==/UserScript==

/* jshint expr: true, esversion: 6 */
/* global jQuery, buscar_linha, buscar_codigo, change_busca */

( function( $ ) {
'use strict';

var sf_interval;

// Format the hours
// @param {number} Time in seconds
// @return {string}
function format_time( time ) {
	var hour = Math.floor( time / 60 ),
		minutes = Math.floor( time % 60 );

	return ( hour > 0 ? ( ( hour < 10 && '0' ) + hour + 'h' ) : '' ) + ( ( minutes < 10 && '0' ) + minutes + 'min' );
}

// Show the ammount of travels and the average between them
// @return {function} closure
function schedule_features() {
	var hours_day = 0,
		hours_afternoon = 0,
		hours_night = 0,
		hours_total = 0,
		average_day = 0,
		average_afternoon = 0,
		average_night = 0,
		average_general = 0;

	return function() {
		var hour_part, prev_minutes, current_minutes, average_increment;

		if ( !$( '#frmHorario' ).length )
			return;

		if ( !$( '#schedule_amount' ).length ) {
			$( '.tit02' ).eq( 0 ).append( '<div id="schedule_amount" style="float:right;">Quantidade de horários</div>' );
			$( '.tit02' ).eq( 1 ).append(
				$( '<div id="schedule_amount_total_text" style="float:right"> - Total: </div>' ).append( $( '<div id="schedule_amount_total" style="float:right" />' ) ),
				$( '<div id="schedule_amount_night_text" style="float:right"> - Noite: </div>' ).append( $( '<div id="schedule_amount_night" style="float:right" />' ) ),
				$( '<div id="schedule_amount_afternoon_text" style="float:right"> - Tarde: </div>' ).append( $( '<div id="schedule_amount_afternoon" style="float:right" />' ) ),
				$( '<div id="schedule_amount_day_text" style="float:right">Dia: </div>' ).append( $( '<div id="schedule_amount_day" style="float:right" />' ) )
			);
			$( '.tit02' ).eq( 1 ).parent().parent().next().prepend(
				$( '<div id="schedule_average" style="float:right">Média entre as saídas</div>' ), '<br />',
				$( '<div id="schedule_average_general_text" style="float:right"> - Geral: </div>' ).append( $( '<div id="schedule_average_general" style="float:right" />' ) ),
				$( '<div id="schedule_average_night_text" style="float:right"> - Noite: </div>' ).append( $( '<div id="schedule_average_night" style="float:right" />' ) ),
				$( '<div id="schedule_average_afternoon_text" style="float:right"> - Tarde: </div>' ).append( $( '<div id="schedule_average_afternoon" style="float:right" />' ) ),
				$( '<div id="schedule_average_day_text" style="float:right">Dia: </div>' ).append( $( '<div id="schedule_average_day" style="float:right" />' ) )
			);
		}

		// Gets the data
		$( '#content table' ).eq( 0 ).find( 'td div' ).each( function( i ) {
			if ( $( this ).html().search( /\d{2}:\d{2}/ ) === -1 )
				return;

			hour_part = $( this ).html().split( ':' );
			current_minutes = parseInt( hour_part[ 0 ] ) * 60 + parseInt( hour_part[ 1 ] );

			if ( i === 3 || !prev_minutes )
				prev_minutes = current_minutes;

			if ( i > 3 ) {
				average_increment = current_minutes < prev_minutes ? 60 - hour_part[ 1 ] + current_minutes : current_minutes - prev_minutes;
				prev_minutes = current_minutes;

				$( this ).parent().attr( 'bgcolor' ) === '#6699CC' && ( average_day += average_increment );
				$( this ).parent().attr( 'bgcolor' ) === '#eaedf4' && ( average_afternoon += average_increment );
				$( this ).parent().attr( 'bgcolor' ) === '#999CC' && ( average_night += average_increment );
			}

			( $( this ).parent().attr( 'bgcolor' ) === '#6699CC' ) && hours_day++;
			( $( this ).parent().attr( 'bgcolor' ) === '#eaedf4' ) && hours_afternoon++;
			( $( this ).parent().attr( 'bgcolor' ) === '#999CC' ) && hours_night++;
		} );

		// Sets the averages
		hours_total = ( hours_day + hours_afternoon + hours_night );
		average_general = average_day + average_afternoon + average_night;
		average_general = hours_total > 2 ? average_general / ( hours_total - 1 ) : average_general;
		average_day = hours_day > 2 ? average_day / ( hours_day - 1 ) : average_day;
		average_afternoon = hours_afternoon > 2 ? average_afternoon / hours_afternoon : average_afternoon;
		average_night = hours_night > 2 ? average_night / hours_night : average_night;

		// Sets the data on screen
		$( '#schedule_amount_day' ).html( hours_day );
		$( '#schedule_amount_afternoon' ).html( hours_afternoon );
		$( '#schedule_amount_night' ).html( hours_night );
		$( '#schedule_amount_total' ).html( hours_total < 0 ? 0 : hours_total );
		$( '#schedule_average_day' ).html( format_time( average_day ) );
		$( '#schedule_average_afternoon' ).html( format_time( average_afternoon ) );
		$( '#schedule_average_night' ).html( format_time( average_night ) );
		$( '#schedule_average_general' ).html( format_time( average_general ) );
		$( '#frmHorario img[height="20"]' ).click( () => {
			clearInterval( sf_interval );
			sf_interval = setInterval( schedule_features(), 50 );
		} );

		hours_day = hours_afternoon = hours_night = average_day = average_afternoon = average_night = average_general = 0;
	};
}

// Sets the child
// @return {undefined}
function child_set() {
	var wait_data = setTimeout( function() {
		$( '#dados tbody tr' ).each( function( index ) {
			if ( index > 0 )
				$( this ).find( 'td' )[ 0 ].textContent += '-' + /\'(\d{2})\'/.exec( $( this ).html() )[ 1 ];
		} );
	}, 150 );
}

// Main function
// @return {undefined}
function main() {
	// Enables search with ENTER button and starts child_set() function
	$( '#itemBusca' ).change( function() {
		var code, name;

		if ( $( this ).val() == '3' ) {
			// FIXEME click button don't enables child_set() function
			code = setTimeout( function() {
				$( '#codigoBusca' ).keydown( function( e ) {
					if ( e.which === 13 ) {
						e.preventDefault();
						buscar_codigo( '3' );
						child_set();
					}
				} );
			}, 50 );
		}

		if ( $( this ).val() == '4' ) {
			name = setInterval( function() {
				$( '#linhaBusca' ).keydown( function( e ) {
					clearInterval( name );

					if ( e.which === 13 ) {
						e.preventDefault();
						buscar_linha();
						child_set(); // FIXME don't work
					}
				} );
			}, 50 );
		}
	} );

	sf_interval = setInterval( schedule_features(), 50 );

	$( '#itemBusca' ).find( 'option[value="3"]' ).attr( 'selected', true ).trigger( 'change' );
	change_busca();
}

// Inits
$( main );

}( jQuery ) );