MediaWiki:Gadget-wikidebug.js: Różnice pomiędzy wersjami

Z Wikipedii, wolnej encyklopedii
Usunięta treść Dodana treść
wyswietlaj blad takze w konsoli
dynamicznie aktualizuj licznik bledow oraz ikonke
Linia 34: Linia 34:
'error' : '//upload.wikimedia.org/wikipedia/commons/0/0d/Wikidebug_state_bug_occurred.png'
'error' : '//upload.wikimedia.org/wikipedia/commons/0/0d/Wikidebug_state_bug_occurred.png'
},
},
strState = 'active';
strState = 'active',
$errorCount = null,
$errorIcon = null;


window.onerror = function ( strErrMsg, strFileName, intLine ) {
window.onerror = function ( strErrMsg, strFileName, intLine ) {
Linia 45: Linia 47:
} );
} );
refreshErrorIndicators();
return false; // forward to default event handler, log on console
return false; // forward to default event handler, log on console
};
};

function refreshErrorIndicators() {
if ( $errorCount && $errorIcon ) {
$errorCount.text( mw.format( '($1)', errors.length ) );
$errorIcon.attr( 'src', stateIcons[ strState ] );
}
}


function getBrowser() {
function getBrowser() {
Linia 71: Linia 81:
} ).join( '\n' )
} ).join( '\n' )
).trim();
).trim();
OO.ui.alert( $( '<textarea>' ).attr( 'rows', 15 ).text( msg ), {
mw.loader.using( 'oojs-ui-core' ).done( function () {
OO.ui.alert( $( '<textarea>' ).attr( 'rows', 15 ).text( msg ), {
size: 'large',
title: 'wikidebug'
size: 'large',
title: 'wikidebug'
} );
} );
} );
}
}


$( function () {
$( function () {
$errorCount = $( '<span>' );
$errorIcon = $( '<img>' );
refreshErrorIndicators();
$( mw.util.addPortletLink( 'p-personal', '#', 'wikidebug', 'p-wikidebug', 'wikidebug' ) )
$( mw.util.addPortletLink( 'p-personal', '#', 'wikidebug', 'p-wikidebug', 'wikidebug' ) )
.append( [
.append( [ ' ', $errorCount, $errorIcon ] )
.on( 'click', showInfo );
' ',
$( '<span>' ).text( errors.length ? mw.format( '($1)', errors.length ) : '' ),
$( '<img>' ).attr( 'src', stateIcons[ strState ] )
] )
.on( 'click', function ( evt ) {
mw.loader.using( 'oojs-ui-core' ).done( showInfo );
} );
} );
} );

Wersja z 21:39, 18 gru 2021

/**
	@package wikidebug
	@brief Script for debugging JS scripts

	@section Dependencies
	- jQuery - weakly dependent (might be easily rewritten to use something else); uses the "jQuery" object
	- mediawiki.util - for addPortletLink()
	- oojs-ui-core - for OO.ui.alert()

	@section done Functionality
	- Collect errors in an array.
	- Add a debugger icon.
	- Display an error indicator (different icon).
	- Display number of errors caught near the icon.
	- Show errors and browser info upon click on an icon.
	- Display a list of scripts in the info alert.

	@todo
	- Change info alert to some menu or make a right-click menu.
	- Debugger deactivation when the icon(?) is clicked - or maybe when an option is choose (save in a cookie).
	- Check if window.onerror is actualy working. So far:
		- working in: IE, FF
		- not working in: Opera, Chrome, Safari
	- i18n, titles.
	- styling (ids/classes + CSS)
	- Add a link to start wiki debugging mode (document.cookie="resourceLoaderDebug=1;path=/").
	- Display a list of styles?
*/

var errors = [],
	stateIcons = {
		'active' : '//upload.wikimedia.org/wikipedia/commons/1/16/Wikidebug_state_bug.png',
		'inactive' : '//upload.wikimedia.org/wikipedia/commons/4/44/Wikidebug_state_bug_inactive.png',
		'error' : '//upload.wikimedia.org/wikipedia/commons/0/0d/Wikidebug_state_bug_occurred.png'
	},
	strState = 'active',
	$errorCount = null,
	$errorIcon = null;

window.onerror = function ( strErrMsg, strFileName, intLine ) {
	strState = 'error';
	
	errors.push( {
		strErrMsg: strErrMsg,
		strFileName: strFileName,
		intLine: intLine
	} );
	
	refreshErrorIndicators();
	return false; // forward to default event handler, log on console
};

function refreshErrorIndicators() {
	if ( $errorCount && $errorIcon ) {
		$errorCount.text( mw.format( '($1)', errors.length ) );
		$errorIcon.attr( 'src', stateIcons[ strState ] );
	}
}

function getBrowser() {
	return navigator.userAgent;
}

function getScripts() {
	var scripts = document.getElementsByTagName( 'script' );
	
	return [].slice.call( scripts ).reduce( function ( arr, el ) {
		return el.src ? arr.concat( el.src ) : arr;
	}, [] );
}

function showInfo() {
	var msg = mw.format(
			'=== Browser ===\n$1\n\n=== Scripts ===\n$2\n\n=== Errors ===\n$3',
			getBrowser(),
			getScripts().map( function ( script ) {
					return '* ' + script;
				} ).join( '\n' ),
			errors.map( function ( err ) {
					return mw.format( '* [$1 @$2] $3', err.strFileName, err.intLine, err.strErrMsg );
				} ).join( '\n' )
		).trim();
	
	mw.loader.using( 'oojs-ui-core' ).done( function () {
		OO.ui.alert( $( '<textarea>' ).attr( 'rows', 15 ).text( msg ), {
			size: 'large',
			title: 'wikidebug'
		} );
	} );
}

$( function () {
	$errorCount = $( '<span>' );
	$errorIcon = $( '<img>' );
	
	refreshErrorIndicators();
	
	$( mw.util.addPortletLink( 'p-personal', '#', 'wikidebug', 'p-wikidebug', 'wikidebug' ) )
		.append( [ ' ', $errorCount, $errorIcon ] )
		.on( 'click', showInfo );
} );