From 71d2eed225885e5e40338741ea94ce1ebd5ac8eb Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 28 Apr 2016 11:28:08 -0400 Subject: [PATCH] this fixes #1598 --- src/js/tab.js | 59 ++++++++++++++++++++++++++++++---------------- src/js/uritools.js | 8 +++++++ 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/js/tab.js b/src/js/tab.js index 540c82590..51dc61d23 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -605,22 +605,7 @@ vAPI.tabs.onPopupUpdated = (function() { return ''; }; - var popunderMatch = function(openerURL, targetURL) { - var result = popupMatch(targetURL, openerURL, null, 'popunder'); - if ( µb.isBlockResult(result) ) { - return result; - } - // https://github.com/gorhill/uBlock/issues/1010#issuecomment-186824878 - // Check the opener tab as if it were the newly opened tab: if there - // is a hit against a popup filter, and if the matching filter is not - // a broad one, we will consider the opener tab to be a popunder tab. - // For now, a "broad" filter is one which does not touch any part of - // the hostname part of the opener URL. - var openerHostname = µb.URI.hostnameFromURI(openerURL); - if ( openerHostname === '' ) { - return ''; - } - result = popupMatch(targetURL, openerURL, null, 'popup'); + var mapPopunderResult = function(popunderURL, popunderHostname, result) { if ( result.startsWith('sb:') === false ) { return ''; } @@ -637,23 +622,57 @@ vAPI.tabs.onPopupUpdated = (function() { if ( re === null ) { return ''; } - var matches = re.exec(openerURL); + var matches = re.exec(popunderURL); if ( matches === null ) { return ''; } var beg = matches.index, end = beg + matches[0].length, - pos = openerURL.indexOf(openerHostname); + pos = popunderURL.indexOf(popunderHostname); if ( pos === -1 ) { return ''; } // https://github.com/gorhill/uBlock/issues/1471 // We test whether the opener hostname as at least one character // within matched portion of URL. - if ( beg >= pos + openerHostname.length || end <= pos ) { + return beg < pos + popunderHostname.length && end > pos ? result : ''; + }; + + var popunderMatch = function(openerURL, targetURL) { + var result = popupMatch(targetURL, openerURL, null, 'popunder'); + if ( µb.isBlockResult(result) ) { + return result; + } + // https://github.com/gorhill/uBlock/issues/1010#issuecomment-186824878 + // Check the opener tab as if it were the newly opened tab: if there + // is a hit against a popup filter, and if the matching filter is not + // a broad one, we will consider the opener tab to be a popunder tab. + // For now, a "broad" filter is one which does not touch any part of + // the hostname part of the opener URL. + var popunderURL = openerURL; + var popunderHostname = µb.URI.hostnameFromURI(popunderURL); + if ( popunderHostname === '' ) { return ''; } - return result; + result = mapPopunderResult( + popunderURL, + popunderHostname, + popupMatch(targetURL, popunderURL, null, 'popup') + ); + if ( result !== '' ) { + return result; + } + // https://github.com/gorhill/uBlock/issues/1598 + // Try to find a match against origin part of the opener URL. + popunderURL = µb.URI.originFromURI(popunderURL); + if ( popunderURL === '' ) { + return ''; + } + return mapPopunderResult( + popunderURL, + popunderHostname, + popupMatch(targetURL, popunderURL, null, 'popup') + ); }; return function(targetTabId, openerTabId) { diff --git a/src/js/uritools.js b/src/js/uritools.js index 11ef86a79..9bd1271a8 100644 --- a/src/js/uritools.js +++ b/src/js/uritools.js @@ -49,6 +49,7 @@ var reRFC3986 = /^([^:\/?#]+:)?(\/\/[^\/?#]*)?([^?#]*)(\?[^#]*)?(#.*)?/; // Derived var reSchemeFromURI = /^[^:\/?#]+:/; var reAuthorityFromURI = /^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/; +var reOriginFromURI = /^(?:[^:\/?#]+:)?(?:\/\/[^\/?#]+)/; var reCommonHostnameFromURL = /^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//; var rePathFromURI = /^(?:[^:\/?#]+:)?(?:\/\/[^\/?#]*)?([^?#]*)/; @@ -220,6 +221,13 @@ URI.assemble = function(bits) { /******************************************************************************/ +URI.originFromURI = function(uri) { + var matches = reOriginFromURI.exec(uri); + return matches !== null ? matches[0].toLowerCase() : ''; +}; + +/******************************************************************************/ + URI.schemeFromURI = function(uri) { var matches = reSchemeFromURI.exec(uri); if ( !matches ) {