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
|
|
|
|
*/
|
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
/* global vAPI, uDom */
|
2014-10-19 13:11:27 +02:00
|
|
|
'use strict';
|
2014-07-07 03:52:16 +02:00
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var stats;
|
2014-10-06 20:02:44 +02:00
|
|
|
var reResultParser = /^(@@)?(\*|\|\|([^$^]+)\^)\$(.+)$/;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// https://github.com/gorhill/httpswitchboard/issues/345
|
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
var messager = vAPI.messaging.channel('popup.js');
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-07 03:52:16 +02:00
|
|
|
var formatNumber = function(count) {
|
2014-06-24 07:20:59 +02:00
|
|
|
if ( typeof count !== 'number' ) {
|
|
|
|
return '';
|
|
|
|
}
|
2014-07-07 21:59:26 +02:00
|
|
|
return count.toLocaleString();
|
2014-06-24 07:20:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-06 20:02:44 +02: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 17:14:57 +01: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;
|
2014-10-28 07:02:51 +01:00
|
|
|
// 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 17:14:57 +01:00
|
|
|
}
|
2014-10-06 20:02:44 +02: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-10-17 21:44:19 +02:00
|
|
|
var renderStats = function(details) {
|
|
|
|
if ( details ) {
|
|
|
|
stats = details;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-07-25 21:09:59 +02:00
|
|
|
var isHTTP = /^https?:\/\/[0-9a-z]/.test(stats.pageURL);
|
2014-07-13 21:44:35 +02:00
|
|
|
|
|
|
|
// Conditions for request log:
|
|
|
|
// - `http` or `https` scheme
|
|
|
|
// - logging of requests enabled
|
2014-07-02 22:52:57 +02:00
|
|
|
uDom('#gotoLog').toggleClass(
|
|
|
|
'enabled',
|
2014-09-14 22:20:40 +02:00
|
|
|
isHTTP && stats.logRequests
|
2014-07-02 22:52:57 +02:00
|
|
|
);
|
2014-07-13 21:44:35 +02:00
|
|
|
|
|
|
|
// Conditions for element picker:
|
|
|
|
// - `http` or `https` scheme
|
2014-07-13 02:32:44 +02:00
|
|
|
uDom('#gotoPick').toggleClass(
|
|
|
|
'enabled',
|
2014-07-13 21:44:35 +02:00
|
|
|
isHTTP
|
2014-07-13 02:32:44 +02:00
|
|
|
);
|
2014-07-02 18:02:29 +02:00
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
var or = vAPI.i18n('popupOr');
|
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),
|
2014-07-18 16:57:11 +02:00
|
|
|
'<span class="dim"> ',
|
|
|
|
or,
|
|
|
|
' ',
|
2014-09-05 21:59:47 +02:00
|
|
|
formatNumber(Math.floor(blocked * 100 / total)),
|
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),
|
2014-07-18 16:57:11 +02:00
|
|
|
'<span class="dim"> ',
|
|
|
|
or,
|
|
|
|
' ',
|
2014-09-05 21:59:47 +02:00
|
|
|
formatNumber(Math.floor(blocked * 100 / total)),
|
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-10-06 20:02:44 +02: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-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
messager.send( {
|
|
|
|
what: 'activeTabStats'
|
|
|
|
}, renderStats );
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-02 17:40:27 +02:00
|
|
|
var toggleNetFilteringSwitch = function(ev) {
|
2014-06-24 00:42:43 +02:00
|
|
|
if ( !stats || !stats.pageURL ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-02 18:02:29 +02:00
|
|
|
var off = uDom(this).toggleClass('off').hasClassName('off');
|
2014-10-17 21:44:19 +02:00
|
|
|
messager.send({
|
2014-06-24 00:42:43 +02:00
|
|
|
what: 'toggleNetFiltering',
|
2014-08-02 17:40:27 +02:00
|
|
|
url: stats.pageURL,
|
|
|
|
scope: ev.ctrlKey || ev.metaKey ? 'page' : '',
|
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() {
|
2014-10-17 21:44:19 +02:00
|
|
|
messager.send({
|
|
|
|
what: 'gotoURL',
|
|
|
|
details: {
|
|
|
|
url: 'dashboard.html',
|
|
|
|
select: true,
|
|
|
|
index: -1
|
|
|
|
}
|
2014-07-02 18:02:29 +02:00
|
|
|
});
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
2014-07-02 18:02:29 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var gotoStats = function() {
|
2014-10-17 21:44:19 +02:00
|
|
|
messager.send({
|
|
|
|
what: 'gotoURL',
|
|
|
|
details: {
|
|
|
|
url: 'dashboard.html?tab=stats&which=' + stats.tabId,
|
|
|
|
select: true,
|
|
|
|
index: -1
|
|
|
|
}
|
2014-07-02 18:02:29 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-13 02:32:44 +02:00
|
|
|
var gotoPick = function() {
|
2014-10-17 21:44:19 +02:00
|
|
|
messager.send({
|
2014-07-13 02:32:44 +02:00
|
|
|
what: 'gotoPick',
|
|
|
|
tabId: stats.tabId
|
|
|
|
});
|
|
|
|
window.open('','_self').close();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-02 18:02:29 +02:00
|
|
|
var renderHeader = function() {
|
|
|
|
var hdr = uDom('#version');
|
2014-10-17 21:44:19 +02:00
|
|
|
hdr.html(hdr.html() + 'v'); // + chrome.runtime.getManifest().version);
|
2014-07-02 18:02:29 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-06 20:02:44 +02: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();
|
|
|
|
};
|
2014-10-17 21:44:19 +02:00
|
|
|
messager.send({
|
2014-10-06 20:02:44 +02:00
|
|
|
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 16:46:10 +02:00
|
|
|
var toggleDynamicFiltering = function(ev) {
|
|
|
|
// Discard events destined to child elements.
|
|
|
|
if ( ev !== undefined && ev.target !== this ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-06 20:02:44 +02:00
|
|
|
var el = uDom('#dynamicFilteringToggler');
|
|
|
|
el.toggleClass('on');
|
2014-10-17 21:44:19 +02:00
|
|
|
messager.send({
|
2014-10-06 20:02:44 +02:00
|
|
|
what: 'userSettings',
|
|
|
|
name: 'dynamicFilteringEnabled',
|
|
|
|
value: el.hasClassName('on')
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
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);
|
2014-08-02 17:40:27 +02:00
|
|
|
uDom('#switch .fa').on('click', toggleNetFilteringSwitch);
|
2014-07-02 18:02:29 +02:00
|
|
|
uDom('#gotoLog').on('click', gotoStats);
|
2014-07-13 02:32:44 +02:00
|
|
|
uDom('#gotoPick').on('click', gotoPick);
|
2014-10-06 20:02:44 +02:00
|
|
|
uDom('#dynamicFilteringToggler').on('click', toggleDynamicFiltering);
|
|
|
|
uDom('.dynamicFiltering').on('click', 'div', onDynamicFilterClicked);
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|