1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-09 12:22:33 +01:00

this fixes #99, #991: ability to block popunders

This commit is contained in:
gorhill 2015-11-30 18:50:22 -05:00
parent d2646b68ce
commit c39b892306
2 changed files with 146 additions and 105 deletions

View File

@ -369,6 +369,10 @@ Matrix.prototype.evaluateCellZY = function(srcHostname, desHostname, type) {
// Specific-destination, any party, any type // Specific-destination, any party, any type
var d = desHostname; var d = desHostname;
if ( d === '' ) {
this.r = 0;
return this;
}
while ( d !== '*' ) { while ( d !== '*' ) {
this.y = d; this.y = d;
if ( this.evaluateCellZ(srcHostname, d, '*') !== 0 ) { return this; } if ( this.evaluateCellZ(srcHostname, d, '*') !== 0 ) { return this; }

View File

@ -468,11 +468,93 @@ vAPI.tabs.onClosed = function(tabId) {
/******************************************************************************/ /******************************************************************************/
// https://github.com/chrisaljoudi/uBlock/issues/297 // https://github.com/gorhill/uBlock/issues/99
// https://github.com/gorhill/uBlock/issues/991
//
// popup:
// Test/close target URL
// popunder:
// Test/close opener URL
//
// popup filter match:
// 0 = false
// 1 = true
//
// opener: 0 0 1 1
// target: 0 1 0 1
// ---- ---- ---- ----
// result: a b c d
//
// a: do nothing
// b: close target
// c: close opener
// d: close target
vAPI.tabs.onPopup = function(details) { vAPI.tabs.onPopup = (function() {
//console.debug('vAPI.tabs.onPopup: details = %o', details); //console.debug('vAPI.tabs.onPopup: details = %o', details);
// The same context object will be reused everytime. This also allows to
// remember whether a popup or popunder was matched.
var context = {};
var popupMatch = function(openerURL, targetURL, clickedURL) {
var openerHostname = µb.URI.hostnameFromURI(openerURL);
var openerDomain = µb.URI.domainFromHostname(openerHostname);
context.pageHostname = openerHostname;
context.pageDomain = openerDomain;
context.rootURL = openerURL;
context.rootHostname = openerHostname;
context.rootDomain = openerDomain;
context.requestURL = targetURL;
context.requestHostname = µb.URI.hostnameFromURI(targetURL);
context.requestType = 'popup';
// Dynamic filtering makes sense only when we have a valid hostname.
if ( openerHostname !== '' ) {
// Check user switch first
if (
targetURL !== clickedURL &&
µb.hnSwitches.evaluateZ('no-popups', openerHostname)
) {
return 'ub:no-popups: ' + µb.hnSwitches.z + ' true';
}
// https://github.com/gorhill/uBlock/issues/581
// Take into account popup-specific rules in dynamic URL filtering, OR
// generic allow rules.
µb.sessionURLFiltering.evaluateZ(openerHostname, targetURL, 'popup');
if (
µb.sessionURLFiltering.r === 1 && µb.sessionURLFiltering.type === 'popup' ||
µb.sessionURLFiltering.r === 2
) {
return µb.sessionURLFiltering.toFilterString();
}
// https://github.com/gorhill/uBlock/issues/581
// Take into account `allow` rules in dynamic filtering: `block` rules
// are ignored, as block rules are not meant to block specific types
// like `popup` (just like with static filters).
µb.sessionFirewall.evaluateCellZY(openerHostname, context.requestHostname, 'popup');
if ( µb.sessionFirewall.r === 2 ) {
return µb.sessionFirewall.toFilterString();
}
}
// https://github.com/chrisaljoudi/uBlock/issues/323
// https://github.com/chrisaljoudi/uBlock/issues/1142
// Don't block if uBlock is turned off in popup's context
if (
µb.getNetFilteringSwitch(targetURL) &&
µb.staticNetFilteringEngine.matchStringExactType(context, targetURL, 'popup') !== undefined
) {
return µb.staticNetFilteringEngine.toResultString(µb.logger.isEnabled());
}
return '';
};
return function(details) {
var tabContext = µb.tabContextManager.lookup(details.openerTabId); var tabContext = µb.tabContextManager.lookup(details.openerTabId);
var openerURL = ''; var openerURL = '';
if ( tabContext.tabId === details.openerTabId ) { if ( tabContext.tabId === details.openerTabId ) {
@ -482,8 +564,6 @@ vAPI.tabs.onPopup = function(details) {
return; return;
} }
var µburi = µb.URI;
// https://github.com/gorhill/uBlock/issues/341 // https://github.com/gorhill/uBlock/issues/341
// Allow popups if uBlock is turned off in opener's context. // Allow popups if uBlock is turned off in opener's context.
if ( µb.getNetFilteringSwitch(openerURL) === false ) { if ( µb.getNetFilteringSwitch(openerURL) === false ) {
@ -501,79 +581,28 @@ vAPI.tabs.onPopup = function(details) {
} }
} }
var openerHostname = µburi.hostnameFromURI(openerURL); // Popup test.
var openerDomain = µburi.domainFromHostname(openerHostname); var openerTabId = details.openerTabId;
var context = { var targetTabId = details.targetTabId;
pageHostname: openerHostname, var result = popupMatch(openerURL, targetURL, µb.mouseURL);
pageDomain: openerDomain,
rootHostname: openerHostname,
rootDomain: openerDomain,
requestURL: targetURL,
requestHostname: µb.URI.hostnameFromURI(targetURL),
requestType: 'popup'
};
var result = ''; // Popunder test.
var loggerEnabled = µb.logger.isEnabled();
// Check user switch first
if (
targetURL !== µb.mouseURL &&
µb.hnSwitches.evaluateZ('no-popups', openerHostname)
) {
result = 'ub:no-popups: ' + µb.hnSwitches.z + ' true';
}
// https://github.com/gorhill/uBlock/issues/581
// Take into account popup-specific rules in dynamic URL filtering, OR
// generic allow rules.
if ( result === '' ) { if ( result === '' ) {
µb.sessionURLFiltering.evaluateZ(openerHostname, targetURL, 'popup'); openerTabId = details.targetTabId;
if ( targetTabId = details.openerTabId;
µb.sessionURLFiltering.r === 1 && µb.sessionURLFiltering.type === 'popup' || result = popupMatch(targetURL, openerURL, µb.mouseURL);
µb.sessionURLFiltering.r === 2
) {
result = µb.sessionURLFiltering.toFilterString();
}
} }
// https://github.com/gorhill/uBlock/issues/581 // Log only for when there was a hit against an actual filter (allow or block).
// Take into account `allow` rules in dynamic filtering: `block` rules if ( result !== '' && µb.logger.isEnabled() ) {
// are ignored, as block rules are not meant to block specific types
// like `popup` (just like with static filters).
if ( result === '' ) {
µb.sessionFirewall.evaluateCellZY(openerHostname, context.requestHostname, 'popup');
if ( µb.sessionFirewall.r === 2 ) {
result = µb.sessionFirewall.toFilterString();
}
}
// https://github.com/chrisaljoudi/uBlock/issues/323
// https://github.com/chrisaljoudi/uBlock/issues/1142
// Don't block if uBlock is turned off in popup's context
if (
result === '' &&
µb.getNetFilteringSwitch(targetURL) &&
µb.staticNetFilteringEngine.matchStringExactType(context, targetURL, 'popup') !== undefined
) {
result = µb.staticNetFilteringEngine.toResultString(loggerEnabled);
}
// https://github.com/chrisaljoudi/uBlock/issues/91
var pageStore = µb.pageStoreFromTabId(details.openerTabId);
if ( pageStore ) {
pageStore.logRequest(context, result);
}
if ( loggerEnabled ) {
µb.logger.writeOne( µb.logger.writeOne(
details.openerTabId, openerTabId,
'net', 'net',
result, result,
'popup', 'popup',
targetURL, context.requestURL,
openerHostname, µb.URI.hostnameFromURI(context.rootURL),
openerHostname µb.URI.hostnameFromURI(context.rootURL)
); );
} }
@ -582,17 +611,25 @@ vAPI.tabs.onPopup = function(details) {
return; return;
} }
// Only if a popup was blocked do we report it in the dynamic
// filtering pane.
var pageStore = µb.pageStoreFromTabId(openerTabId);
if ( pageStore ) {
pageStore.logRequest(context, result);
}
// Blocked // Blocked
if ( µb.userSettings.showIconBadge ) { if ( µb.userSettings.showIconBadge ) {
µb.updateBadgeAsync(details.openerTabId); µb.updateBadgeAsync(openerTabId);
} }
// It is a popup, block and remove the tab. // It is a popup, block and remove the tab.
µb.unbindTabFromPageStats(details.targetTabId); µb.unbindTabFromPageStats(targetTabId);
vAPI.tabs.remove(details.targetTabId); vAPI.tabs.remove(targetTabId);
return true; return true;
}; };
})();
vAPI.tabs.registerListeners(); vAPI.tabs.registerListeners();