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

Usunięta treść Dodana treść
Krinkle (dyskusja | edycje)
Minor clean up
rework
Linia 5:
@section Dependencies
- jQuery - weakly dependent (might be easily rewritten to use something else); uses the "jQuery" object
- sftJSmsgmediawiki.util - used for nice alertsaddPortletLink()
- oojs-ui-core - for OO.ui.alert()
 
@section done Functionality
Linia 27 ⟶ 28:
*/
 
var errors = [],
/**
stateIcons = {
@brief Class for debugging
*/
function wikiDbgClass()
{
//! Version of class
this.ver = this.version = '1.0.2';
 
//! @brief Array of errors that were caught
//! this is an array of objects: {strErrMsg:'error message', strFileName:'some.script.name.js', intLine:123}
this.arrErrors = [];
 
//! <private> variable containing an object of the class
var self = this;
 
//! <private> Icons urls depending on state of the debugger
var oStateIcons = {
'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/3/39/Wikidebug_state_bug_occured.png'
};,
strState = 'active';
// test ver.
if (location.hostname == 'localhost')
{
oStateIcons = {
'active' : 'img/bug.png',
'inactive' : 'img/bug_inactive.png',
'error' : 'img/bug_occured.png'
};
}
 
window.onerror = function ( strErrMsg, strFileName, intLine ) {
//! <private> state of the debugger
var strState = 'activeerror'; // for now always active
errors.push( {
strErrMsg: strErrMsg,
strFileName: strFileName,
intLine: intLine
} );
return true;
};
 
function getBrowser() {
//! <private> Main container element
return navigator.userAgent;
var elContainer = null;
}
//! <private> Icon element
var elIcon = null;
//! <private> Number of bugs indicator element
var elNumBugs = null;
 
function getScripts() {
/**
var scripts = document.getElementsByTagName( 'script' );
@brief <private> Error handler to be run onerror
*/
return [].slice.call( scripts ).reduce( function ( arr, el ) {
function errorHandler(strErrMsg, strFileName, intLine)
return el.src ? arr.concat( el.src ) : arr;
{
}, [] );
strState = 'error';
}
self.arrErrors.push({
'strErrMsg':strErrMsg, 'strFileName':strFileName, 'intLine':intLine
});
return true;
}
 
/**
@brief <private> Init stuff that needs to have the page ready
*/
function pageReady()
{
// add container for info and icons
elContainer = document.createElement('div');
var elParent = document.getElementById('p-personal');
if (!elParent)
{
elParent = document.body;
//elContainer.style.cssText = 'position:absolute; right:0; top:0; z-index:10000; background-color:white;';
}
else
{
//elContainer.style.cssText = 'position:relative; z-index:10000; background-color:white;';
}
elContainer.setAttribute('id', 'wikidebug-container');
elParent.appendChild(elContainer);
 
// add num of bugs
elNumBugs = document.createElement('span');
if (self.arrErrors.length)
{
elNumBugs.innerHTML = self.arrErrors.length;
}
elContainer.appendChild(elNumBugs);
 
// add bugs icon
elIcon = document.createElement('img');
elIcon.src = oStateIcons[strState];
elIcon.setAttribute('alt', 'bug:'+strState);
elIcon.onclick = function() { self.showInfo(); };
elContainer.appendChild(elIcon);
}
 
/**
@brief <private> Get browser details
*/
function getBrowser()
{
return navigator.userAgent;
}
 
/**
@brief <private> Get loaded scripts
*/
function getScripts()
{
var arrScripts = [];
//var reShortenScriptUrl = /^http:\/\/([^\/]+).+?[?&]title=([^?&]+).+/;
jQuery('script').each(function()
{
var url = this.src;
/*
if (url.search(reShortenScriptUrl)>=0)
{
url = url.replace(reShortenScriptUrl, '$1/$2');
}
*/
if (url && url.length) // external script
{
arrScripts.push(url);
}
});
return arrScripts;
}
 
/**
@brief Init debugging
*/
this.init = function ()
{
if (strState == 'active')
{
window.onerror = errorHandler;
}
jQuery(document).ready(pageReady);
};
 
function showInfo() {
/**
var msg = mw.format(
@brief Show debug info
'=== Browser ===\n$1\n\n=== Scripts ===\n$2\n\n=== Errors ===\n$3',
*/
getBrowser(),
this.showInfo = function()
getScripts().map( function ( script ) {
{
return '* ' + script;
// conv. errors
var } strErrors =).join( '\n'; ),
errors.map( function ( err ) {
for (var i in self.arrErrors)
return mw.format( '* [$1 @$2] $3', err.strFileName, err.intLine, err.strErrMsg );
{
} ).join( '\n' )
strErrors += '\n* ['+self.arrErrors[i].strFileName+' @'+self.arrErrors[i].intLine+'] '
+self.arrErrors[i].strErrMsg
;
}
// show
jsAlert(''
+'<div><textarea rows="10" style="width:100%">'
+'=== Browser ===\n'
+getBrowser()
+'\n\n'
+'=== Scripts ===\n'
+'\n* '+getScripts().join('\n* ')
+'\n\n'
+'=== Errors ===\n'
+strErrors
+'\n\n'
+'</textarea></div>'
);
};
OO.ui.alert( $( '<textarea>' ).attr( 'rows', 15 ).text( msg ), {
size: 'large',
title: 'wikidebug'
} );
}
 
$( function () {
//
$( mw.util.addPortletLink( 'p-personal', '#', 'wikidebug', 'p-wikidebug', 'wikidebug' ) )
// Init debugger object
.append( [
//
$( '<span>' ).text( errors.length || '' ),
var wikidbg = new wikiDbgClass();
$( '<img>' ).attr( 'src', stateIcons[ strState ] )
// this should start debugging
] )
wikidbg.init();
.on( 'click', function ( evt ) {
// EOF
mw.loader.using( 'oojs-ui-core' ).done( showInfo );
} );
} );