this fixes #99, #991: ability to popunders

This commit is contained in:
gorhill 2015-11-30 18:50:22 -05:00
parent d2646b68ce
commit 6418e6bda6
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
var d = desHostname;
if ( d === '' ) {
this.r = 0;
return this;
}
while ( d !== '*' ) {
this.y = d;
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);
// 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 openerURL = '';
if ( tabContext.tabId === details.openerTabId ) {
@ -482,8 +564,6 @@ vAPI.tabs.onPopup = function(details) {
return;
}
var µburi = µb.URI;
// https://github.com/gorhill/uBlock/issues/341
// Allow popups if uBlock is turned off in opener's context.
if ( µb.getNetFilteringSwitch(openerURL) === false ) {
@ -501,79 +581,28 @@ vAPI.tabs.onPopup = function(details) {
}
}
var openerHostname = µburi.hostnameFromURI(openerURL);
var openerDomain = µburi.domainFromHostname(openerHostname);
var context = {
pageHostname: openerHostname,
pageDomain: openerDomain,
rootHostname: openerHostname,
rootDomain: openerDomain,
requestURL: targetURL,
requestHostname: µb.URI.hostnameFromURI(targetURL),
requestType: 'popup'
};
// Popup test.
var openerTabId = details.openerTabId;
var targetTabId = details.targetTabId;
var result = popupMatch(openerURL, targetURL, µb.mouseURL);
var result = '';
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.
// Popunder test.
if ( result === '' ) {
µb.sessionURLFiltering.evaluateZ(openerHostname, targetURL, 'popup');
if (
µb.sessionURLFiltering.r === 1 && µb.sessionURLFiltering.type === 'popup' ||
µb.sessionURLFiltering.r === 2
) {
result = µb.sessionURLFiltering.toFilterString();
}
openerTabId = details.targetTabId;
targetTabId = details.openerTabId;
result = popupMatch(targetURL, openerURL, µb.mouseURL);
}
// 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).
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 ) {
// Log only for when there was a hit against an actual filter (allow or block).
if ( result !== '' && µb.logger.isEnabled() ) {
µb.logger.writeOne(
details.openerTabId,
openerTabId,
'net',
result,
'popup',
targetURL,
openerHostname,
openerHostname
context.requestURL,
µb.URI.hostnameFromURI(context.rootURL),
µb.URI.hostnameFromURI(context.rootURL)
);
}
@ -582,17 +611,25 @@ vAPI.tabs.onPopup = function(details) {
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
if ( µb.userSettings.showIconBadge ) {
µb.updateBadgeAsync(details.openerTabId);
µb.updateBadgeAsync(openerTabId);
}
// It is a popup, block and remove the tab.
µb.unbindTabFromPageStats(details.targetTabId);
vAPI.tabs.remove(details.targetTabId);
µb.unbindTabFromPageStats(targetTabId);
vAPI.tabs.remove(targetTabId);
return true;
};
})();
vAPI.tabs.registerListeners();