uBlock/js/popup.js

289 lines
8.7 KiB
JavaScript
Raw Normal View History

2014-06-23 16:42:43 -06: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
*/
2014-07-06 19:52:16 -06:00
/* global chrome, messaging, uDom */
2014-06-23 16:42:43 -06:00
/******************************************************************************/
/******************************************************************************/
(function() {
/******************************************************************************/
var stats;
2014-10-06 12:02:44 -06:00
var reResultParser = /^(@@)?(\*|\|\|([^$^]+)\^)\$(.+)$/;
2014-06-23 16:42:43 -06:00
/******************************************************************************/
// https://github.com/gorhill/httpswitchboard/issues/345
messaging.start('popup.js');
/******************************************************************************/
2014-07-06 19:52:16 -06:00
var formatNumber = function(count) {
2014-06-23 23:20:59 -06:00
if ( typeof count !== 'number' ) {
return '';
}
return count.toLocaleString();
2014-06-23 23:20:59 -06:00
};
/******************************************************************************/
2014-10-06 12:02:44 -06:00
var syncDynamicFilter = function(scope, i, result) {
var el = uDom('[data-scope="' + scope + '"] > div:nth-of-type(' + i + ')');
var matches = reResultParser.exec(result) || [];
var blocked = matches.length !== 0 && matches[1] !== '@@';
el.toggleClass('blocked', blocked);
2014-10-27 10:14:57 -06:00
// https://github.com/gorhill/uBlock/issues/340
// Use dark shade visual cue if the filter is specific to the page hostname
// or one of the ancestor hostname.
var ownFilter = false;
// There might be no page hostname on pages where uBlock can't be active,
// like on browser's built-in pages, etc.
if ( stats.pageHostname ) {
var filterHostname = matches[3] || '*';
if ( stats.pageHostname.slice(0 - filterHostname.length) === filterHostname ) {
ownFilter = (stats.pageHostname.length === filterHostname.length) ||
(stats.pageHostname.substr(0 - filterHostname.length - 1, 1) === '.');
}
2014-10-27 10:14:57 -06:00
}
2014-10-06 12:02:44 -06:00
el.toggleClass('ownFilter', ownFilter);
};
/******************************************************************************/
var syncAllDynamicFilters = function() {
var scopes = ['.', '/'];
var scope, results, i;
while ( scope = scopes.pop() ) {
if ( stats.dynamicFilterResults.hasOwnProperty(scope) === false ) {
continue;
}
results = stats.dynamicFilterResults[scope];
i = 5;
while ( i-- ) {
syncDynamicFilter(scope, i + 1, results[i]);
}
}
};
/******************************************************************************/
2014-06-23 16:42:43 -06:00
var renderStats = function() {
2014-07-02 10:02:29 -06:00
if ( !stats ) {
2014-06-23 16:42:43 -06:00
return;
}
2014-07-02 10:02:29 -06:00
2014-07-25 13:09:59 -06:00
var isHTTP = /^https?:\/\/[0-9a-z]/.test(stats.pageURL);
2014-07-13 13:44:35 -06:00
// Conditions for request log:
// - `http` or `https` scheme
// - logging of requests enabled
2014-07-02 14:52:57 -06:00
uDom('#gotoLog').toggleClass(
'enabled',
2014-09-14 14:20:40 -06:00
isHTTP && stats.logRequests
2014-07-02 14:52:57 -06:00
);
2014-07-13 13:44:35 -06:00
// Conditions for element picker:
// - `http` or `https` scheme
2014-07-12 18:32:44 -06:00
uDom('#gotoPick').toggleClass(
'enabled',
2014-07-13 13:44:35 -06:00
isHTTP
2014-07-12 18:32:44 -06:00
);
2014-07-02 10:02:29 -06:00
2014-07-18 08:57:11 -06:00
var or = chrome.i18n.getMessage('popupOr');
2014-06-23 16:42:43 -06:00
var blocked = stats.pageBlockedRequestCount;
var total = stats.pageAllowedRequestCount + blocked;
2014-07-02 10:02:29 -06:00
var html = [];
2014-06-23 16:42:43 -06:00
if ( total === 0 ) {
2014-07-02 10:02:29 -06:00
html.push('0');
2014-06-23 16:42:43 -06:00
} else {
2014-07-02 10:02:29 -06:00
html.push(
2014-06-23 23:20:59 -06:00
formatNumber(blocked),
2014-07-18 08:57:11 -06:00
'<span class="dim">&nbsp;',
or,
'&nbsp;',
formatNumber(Math.floor(blocked * 100 / total)),
2014-06-23 23:20:59 -06:00
'%</span>'
2014-07-02 10:02:29 -06:00
);
2014-06-23 16:42:43 -06:00
}
2014-07-02 10:02:29 -06:00
uDom('#page-blocked').html(html.join(''));
2014-06-23 16:42:43 -06:00
blocked = stats.globalBlockedRequestCount;
total = stats.globalAllowedRequestCount + blocked;
2014-07-02 10:02:29 -06:00
html = [];
2014-06-23 16:42:43 -06:00
if ( total === 0 ) {
2014-07-02 10:02:29 -06:00
html.push('0');
2014-06-23 16:42:43 -06:00
} else {
2014-07-02 10:02:29 -06:00
html.push(
2014-06-23 23:20:59 -06:00
formatNumber(blocked),
2014-07-18 08:57:11 -06:00
'<span class="dim">&nbsp;',
or,
'&nbsp;',
formatNumber(Math.floor(blocked * 100 / total)),
2014-06-23 23:20:59 -06:00
'%</span>'
2014-07-02 10:02:29 -06:00
);
2014-06-23 16:42:43 -06:00
}
2014-10-06 12:02:44 -06:00
syncAllDynamicFilters();
uDom('#total-blocked').html(html.join(''));
uDom('#switch .fa').toggleClass('off', stats.pageURL === '' || !stats.netFilteringSwitch);
uDom('#dynamicFilteringToggler').toggleClass('on', stats.dynamicFilteringEnabled);
2014-06-23 16:42:43 -06:00
};
/******************************************************************************/
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 );
};
2014-07-16 15:20:11 -06:00
chrome.tabs.query({ active: true, currentWindow: true }, onTabsReceived);
2014-06-23 16:42:43 -06:00
/******************************************************************************/
2014-08-02 09:40:27 -06:00
var toggleNetFilteringSwitch = function(ev) {
2014-06-23 16:42:43 -06:00
if ( !stats || !stats.pageURL ) {
return;
}
2014-07-02 10:02:29 -06:00
var off = uDom(this).toggleClass('off').hasClassName('off');
2014-06-23 16:42:43 -06:00
messaging.tell({
what: 'toggleNetFiltering',
2014-08-02 09:40:27 -06:00
url: stats.pageURL,
scope: ev.ctrlKey || ev.metaKey ? 'page' : '',
2014-07-02 10:02:29 -06:00
state: !off,
2014-06-23 16:42:43 -06:00
tabId: stats.tabId
});
};
/******************************************************************************/
2014-07-02 10:02:29 -06:00
var gotoDashboard = function() {
messaging.tell({
what: 'gotoExtensionURL',
url: 'dashboard.html'
});
2014-06-23 16:42:43 -06:00
};
2014-07-02 10:02:29 -06:00
/******************************************************************************/
var gotoStats = function() {
messaging.tell({
what: 'gotoExtensionURL',
url: 'dashboard.html?tab=stats&which=' + stats.tabId
});
};
/******************************************************************************/
2014-07-12 18:32:44 -06:00
var gotoPick = function() {
messaging.tell({
what: 'gotoPick',
tabId: stats.tabId
});
window.open('','_self').close();
};
/******************************************************************************/
2014-07-02 10:02:29 -06:00
var renderHeader = function() {
var hdr = uDom('#version');
hdr.html(hdr.html() + 'v' + chrome.runtime.getManifest().version);
};
2014-06-23 16:42:43 -06:00
/******************************************************************************/
2014-10-06 12:02:44 -06:00
var onDynamicFilterClicked = function(ev) {
var elScope = uDom(ev.currentTarget);
var scope = elScope.attr('data-scope') === '/' ? '*' : stats.pageHostname;
var elFilter = uDom(ev.target);
var onDynamicFilterChanged = function(details) {
stats.dynamicFilterResults = details;
syncAllDynamicFilters();
};
messaging.ask({
what: 'toggleDynamicFilter',
hostname: scope,
requestType: elFilter.attr('data-type'),
firstParty: elFilter.attr('data-first-party') !== null,
block: elFilter.hasClassName('blocked') === false,
pageHostname: stats.pageHostname
}, onDynamicFilterChanged);
};
/******************************************************************************/
2014-10-07 08:46:10 -06:00
var toggleDynamicFiltering = function(ev) {
// Discard events destined to child elements.
if ( ev !== undefined && ev.target !== this ) {
return;
}
2014-10-06 12:02:44 -06:00
var el = uDom('#dynamicFilteringToggler');
el.toggleClass('on');
messaging.tell({
what: 'userSettings',
name: 'dynamicFilteringEnabled',
value: el.hasClassName('on')
});
};
/******************************************************************************/
2014-06-23 16:42:43 -06:00
var installEventHandlers = function() {
2014-07-02 10:02:29 -06:00
uDom('h1,h2,h3,h4').on('click', gotoDashboard);
2014-08-02 09:40:27 -06:00
uDom('#switch .fa').on('click', toggleNetFilteringSwitch);
2014-07-02 10:02:29 -06:00
uDom('#gotoLog').on('click', gotoStats);
2014-07-12 18:32:44 -06:00
uDom('#gotoPick').on('click', gotoPick);
2014-10-06 12:02:44 -06:00
uDom('#dynamicFilteringToggler').on('click', toggleDynamicFiltering);
uDom('.dynamicFiltering').on('click', 'div', onDynamicFilterClicked);
2014-06-23 16:42:43 -06:00
};
/******************************************************************************/
// Make menu only when popup html is fully loaded
2014-07-02 10:02:29 -06:00
uDom.onLoad(function() {
2014-06-23 16:42:43 -06:00
renderHeader();
renderStats();
installEventHandlers();
});
/******************************************************************************/
})();