X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;ds=sidebyside;f=resources%2Fmediawiki%2Fmediawiki.inspect.js;h=346e783aff952316b0ec81c4875d43cfe7370863;hb=c18fe7511db2cdb2cd8bdd83ae86ac6c6d389164;hp=031773718821ac9228dc662251b77c42fbad7409;hpb=55d65ad807d817ecb022be7d5eb01b2146f94740;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/mediawiki/mediawiki.inspect.js b/resources/mediawiki/mediawiki.inspect.js index 0317737188..e76141d3a1 100644 --- a/resources/mediawiki/mediawiki.inspect.js +++ b/resources/mediawiki/mediawiki.inspect.js @@ -14,6 +14,13 @@ } ); } + function humanSize( bytes ) { + if ( !$.isNumeric( bytes ) || bytes === 0 ) { return bytes; } + var i = 0, units = [ '', ' kB', ' MB', ' GB', ' TB', ' PB' ]; + for ( ; bytes >= 1024; bytes /= 1024 ) { i++; } + return bytes.toFixed( 1 ) + units[i]; + } + /** * @class mw.inspect * @singleton @@ -88,11 +95,15 @@ */ auditSelectors: function ( css ) { var selectors = { total: 0, matched: 0 }, - style = document.createElement( 'style' ); + style = document.createElement( 'style' ), + sheet, rules; style.textContent = css; document.body.appendChild( style ); - $.each( style.sheet.cssRules, function ( index, rule ) { + // Standards-compliant browsers use .sheet.cssRules, IE8 uses .styleSheet.rules… + sheet = style.sheet || style.styleSheet; + rules = sheet.cssRules || sheet.rules; + $.each( rules, function ( index, rule ) { selectors.total++; if ( document.querySelector( rule.selectorText ) !== null ) { selectors.matched++; @@ -124,13 +135,16 @@ try { // Bartosz made me put this here. if ( window.opera ) { throw window.opera; } - console.table( data ); + // Use Function.prototype#call to force an exception on Firefox, + // which doesn't define console#table but doesn't complain if you + // try to invoke it. + console.table.call( console, data ); return; - } catch (e) {} + } catch ( e ) {} try { - console.log( JSON.stringify( data, null, 2 ) ); + console.log( $.toJSON( data, null, 2 ) ); return; - } catch (e) {} + } catch ( e ) {} mw.log( data ); }, @@ -174,9 +188,7 @@ // Convert size to human-readable string. $.each( modules, function ( i, module ) { - module.size = module.size > 1024 ? - ( module.size / 1024 ).toFixed( 2 ) + ' KB' : - ( module.size !== null ? module.size + ' B' : null ); + module.size = humanSize( module.size ); } ); return modules; @@ -194,7 +206,7 @@ try { css = module.style.css.join(); - } catch (e) { return; } // skip + } catch ( e ) { return; } // skip stats = inspect.auditSelectors( css ); modules.push( { @@ -207,6 +219,23 @@ } ); sortByProperty( modules, 'allSelectors', true ); return modules; + }, + + /** + * Report stats on mw.loader.store: the number of localStorage + * cache hits and misses, the number of items purged from the + * cache, and the total size of the module blob in localStorage. + */ + store: function () { + var raw, stats = { enabled: mw.loader.store.enabled }; + if ( stats.enabled ) { + $.extend( stats, mw.loader.store.stats ); + try { + raw = localStorage.getItem( mw.loader.store.getStoreKey() ); + stats.totalSize = humanSize( $.byteLength( raw ) ); + } catch ( e ) {} + } + return [stats]; } } };