mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-07 03:12:33 +01:00
this fixes #101
This commit is contained in:
parent
6cd967e6d7
commit
86870dadb7
181
js/traffic.js
181
js/traffic.js
@ -29,26 +29,92 @@
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
// Intercept root frame requests. This is where we identify and block popups.
|
// Intercept and filter web requests according to white and black lists.
|
||||||
|
|
||||||
var onBeforeRootDocument = function(tabId, details) {
|
var onBeforeRequest = function(details) {
|
||||||
var µb = µBlock;
|
//console.debug('onBeforeRequest()> "%s": %o', details.url, details);
|
||||||
|
|
||||||
// Ignore non-http schemes: I don't think this could ever happened
|
// Do not block behind the scene requests.
|
||||||
// because of filters at addListener() time... Will see.
|
var tabId = details.tabId;
|
||||||
var requestURL = details.url;
|
if ( tabId < 0 ) {
|
||||||
if ( requestURL.slice(0, 4) !== 'http' ) {
|
|
||||||
console.error('onBeforeRootDocument(): Unexpected scheme!');
|
|
||||||
µb.unbindTabFromPageStats(tabId);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var µb = µBlock;
|
||||||
|
var requestURL = details.url;
|
||||||
|
var requestType = details.type;
|
||||||
|
|
||||||
|
// Special handling for root document.
|
||||||
|
if ( requestType === 'main_frame' && details.parentFrameId === -1 ) {
|
||||||
|
µb.bindTabToPageStats(tabId, requestURL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var µburi = µb.URI.set(requestURL);
|
||||||
|
var requestHostname = µburi.hostname;
|
||||||
|
var requestPath = µburi.path;
|
||||||
|
|
||||||
|
// rhill 2013-12-15:
|
||||||
|
// Try to transpose generic `other` category into something more
|
||||||
|
// meaningful.
|
||||||
|
if ( requestType === 'other' ) {
|
||||||
|
requestType = µb.transposeType(requestType, requestPath);
|
||||||
|
}
|
||||||
|
|
||||||
// Lookup the page store associated with this tab id.
|
// Lookup the page store associated with this tab id.
|
||||||
var pageStore = µb.bindTabToPageStats(tabId, requestURL);
|
var pageStore = µb.pageStoreFromTabId(tabId);
|
||||||
if ( !pageStore ) {
|
if ( !pageStore ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var reason = false;
|
||||||
|
if ( µb.getNetFilteringSwitch(pageStore.pageHostname) ) {
|
||||||
|
reason = µb.abpFilters.matchString(pageStore, requestURL, requestType, requestHostname);
|
||||||
|
}
|
||||||
|
// Record what happened.
|
||||||
|
pageStore.recordRequest(requestType, requestURL, reason);
|
||||||
|
|
||||||
|
// Not blocked?
|
||||||
|
if ( reason === false || reason.slice(0, 2) === '@@' ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Blocked
|
||||||
|
//console.debug('µBlock> onBeforeRequest()> BLOCK "%s" because "%s"', details.url, reason);
|
||||||
|
|
||||||
|
// https://github.com/gorhill/uBlock/issues/18
|
||||||
|
// Do not use redirection, we need to block outright to be sure the request
|
||||||
|
// will not be made. There can be no such guarantee with redirection.
|
||||||
|
|
||||||
|
return { 'cancel': true };
|
||||||
|
};
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
// Intercept root frame requests. This is where we identify and block popups.
|
||||||
|
|
||||||
|
var onBeforeSendHeaders = function(details) {
|
||||||
|
// Do not block behind the scene requests.
|
||||||
|
var tabId = details.tabId;
|
||||||
|
if ( tabId < 0 ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only root document.
|
||||||
|
if ( details.parentFrameId !== -1 ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var µb = µBlock;
|
||||||
|
var requestURL = details.url;
|
||||||
|
|
||||||
|
// Lookup the page store associated with this tab id.
|
||||||
|
var pageStore = µb.pageStoreFromTabId(tabId);
|
||||||
|
if ( !pageStore ) {
|
||||||
|
console.error('µBlock> onBeforeSendHeaders(): no page store for "%s"', requestURL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Heuristic to determine whether we are dealing with a popup:
|
// Heuristic to determine whether we are dealing with a popup:
|
||||||
// - the page store is new (it's not a reused one)
|
// - the page store is new (it's not a reused one)
|
||||||
// - the referrer is not nil
|
// - the referrer is not nil
|
||||||
@ -98,73 +164,6 @@ var onBeforeRootDocument = function(tabId, details) {
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
// Intercept and filter web requests according to white and black lists.
|
|
||||||
|
|
||||||
var onBeforeSendHeaders = function(details) {
|
|
||||||
//console.debug('onBeforeRequestHandler()> "%s": %o', details.url, details);
|
|
||||||
|
|
||||||
// Do not block behind the scene requests.
|
|
||||||
var tabId = details.tabId;
|
|
||||||
if ( tabId < 0 ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Special handling for root document.
|
|
||||||
var requestType = details.type;
|
|
||||||
if ( requestType === 'main_frame' && details.parentFrameId === -1 ) {
|
|
||||||
return onBeforeRootDocument(tabId, details);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore non-http schemes: I don't think this could ever happened
|
|
||||||
// because of filters at addListener() time... Will see.
|
|
||||||
var requestURL = details.url;
|
|
||||||
if ( requestURL.slice(0, 4) !== 'http' ) {
|
|
||||||
console.error('onBeforeSendHeaders(): Unexpected scheme!');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var µb = µBlock;
|
|
||||||
var µburi = µb.URI.set(requestURL);
|
|
||||||
var requestHostname = µburi.hostname;
|
|
||||||
var requestPath = µburi.path;
|
|
||||||
|
|
||||||
// rhill 2013-12-15:
|
|
||||||
// Try to transpose generic `other` category into something more
|
|
||||||
// meaningful.
|
|
||||||
if ( requestType === 'other' ) {
|
|
||||||
requestType = µb.transposeType(requestType, requestPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lookup the page store associated with this tab id.
|
|
||||||
var pageStore = µb.pageStoreFromTabId(tabId);
|
|
||||||
if ( !pageStore ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var reason = false;
|
|
||||||
if ( µb.getNetFilteringSwitch(pageStore.pageHostname) ) {
|
|
||||||
reason = µb.abpFilters.matchString(pageStore, requestURL, requestType, requestHostname);
|
|
||||||
}
|
|
||||||
// Record what happened.
|
|
||||||
pageStore.recordRequest(requestType, requestURL, reason);
|
|
||||||
|
|
||||||
// Not blocked?
|
|
||||||
if ( reason === false || reason.slice(0, 2) === '@@' ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Blocked
|
|
||||||
//console.debug('µBlock> onBeforeSendHeaders()> BLOCK "%s" because "%s"', details.url, reason);
|
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/18
|
|
||||||
// Do not use redirection, we need to block outright to be sure the request
|
|
||||||
// will not be made. There can be no such guarantee with redirection.
|
|
||||||
|
|
||||||
return { 'cancel': true };
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
var referrerFromHeaders = function(headers) {
|
var referrerFromHeaders = function(headers) {
|
||||||
var i = headers.length;
|
var i = headers.length;
|
||||||
while ( i-- ) {
|
while ( i-- ) {
|
||||||
@ -177,14 +176,14 @@ var referrerFromHeaders = function(headers) {
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
chrome.webRequest.onBeforeSendHeaders.addListener(
|
chrome.webRequest.onBeforeRequest.addListener(
|
||||||
//function(details) {
|
//function(details) {
|
||||||
// quickProfiler.start('onBeforeSendHeaders');
|
// quickProfiler.start('onBeforeRequest');
|
||||||
// var r = onBeforeSendHeaders(details);
|
// var r = onBeforeRequest(details);
|
||||||
// quickProfiler.stop();
|
// quickProfiler.stop();
|
||||||
// return r;
|
// return r;
|
||||||
//},
|
//},
|
||||||
onBeforeSendHeaders,
|
onBeforeRequest,
|
||||||
{
|
{
|
||||||
"urls": [
|
"urls": [
|
||||||
"http://*/*",
|
"http://*/*",
|
||||||
@ -201,6 +200,26 @@ chrome.webRequest.onBeforeSendHeaders.addListener(
|
|||||||
"other"
|
"other"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
[ "blocking" ]
|
||||||
|
);
|
||||||
|
|
||||||
|
chrome.webRequest.onBeforeSendHeaders.addListener(
|
||||||
|
//function(details) {
|
||||||
|
// quickProfiler.start('onBeforeSendHeaders');
|
||||||
|
// var r = onBeforeSendHeaders(details);
|
||||||
|
// quickProfiler.stop();
|
||||||
|
// return r;
|
||||||
|
//},
|
||||||
|
onBeforeSendHeaders,
|
||||||
|
{
|
||||||
|
"urls": [
|
||||||
|
"http://*/*",
|
||||||
|
"https://*/*",
|
||||||
|
],
|
||||||
|
"types": [
|
||||||
|
"main_frame"
|
||||||
|
]
|
||||||
|
},
|
||||||
[ "blocking", "requestHeaders" ]
|
[ "blocking", "requestHeaders" ]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user