2014-07-02 10:02:29 -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-10-02 14:45:26 -06:00
|
|
|
/* jshint bitwise: false */
|
2014-07-02 10:02:29 -06:00
|
|
|
/* global chrome, uDom, messaging */
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
messaging.start('stats.js');
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-14 14:20:40 -06:00
|
|
|
var logSettingChanged = function() {
|
2014-07-02 10:02:29 -06:00
|
|
|
messaging.tell({
|
|
|
|
what: 'userSettings',
|
2014-09-14 14:20:40 -06:00
|
|
|
name: 'logRequests',
|
2014-07-02 10:02:29 -06:00
|
|
|
value: this.checked
|
|
|
|
});
|
2014-09-14 14:20:40 -06:00
|
|
|
uDom('#requests').toggleClass('logEnabled', this.checked);
|
2014-07-02 10:02:29 -06:00
|
|
|
renderPageSelector();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var cachedPageSelectors = {};
|
|
|
|
var cachedPageHash = '';
|
|
|
|
|
|
|
|
var toPrettyTypeNames = {
|
2014-07-06 17:14:32 -06:00
|
|
|
'stylesheet': 'css',
|
|
|
|
'sub_frame': 'frame',
|
|
|
|
'object': 'plugin',
|
|
|
|
'xmlhttprequest': 'XHR'
|
2014-07-02 10:02:29 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-02 14:45:26 -06:00
|
|
|
var chunkify = function(s) {
|
|
|
|
var chunkSize = 50;
|
|
|
|
var chunks = [];
|
|
|
|
while ( s.length ) {
|
|
|
|
chunks.push(s.slice(0, chunkSize));
|
|
|
|
s = s.slice(chunkSize);
|
|
|
|
}
|
|
|
|
return chunks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-02 14:52:57 -06:00
|
|
|
var renderURL = function(url, filter) {
|
|
|
|
var chunkSize = 50;
|
|
|
|
// make a regex out of the filter
|
|
|
|
var reText = filter;
|
|
|
|
var pos = reText.indexOf('$');
|
|
|
|
if ( pos > 0 ) {
|
|
|
|
reText = reText.slice(0, pos);
|
|
|
|
}
|
2014-07-06 17:14:32 -06:00
|
|
|
if ( reText.slice(0, 2) === '@@' ) {
|
|
|
|
reText = reText.slice(2);
|
|
|
|
}
|
2014-10-11 07:12:49 -06:00
|
|
|
if ( reText === '*' ) {
|
|
|
|
reText = '\\*';
|
|
|
|
} else {
|
|
|
|
reText = reText
|
|
|
|
.replace(/\./g, '\\.')
|
|
|
|
.replace(/\?/g, '\\?')
|
|
|
|
.replace('||', '')
|
|
|
|
.replace(/\^/g, '.')
|
|
|
|
.replace(/\*/g, '.*')
|
|
|
|
;
|
|
|
|
}
|
2014-07-02 16:42:16 -06:00
|
|
|
var re = new RegExp(reText, 'gi');
|
2014-07-02 14:52:57 -06:00
|
|
|
var matches = re.exec(url);
|
2014-10-02 14:45:26 -06:00
|
|
|
var renderedURL = chunkify(url);
|
2014-07-02 14:52:57 -06:00
|
|
|
|
2014-07-02 16:42:16 -06:00
|
|
|
if ( matches && matches[0].length ) {
|
|
|
|
var index = (re.lastIndex / chunkSize) | 0;
|
|
|
|
var offset = re.lastIndex % chunkSize;
|
|
|
|
if ( index > 0 && offset === 0 ) {
|
2014-07-05 08:06:49 -06:00
|
|
|
offset = chunkSize;
|
2014-07-02 16:42:16 -06:00
|
|
|
index -= 1;
|
|
|
|
}
|
|
|
|
var segment = renderedURL[index];
|
|
|
|
renderedURL[index] = segment.slice(0, offset) + '</b>' + segment.slice(offset);
|
|
|
|
|
|
|
|
index = (matches.index / chunkSize) | 0;
|
|
|
|
offset = matches.index % chunkSize;
|
|
|
|
if ( index > 0 && offset === 0 ) {
|
|
|
|
offset = chunkSize;
|
|
|
|
index -= 1;
|
|
|
|
}
|
|
|
|
segment = renderedURL[index];
|
|
|
|
renderedURL[index] = segment.slice(0, offset) + '<b>' + segment.slice(offset);
|
2014-07-02 14:52:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return renderedURL.join('\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-02 10:02:29 -06:00
|
|
|
var renderPageDetails = function(tabId) {
|
|
|
|
if ( !cachedPageSelectors[tabId] ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var onDataReceived = function(details) {
|
|
|
|
if ( details.hash === cachedPageHash ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-06 17:14:32 -06:00
|
|
|
cachedPageHash = details.hash;
|
|
|
|
var renderRequests = function(requests, className) {
|
|
|
|
requests.sort(function(a, b) {
|
|
|
|
var r = a.domain.localeCompare(b.domain);
|
|
|
|
if ( r ) { return r; }
|
2014-07-02 10:02:29 -06:00
|
|
|
r = a.reason.localeCompare(b.reason);
|
2014-07-06 17:14:32 -06:00
|
|
|
if ( r ) { return r; }
|
|
|
|
r = a.type.localeCompare(b.type);
|
|
|
|
if ( r ) { return r; }
|
|
|
|
return a.url.localeCompare(b.url);
|
|
|
|
});
|
|
|
|
var html = [], request;
|
2014-07-02 10:02:29 -06:00
|
|
|
html.push(
|
2014-07-06 17:14:32 -06:00
|
|
|
'<tr class="header ', className, '">',
|
|
|
|
'<td colspan="4"><h3>',
|
|
|
|
chrome.i18n.getMessage(className + (requests.length ? 'RequestsHeader' : 'RequestsEmpty')),
|
|
|
|
'</h3>'
|
2014-07-02 10:02:29 -06:00
|
|
|
);
|
2014-07-07 11:02:03 -06:00
|
|
|
var currentDomain = '';
|
2014-07-06 17:14:32 -06:00
|
|
|
for ( var i = 0; i < requests.length; i++ ) {
|
|
|
|
request = requests[i];
|
2014-07-07 11:02:03 -06:00
|
|
|
if ( request.domain !== currentDomain ) {
|
|
|
|
currentDomain = request.domain;
|
|
|
|
html.push(
|
|
|
|
'<tr class="', className, ' domainHeader">',
|
|
|
|
'<td colspan="4">', currentDomain
|
|
|
|
);
|
|
|
|
}
|
2014-07-06 17:14:32 -06:00
|
|
|
html.push(
|
2014-10-02 14:45:26 -06:00
|
|
|
'<tr class="', className, request.flags & 0x01 ? ' logMirrored': '', ' requestEntry">',
|
2014-07-07 11:02:03 -06:00
|
|
|
'<td>',
|
2014-07-06 17:14:32 -06:00
|
|
|
'<td>', toPrettyTypeNames[request.type] || request.type,
|
|
|
|
'<td>', renderURL(request.url, request.reason),
|
2014-10-02 14:45:26 -06:00
|
|
|
'<td>', chunkify(request.reason).join('\n')
|
2014-07-06 17:14:32 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return html;
|
|
|
|
};
|
|
|
|
uDom('#requests .tableHeader ~ tr').remove();
|
|
|
|
var htmlBlocked = renderRequests(details.blockedRequests || [], 'logBlocked');
|
|
|
|
var htmlAllowed = renderRequests(details.allowedRequests || [], 'logAllowed');
|
|
|
|
uDom('#requests .tableHeader').insertAfter(htmlBlocked.concat(htmlAllowed).join(''));
|
2014-07-02 10:02:29 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
messaging.ask({ what: 'getPageDetails', tabId: tabId }, onDataReceived);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var pageSelectorChanged = function() {
|
|
|
|
renderPageDetails(this.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var renderPageSelector = function(targetTabId) {
|
2014-09-14 14:20:40 -06:00
|
|
|
if ( !uDom('#logRequests').prop('checked') ) {
|
2014-07-02 10:02:29 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var selectedTabId = targetTabId || parseInt(uDom('#pageSelector').val(), 10);
|
|
|
|
var onTabReceived = function(tab) {
|
2014-07-06 17:14:32 -06:00
|
|
|
if ( !tab ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-02 10:02:29 -06:00
|
|
|
var html = [
|
|
|
|
'<option value="',
|
|
|
|
tab.id,
|
|
|
|
'">',
|
|
|
|
tab.title
|
|
|
|
];
|
|
|
|
uDom('#pageSelector').append(html.join(''));
|
|
|
|
if ( tab.id === selectedTabId ) {
|
|
|
|
uDom('#pageSelector').val(tab.id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var onDataReceived = function(pageSelectors) {
|
2014-07-07 11:02:03 -06:00
|
|
|
uDom('#requests').toggleClass('empty', pageSelectors.length === 0);
|
2014-07-02 10:02:29 -06:00
|
|
|
uDom('#pageSelector option').remove();
|
|
|
|
cachedPageSelectors = {};
|
|
|
|
pageSelectors.sort().map(function(tabId) {
|
|
|
|
cachedPageSelectors[tabId] = true;
|
|
|
|
});
|
|
|
|
if ( !cachedPageSelectors[selectedTabId] ) {
|
|
|
|
selectedTabId = pageSelectors[0];
|
|
|
|
}
|
|
|
|
for ( var i = 0; i < pageSelectors.length; i++ ) {
|
|
|
|
chrome.tabs.get(parseInt(pageSelectors[i], 10), onTabReceived);
|
|
|
|
}
|
2014-07-06 19:52:16 -06:00
|
|
|
if ( pageSelectors.length > 0 ) {
|
2014-07-02 10:02:29 -06:00
|
|
|
renderPageDetails(selectedTabId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
messaging.ask({ what: 'getPageSelectors' }, onDataReceived);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var onUserSettingsReceived = function(details) {
|
2014-09-14 14:20:40 -06:00
|
|
|
uDom('#logRequests').prop('checked', details.logRequests);
|
|
|
|
uDom('#requests').toggleClass('logEnabled', details.logRequests);
|
2014-07-02 10:02:29 -06:00
|
|
|
|
|
|
|
var matches = window.location.search.slice(1).match(/(?:^|&)which=(\d+)/);
|
|
|
|
var tabId = matches && matches.length === 2 ? parseInt(matches[1], 10) : 0;
|
|
|
|
renderPageSelector(tabId);
|
|
|
|
|
2014-09-14 14:20:40 -06:00
|
|
|
uDom('#logRequests').on('change', logSettingChanged);
|
2014-07-02 10:02:29 -06:00
|
|
|
uDom('#refresh').on('click', function() { renderPageSelector(); });
|
|
|
|
uDom('#pageSelector').on('change', pageSelectorChanged);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
uDom.onLoad(function() {
|
|
|
|
messaging.ask({ what: 'userSettings' }, onUserSettingsReceived);
|
|
|
|
});
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|