| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- /**
- * General Plugin
- * @version 1.0
- * @author dron
- * @create 2013-04-21
- */
- Tracker.setupGeneralPlugin = function(){
- Tracker.Plugins.addOn( "general", function(){
- var template, data, calculate, render, update, eventDelegate,
- i, l, r, q, tmpl, now, window, document, mainBody, mainBodyId;
- tmpl = Tracker.Util.tmpl;
- now = function( time ){
- time = new Date();
- time = [ time.getFullYear(), time.getMonth() + 1, time.getDate() ].join( "-" ) + " " +
- [ time.getHours(), time.getMinutes(), time.getSeconds() ].join( ":" );
- time = time.replace( /\b(\d)\b/g, "0$1" );
- return time;
- };
- Tracker.Plugins.addStyle( [
- "#plugin-general-page .toolbar{ height: 42px; background-color: #fafafa; border-bottom: 1px solid #d5d5d5; }",
- "#plugin-general-page .toolbar li{ display: block; float: left; }",
- "#plugin-general-page .toolbar li.button-like{ padding: 5px 0 0 5px; }",
- "#plugin-general-page .toolbar li.text-like{ padding: 12px 0 0 20px; line-height: 20px; }",
- "#plugin-general-page .toolbar li.first{ padding-left: 30px; }",
- "#plugin-general-page .body{ position: absolute; top: 43px; bottom: 0; left: 0; right: 0; padding: 10px 30px; }",
- "#plugin-general-page .body .table{ margin-bottom: 30px; width: 900px; table-layout:fixed; }",
- "#plugin-general-page .body .table td{ }",
- "#plugin-general-page .body .table td .ellipsisable{ width: 100%; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }",
- "#plugin-general-page .body .unit{ font-style: italic; font-weight: 400; }",
- "#plugin-general-page .body .close-up{ position: relative; top: -4px; }",
- "#plugin-general-page .body .low{ color: #ff814e; }",
- "#plugin-general-page .body .high{ color: #2bb027; }",
- "#plugin-general-page .body .close-up b{ font-family: Constantia, Georgia; font-size: 24px; font-weight: 400; position: absolute; }"
- ].join( "" ) );
- template = {
- page: tmpl( [
- "<ul class='toolbar unstyled clearfix'>",
- "<li class='first button-like'>",
- "<button class='btn' action='update'>刷新</button>",
- "</li>",
- "<li class='text-like muted'>最后更新时间:<%= now() %></li>",
- "</ul>",
- "<div id='<%= mainBodyId %>' class='body scrollable'>",
- "<% if( count > 0 ){ %>",
- "<h5>当前网页共包含 <%= count %> 个脚本资源:<%= embedCount %> 个内嵌,<%= linkCount %> 个外链文件,<%= appendCount %> 个动态加载。</h5>",
- "<table class='table table-bordered table-striped'>",
- "<thead>",
- "<tr>",
- "<th width='*'>使用量 <span class='unit'>(%)</span></th>",
- "<th width='90'>冗余量 <span class='unit'>(%)</span></th>",
- "<th width='120'>使用行数</th>",
- "<th width='90'>总行数</th>",
- "<th width='90'>总大小 <span class='unit'>(k)</span></th>",
- "<th width='120'>总加载耗时 <span class='unit'>(ms)</span></th>",
- "<th width='120'>总运行耗时 <span class='unit'>(ms)</span></th>",
- "</tr>",
- "</thead>",
- "<tbody>",
- "<tr>",
- "<td><span class='close-up <%= usefulRate < 50 ? 'low' : 'high' %>'><b><%= usefulRate %><b></span></td>",
- "<td><%= redundancyRate %></td>",
- "<td><%= usefulLines %></td>",
- "<td><%= totalLines %></td>",
- "<td><%= totalSize %></td>",
- "<td><%= totalLoadConsum %></td>",
- "<td><%= totalRunConsum %></td>",
- "</tr>",
- "</tbody>",
- "</table>",
- "<h5>冗余量最高 top <%= showCodeCount %> 脚本:</h5>",
- "<table class='table table-bordered table-striped table-condensed'>",
- "<thead>",
- "<tr>",
- "<th width='*'>文件名</th>",
- "<th width='70'>类型</th>",
- "<th width='90'>冗余量 <span class='unit'>(%)</span></th>",
- "<th width='40'></th>",
- "</tr>",
- "</thead>",
- "<tbody>",
- "<% for( var i = 0, r; i < redundancyRateTableData.length; i ++ ){ %>",
- "<% r = redundancyRateTableData[ i ]; %>",
- "<tr>",
- "<td><div class='ellipsisable'><%= r.fullUrl || '-' %></div></td>",
- "<td>",
- "<% switch( r.type ){ ",
- "case 'embed':",
- "print( '内嵌' );",
- "break;",
- "case 'link':",
- "print( '文件链接' );",
- "break;",
- "case 'append':",
- "print( '动态插入' );",
- "break;",
- "} %>",
- "</td>",
- "<td><%= Math.round( ( 1 - r.prop( 'usefulRate' ) ) * 1e4 ) / 1e2 %></td>",
- "<td><button class='btn btn-mini' action='show-code' data-code-id='<%= r.id %>'>查看</button></td>",
- "</tr>",
- "<% } %>",
- "</tbody>",
- "</table>",
- "<h5>体积最大 top <%= showCodeCount %> 脚本:</h5>",
- "<table class='table table-bordered table-striped table-condensed'>",
- "<thead>",
- "<tr>",
- "<th width='*'>文件名</th>",
- "<th width='70'>类型</th>",
- "<th width='90'>大小 <span class='unit'>(k)</span></th>",
- "<th width='40'></th>",
- "</tr>",
- "</thead>",
- "<tbody>",
- "<% for( var i = 0, r; i < sizeTableData.length; i ++ ){ %>",
- "<% r = sizeTableData[ i ]; %>",
- "<tr>",
- "<td><div class='ellipsisable'><%= r.fullUrl || '-' %></div></td>",
- "<td>",
- "<% switch( r.type ){ ",
- "case 'embed':",
- "print( '内嵌' );",
- "break;",
- "case 'link':",
- "print( '文件链接' );",
- "break;",
- "case 'append':",
- "print( '动态插入' );",
- "break;",
- "} %>",
- "</td>",
- "<td><%= ( r.size / 1024 ).toFixed( 2 ) %></td>",
- "<td><button class='btn btn-mini' action='show-code' data-code-id='<%= r.id %>'>查看</button></td>",
- "</tr>",
- "<% } %>",
- "</tbody>",
- "</table>",
- "<h5>运时耗时最高 top <%= showCodeCount %> 脚本:</h5>",
- "<table class='table table-bordered table-striped table-condensed'>",
- "<thead>",
- "<tr>",
- "<th width='*'>文件名</th>",
- "<th width='70'>类型</th>",
- "<th width='90'>耗时 <span class='unit'>(ms)</span></th>",
- "<th width='40'></th>",
- "</tr>",
- "</thead>",
- "<tbody>",
- "<% for( var i = 0, r; i < runConsumTableData.length; i ++ ){ %>",
- "<% r = runConsumTableData[ i ]; %>",
- "<tr>",
- "<td><div class='ellipsisable'><%= r.fullUrl || '-' %></div></td>",
- "<td>",
- "<% switch( r.type ){ ",
- "case 'embed':",
- "print( '内嵌' );",
- "break;",
- "case 'link':",
- "print( '文件链接' );",
- "break;",
- "case 'append':",
- "print( '动态插入' );",
- "break;",
- "} %>",
- "</td>",
- "<td><%= r.runConsum %></td>",
- "<td><button class='btn btn-mini' action='show-code' data-code-id='<%= r.id %>'>查看</button></td>",
- "</tr>",
- "<% } %>",
- "</tbody>",
- "</table>",
- "<% } else { %>",
- "该网页没有任何 JS 代码。",
- "<% } %>",
- "</div>"
- ].join( "" ) )
- };
- data = function(){};
- data.prototype = {
- count: 0,
- embedCount: 0,
- linkCount: 0,
- appendCount: 0,
- usefulRate: 0,
- redundancyRate: 100,
- usefulLines: 0,
- totalLines: 0,
- totalSize: 0,
- totalLoadConsum: 0,
- totalRunConsum: 0,
- now: now
- };
- data = new data;
- calculate = function(){
- var redundancyRate = function( code1, code2 ){
- return code1.prop( "usefulRate" ) - code2.prop( "usefulRate" );
- };
- var size = function( code1, code2 ){
- return code2.size - code1.size;
- };
- var runConsum = function( code1, code2 ){
- return code2.runConsum - code1.runConsum;
- };
- return function (){
- for( i in data )
- delete data[ i ];
- l = data.count = Tracker.CodeList.count();
- data.showCodeCount = l > 2 ? 3 : l;
- for( i = 0; i < l; i ++ ){
- r = Tracker.CodeList.get( i );
- if( r.type == "embed" ){
- data.embedCount ++;
- }else if( r.type == "link" ){
- data.linkCount ++;
- }else if( r.type == "append" ){
- data.appendCount ++;
- }
- data.usefulLines += r.arriveRowsCount;
- data.totalLines += r.rowsCount;
- data.totalSize += r.size;
- data.totalLoadConsum += r.loadConsum;
- data.totalRunConsum += r.runConsum;
- r.prop( "usefulRate", r.arriveRowsCount / r.rowsCount );
- }
- r = data.usefulLines / data.totalLines;
- data.usefulRate = Math.round( ( r ) * 1e4 ) / 1e2;
- data.redundancyRate = Math.round( ( 1 - r ) * 1e4 ) / 1e2;
- data.totalSize = ( data.totalSize / 1000 ).toFixed( 2 );
- q = Tracker.CodeList.list().slice( 0 );
- q.sort( redundancyRate );
- data.redundancyRateTableData = q.slice( 0, data.showCodeCount );
- q = Tracker.CodeList.list().slice( 0 );
- q.sort( size );
- data.sizeTableData = q.slice( 0, data.showCodeCount );
- q = Tracker.CodeList.list().slice( 0 );
- q.sort( runConsum );
- data.runConsumTableData = q.slice( 0, data.showCodeCount );
- mainBodyId = data.mainBodyId = Tracker.Util.id();
- }
- }();
- render = Tracker.Util.bind( function( text ){
- this.body.innerHTML = text || template.page( data );
- }, this );
- update = function(){
- calculate();
- render();
- };
- eventDelegate = function( el ){
- Tracker.Event.add( el, "click", function( e ){
- var target, action, codeId;
- target = e.target;
- action = target.getAttribute( "action" );
- codeId = target.getAttribute( "data-code-id" );
- if( action == "show-code" ){
- Tracker.View.ControlPanel.activeTab( "code-list" );
- Tracker.View.ControlPanel.showCodeDetail( codeId );
- }else if( action == "update" ){
- document.getElementById( mainBodyId ).innerHTML = "loading...";
- window.setTimeout( update, 1e2 );
- }
- } );
- };
- this.onStartUp( function( win, doc ){
- window = win;
- document = doc;
- eventDelegate( this.body );
- } );
- this.onActive( update );
- } );
- };
|