mirror of https://github.com/gorhill/uBlock.git
code review
This commit is contained in:
parent
93d26f6159
commit
fb3f17147b
|
@ -46,37 +46,24 @@ var onBeforeRequest = function(details) {
|
||||||
//console.debug('µBlock.webRequest/onBeforeRequest(): "%s": %o', details.url, details);
|
//console.debug('µBlock.webRequest/onBeforeRequest(): "%s": %o', details.url, details);
|
||||||
//console.debug('µBlock.webRequest/onBeforeRequest(): "type=%s, id=%d, parent id=%d, url=%s', details.type, details.frameId, details.parentFrameId, details.url);
|
//console.debug('µBlock.webRequest/onBeforeRequest(): "type=%s, id=%d, parent id=%d, url=%s', details.type, details.frameId, details.parentFrameId, details.url);
|
||||||
|
|
||||||
var µb = µBlock;
|
|
||||||
var tabId = details.tabId;
|
|
||||||
var requestType = details.type;
|
|
||||||
var requestURL = details.url;
|
|
||||||
|
|
||||||
// Special handling for root document.
|
// Special handling for root document.
|
||||||
// https://github.com/gorhill/uBlock/issues/1001
|
// https://github.com/gorhill/uBlock/issues/1001
|
||||||
// This must be executed regardless of whether the request is
|
// This must be executed regardless of whether the request is
|
||||||
// behind-the-scene
|
// behind-the-scene
|
||||||
|
var requestType = details.type;
|
||||||
if ( requestType === 'main_frame' ) {
|
if ( requestType === 'main_frame' ) {
|
||||||
pageStore = µb.bindTabToPageStats(tabId, requestURL, 'beforeRequest');
|
return onBeforeRootFrameRequest(details);
|
||||||
if ( pageStore !== null ) {
|
|
||||||
pageStore.requestURL = requestURL;
|
|
||||||
pageStore.requestHostname = pageStore.pageHostname;
|
|
||||||
pageStore.requestType = 'main_frame';
|
|
||||||
pageStore.logRequest(pageStore, '');
|
|
||||||
}
|
|
||||||
mostRecentRootDocURL = requestURL;
|
|
||||||
mostRecentRootDocURLTimestamp = Date.now();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special treatment: behind-the-scene requests
|
// Special treatment: behind-the-scene requests
|
||||||
|
var tabId = details.tabId;
|
||||||
if ( vAPI.isNoTabId(tabId) ) {
|
if ( vAPI.isNoTabId(tabId) ) {
|
||||||
return onBeforeBehindTheSceneRequest(details);
|
return onBeforeBehindTheSceneRequest(details);
|
||||||
}
|
}
|
||||||
|
|
||||||
var pageStore;
|
|
||||||
|
|
||||||
// Lookup the page store associated with this tab id.
|
// Lookup the page store associated with this tab id.
|
||||||
pageStore = µb.pageStoreFromTabId(tabId);
|
var µb = µBlock;
|
||||||
|
var pageStore = µb.pageStoreFromTabId(tabId);
|
||||||
if ( !pageStore ) {
|
if ( !pageStore ) {
|
||||||
if ( mostRecentRootDocURL === '' ) {
|
if ( mostRecentRootDocURL === '' ) {
|
||||||
return;
|
return;
|
||||||
|
@ -111,7 +98,7 @@ var onBeforeRequest = function(details) {
|
||||||
// > sub_frame), frameId indicates the ID of this frame, not the ID of
|
// > sub_frame), frameId indicates the ID of this frame, not the ID of
|
||||||
// > the outer frame.
|
// > the outer frame.
|
||||||
// > (ref: https://developer.chrome.com/extensions/webRequest)
|
// > (ref: https://developer.chrome.com/extensions/webRequest)
|
||||||
var isFrame = requestType === 'sub_frame' || requestType === 'main_frame';
|
var isFrame = requestType === 'sub_frame';
|
||||||
var frameId = isFrame ? details.parentFrameId : details.frameId;
|
var frameId = isFrame ? details.parentFrameId : details.frameId;
|
||||||
if ( frameId > 0 ) {
|
if ( frameId > 0 ) {
|
||||||
if ( frameStore = pageStore.getFrame(frameId) ) {
|
if ( frameStore = pageStore.getFrame(frameId) ) {
|
||||||
|
@ -120,6 +107,7 @@ var onBeforeRequest = function(details) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup context and evaluate
|
// Setup context and evaluate
|
||||||
|
var requestURL = details.url;
|
||||||
requestContext.requestURL = requestURL;
|
requestContext.requestURL = requestURL;
|
||||||
requestContext.requestHostname = details.hostname;
|
requestContext.requestHostname = details.hostname;
|
||||||
requestContext.requestType = requestType;
|
requestContext.requestType = requestType;
|
||||||
|
@ -178,6 +166,25 @@ var onBeforeRequest = function(details) {
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
var onBeforeRootFrameRequest = function(details) {
|
||||||
|
// Special handling for root document.
|
||||||
|
// https://github.com/gorhill/uBlock/issues/1001
|
||||||
|
// This must be executed regardless of whether the request is
|
||||||
|
// behind-the-scene
|
||||||
|
var requestURL = details.url;
|
||||||
|
var pageStore = µBlock.bindTabToPageStats(details.tabId, requestURL, 'beforeRequest');
|
||||||
|
if ( pageStore !== null ) {
|
||||||
|
pageStore.requestURL = requestURL;
|
||||||
|
pageStore.requestHostname = pageStore.pageHostname;
|
||||||
|
pageStore.requestType = 'main_frame';
|
||||||
|
pageStore.logRequest(pageStore, '');
|
||||||
|
}
|
||||||
|
mostRecentRootDocURL = requestURL;
|
||||||
|
mostRecentRootDocURLTimestamp = Date.now();
|
||||||
|
};
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
// Intercept and filter behind-the-scene requests.
|
// Intercept and filter behind-the-scene requests.
|
||||||
|
|
||||||
var onBeforeBehindTheSceneRequest = function(details) {
|
var onBeforeBehindTheSceneRequest = function(details) {
|
||||||
|
|
Loading…
Reference in New Issue