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

Z Wikipedii, wolnej encyklopedii
Usunięta treść Dodana treść
m trim()
wyswietlaj blad takze w konsoli
Linia 45: Linia 45:
} );
} );
return false; // forward to default event handler, log on console
return true;
};
};



Wersja z 21:27, 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';

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

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();
			
	OO.ui.alert( $( '<textarea>' ).attr( 'rows', 15 ).text( msg ), {
		size: 'large',
		title: 'wikidebug'
	} );
}

$( function () {	
	$( mw.util.addPortletLink( 'p-personal', '#', 'wikidebug', 'p-wikidebug', 'wikidebug' ) )
		.append( [
			' ',
			$( '<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 );
		} );
} );