1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-18 08:52:26 +02:00
uBlock/js/popup.js

192 lines
5.4 KiB
JavaScript
Raw Normal View History

2014-06-24 00:42:43 +02:00
/*******************************************************************************
µBlock - a Chromium browser extension to block requests.
Copyright (C) 2014 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/
/******************************************************************************/
/******************************************************************************/
(function() {
/******************************************************************************/
var stats;
/******************************************************************************/
// https://github.com/gorhill/httpswitchboard/issues/345
messaging.start('popup.js');
/******************************************************************************/
2014-06-24 07:20:59 +02:00
formatNumber = function(count) {
if ( typeof count !== 'number' ) {
return '';
}
var s = count.toFixed(0);
if ( count >= 1000 ) {
if ( count < 10000 ) {
s = s.slice(0,1) + '.' + s.slice(1,3) + 'K';
} else if ( count < 100000 ) {
s = s.slice(0,2) + '.' + s.slice(2,3) + 'K';
} else if ( count < 1000000 ) {
s = s.slice(0,3) + 'K';
} else if ( count < 10000000 ) {
s = s.slice(0,1) + '.' + s.slice(1,3) + 'M';
} else if ( count < 100000000 ) {
s = s.slice(0,2) + '.' + s.slice(2,3) + 'M';
} else if ( count < 1000000000 ) {
s = s.slice(0,3) + 'M';
} else {
s = s.slice(0,-9) + 'G';
}
}
return s;
};
/******************************************************************************/
2014-06-24 00:42:43 +02:00
var renderStats = function() {
2014-07-02 18:02:29 +02:00
if ( !stats ) {
2014-06-24 00:42:43 +02:00
return;
}
2014-07-02 18:02:29 +02:00
uDom('#gotoLog').toggleClass('enabled', stats.logBlockedRequests);
2014-06-24 00:42:43 +02:00
var blocked = stats.pageBlockedRequestCount;
var total = stats.pageAllowedRequestCount + blocked;
2014-07-02 18:02:29 +02:00
var html = [];
2014-06-24 00:42:43 +02:00
if ( total === 0 ) {
2014-07-02 18:02:29 +02:00
html.push('0');
2014-06-24 00:42:43 +02:00
} else {
2014-07-02 18:02:29 +02:00
html.push(
2014-06-24 07:20:59 +02:00
formatNumber(blocked),
'<span class="dim">&nbsp;or&nbsp;',
2014-06-24 00:42:43 +02:00
(blocked * 100 / total).toFixed(0),
2014-06-24 07:20:59 +02:00
'%</span>'
2014-07-02 18:02:29 +02:00
);
2014-06-24 00:42:43 +02:00
}
2014-07-02 18:02:29 +02:00
uDom('#page-blocked').html(html.join(''));
2014-06-24 00:42:43 +02:00
blocked = stats.globalBlockedRequestCount;
total = stats.globalAllowedRequestCount + blocked;
2014-07-02 18:02:29 +02:00
html = [];
2014-06-24 00:42:43 +02:00
if ( total === 0 ) {
2014-07-02 18:02:29 +02:00
html.push('0');
2014-06-24 00:42:43 +02:00
} else {
2014-07-02 18:02:29 +02:00
html.push(
2014-06-24 07:20:59 +02:00
formatNumber(blocked),
'<span class="dim">&nbsp;or&nbsp;',
2014-06-24 00:42:43 +02:00
(blocked * 100 / total).toFixed(0),
2014-06-24 07:20:59 +02:00
'%</span>'
2014-07-02 18:02:29 +02:00
);
2014-06-24 00:42:43 +02:00
}
2014-07-02 18:02:29 +02:00
uDom('#total-blocked').html(html.join(''));
2014-06-24 00:42:43 +02:00
2014-07-02 18:02:29 +02:00
uDom('#switch .fa').toggleClass(
2014-06-24 00:42:43 +02:00
'off',
stats.pageURL === '' || !stats.netFilteringSwitch
);
};
/******************************************************************************/
var onStatsReceived = function(details) {
stats = details;
renderStats();
};
/******************************************************************************/
var onTabsReceived = function(tabs) {
if ( tabs.length === 0 ) {
return;
}
var q = {
what: 'stats',
tabId: tabs[0].id
};
messaging.ask( q, onStatsReceived );
};
chrome.tabs.query({ active: true }, onTabsReceived);
/******************************************************************************/
var handleNetFilteringSwitch = function() {
if ( !stats || !stats.pageURL ) {
return;
}
2014-07-02 18:02:29 +02:00
var off = uDom(this).toggleClass('off').hasClassName('off');
2014-06-24 00:42:43 +02:00
messaging.tell({
what: 'toggleNetFiltering',
hostname: stats.pageHostname,
2014-07-02 18:02:29 +02:00
state: !off,
2014-06-24 00:42:43 +02:00
tabId: stats.tabId
});
};
/******************************************************************************/
2014-07-02 18:02:29 +02:00
var gotoDashboard = function() {
messaging.tell({
what: 'gotoExtensionURL',
url: 'dashboard.html'
});
2014-06-24 00:42:43 +02:00
};
2014-07-02 18:02:29 +02:00
/******************************************************************************/
var gotoStats = function() {
messaging.tell({
what: 'gotoExtensionURL',
url: 'dashboard.html?tab=stats&which=' + stats.tabId
});
};
/******************************************************************************/
var renderHeader = function() {
var hdr = uDom('#version');
hdr.html(hdr.html() + 'v' + chrome.runtime.getManifest().version);
};
2014-06-24 00:42:43 +02:00
/******************************************************************************/
var installEventHandlers = function() {
2014-07-02 18:02:29 +02:00
uDom('h1,h2,h3,h4').on('click', gotoDashboard);
uDom('#switch .fa').on('click', handleNetFilteringSwitch);
uDom('#gotoLog').on('click', gotoStats);
2014-06-24 00:42:43 +02:00
};
/******************************************************************************/
// Make menu only when popup html is fully loaded
2014-07-02 18:02:29 +02:00
uDom.onLoad(function() {
2014-06-24 00:42:43 +02:00
renderHeader();
renderStats();
installEventHandlers();
});
/******************************************************************************/
})();