Skip to content

Commit

Permalink
Merge pull request #41 in EXTENSIONS/popup-blocker from feature/issue…
Browse files Browse the repository at this point in the history
…s/100 to master

Squashed commit of the following:

commit ddfaa2f0fb46112f1115ee7ae28c3ccc7e33eace
Author: seanl-adg <seanl@adguard.com>
Date:   Mon Jun 18 23:11:28 2018 +0900

    Extract numbers to constants; tweak for minifier

commit aa41a0f3a6ce59bcf87674a57146ac6795817b18
Author: seanl-adg <seanl@adguard.com>
Date:   Mon Jun 18 22:50:40 2018 +0900

    Fully fix #45

commit 103e0eb6c4b6f38544364b2fbf2ce5d668ba62b0
Merge: 776e6f2 669c801
Author: seanl-adg <seanl@adguard.com>
Date:   Fri Jun 15 21:35:53 2018 +0900

    Actualize

commit 776e6f268d5d8c0be34b2325a3ba34b021474590
Author: seanl-adg <seanl@adguard.com>
Date:   Fri Jun 15 19:34:34 2018 +0900

    Make it work

commit 611e809a4601b324e08b6949f8cd6d153c75a481
Author: seanl-adg <seanl@adguard.com>
Date:   Fri Jun 15 03:02:10 2018 +0900

    Initial attempt

commit 69c56417970e2019eda07c12e845da171913c6c4
Merge: 3c27461 827e20b
Author: seanl-adg <seanl@adguard.com>
Date:   Tue Jun 12 00:52:32 2018 +0900

    Merge branch 'master' into feature/issues/100

commit 3c27461061fba1c46bb8790961aa7b858d1c2def
Merge: ea6e3bb 636f015
Author: seanl-adg <seanl@adguard.com>
Date:   Fri Jun 8 16:42:42 2018 +0900

    Merge branch 'feature/issues/121' into feature/issues/100

commit 636f01595f5715af8aa51e0836ccbd0ddd20403b
Author: seanl-adg <seanl@adguard.com>
Date:   Fri Jun 8 07:07:26 2018 +0900

    Change dev build url

commit e02ecc6391277a073d4e453d04b68a0ce0f0d67f
Author: seanl-adg <seanl@adguard.com>
Date:   Thu Jun 7 20:41:39 2018 +0900

    change options page url; tweak for closure compiler; update translations from onesky

commit ea6e3bbc3d325ef78d928292e85da465fbc302e7
Author: seanl-adg <seanl@adguard.com>
Date:   Thu Jun 7 12:11:02 2018 +0900

    Attempt to improve overlay detection
  • Loading branch information
Sean Lee committed Jun 18, 2018
1 parent 669c801 commit 43d1fa1
Show file tree
Hide file tree
Showing 22 changed files with 397 additions and 190 deletions.
1 change: 1 addition & 0 deletions compiler/bundle/Bundler.ts
Expand Up @@ -39,6 +39,7 @@ export default class Bundler {
const bundles = sourceStream
.pipe(rollup(this.ctxt.getRollupOptions()))
.pipe(insert.transform(this.rescMgr.inline))
.pipe(insert.transform(Bundler.removeCcExport))
.pipe(hydra(this.ctxt.bundleFilter));
// Create reservoirs
let out:StringMap<Reservoir> = {};
Expand Down
1 change: 0 additions & 1 deletion gulpfile.ts
Expand Up @@ -93,7 +93,6 @@ gulp.task('build', () => {
return new Builder(option).build()
})


/******************************************************************************************************/

// UglifyJS option for dead code removal and stripping out comments.
Expand Down
19 changes: 11 additions & 8 deletions src/dom/open.ts
Expand Up @@ -2,18 +2,22 @@ import adguard from '../page_script_namespace';
import { verifyEvent, retrieveEvent } from '../events/verify';
import examineTarget from '../events/examine_target';
import { isGtmSimulatedAnchorClick } from '../events/framework_workarounds';
import { timeline } from '../timeline/index';
import { TLEventType, TimelineEvent } from '../timeline/event';
import { timeline } from '../timeline/Timeline';
import { TLEventType, TimelineEvent } from '../timeline/TimelineEvent';
import * as log from '../shared/debug';
import createUrl from '../shared/url';
import mockWindow from '../mock_window';
import onBlocked from '../on_blocked';
import { ApplyHandler } from '../proxy/IProxyService';
import ILoggedProxyService from '../proxy/ILoggedProxyService';

export interface PopupContext {
mocked?:boolean,
defaultEventHandlerTarget?:string
}

export function wrapOpen(window:Window, proxyService:ILoggedProxyService) {
const openVerifiedWindow:ApplyHandler<Window,Window> = (execContext, _arguments, externalContext) => {
const openVerifiedWindow:ApplyHandler<Window,Window,PopupContext> = (execContext, _arguments, externalContext) => {
if (adguard.contentScriptApiFacade.originIsWhitelisted()) {
return execContext.invokeTarget(_arguments);
}
Expand Down Expand Up @@ -46,18 +50,17 @@ export function wrapOpen(window:Window, proxyService:ILoggedProxyService) {
return win;
}

onBlocked(url[2], currentEvent);

externalContext.mocked = true;
onBlocked(url[2], currentEvent, externalContext);
log.print('mock a window object');
// Return a mock window object, in order to ensure that the page's own script does not accidentally throw TypeErrors.
win = mockWindow(_arguments[0], _arguments[1], proxyService);
win = proxyService.makeObjectProxy(win);
externalContext['mocked'] = true;
log.callEnd();
return win;
};

proxyService.wrapMethod(window, 'open', openVerifiedWindow);
proxyService.wrapMethod(window.Window.prototype, 'open', openVerifiedWindow); // for IE
proxyService.wrapMethod<Window,Window,PopupContext>(window, 'open', openVerifiedWindow);
proxyService.wrapMethod<Window,Window,PopupContext>(window.Window.prototype, 'open', openVerifiedWindow); // for IE
}

57 changes: 44 additions & 13 deletions src/events/element_tests.ts
Expand Up @@ -25,16 +25,6 @@ export const eventTargetIsRootNode = (el:EventTarget):boolean => {
return false;
};

export const maskStyleTest = (el:Element):boolean => {
const style = getComputedStyle(el);
const position = style.getPropertyValue('position');
const zIndex = style.getPropertyValue('z-index');
// Theoretically, opacity css property can be used to make masks as well
// but hasn't encountered such usage in the wild, so not including it.
if (position !== 'static' && parseInt(zIndex, 10) > 1000) { return true; }
return false;
};

export const maskContentTest = (el:Element):boolean => {
let textContent = el.textContent;
if (textContent && textContent.trim().length) { return false; }
Expand All @@ -50,11 +40,52 @@ export function maybeOverlay(el:Element):boolean {
if (!isHTMLElement(el)) { return false; } // not an HTMLElement instance
const view = el.ownerDocument.defaultView;
const w = view.innerWidth, h = view.innerHeight;
if (el.offsetLeft << 4 < w && (w - el.offsetWidth) << 3 < w
&& el.offsetTop << 4 < h && (h - el.offsetHeight) << 3 < h) {
return maskStyleTest(el);
const {left, right, top, bottom } = el.getBoundingClientRect();

if (rectAlmostCoversView(el.getBoundingClientRect(), w, h)) {
// Find artificial stacking context root
do {
if (isArtificialStackingContextRoot(el)) {
return true;
}
} while (el = el.parentElement)
}
// ToDo: the element may have been modified in the event handler.
// We may still test it using the inline style attribute.
return false;
}

/**
* Detects a common stacking context root pattern.
* Stacking context root: https://philipwalton.com/articles/what-no-one-told-you-about-z-index/
* https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context
*/
export function isArtificialStackingContextRoot(el:Element) {
const { zIndex, position, opacity } = getComputedStyle(el);
if (
(position !== 'static' && zIndex !== 'auto') ||
parseFloat(opacity) < 1
) {
if (parseInt(zIndex) > 1000) {
return true;
}
}
return false;
}


export function numsAreClose(x:number, y: number, threshold:number) {
return (((x - y) / threshold) | 0) === 0;
}

/**
* @param w view.innerWidth
* @param h view.innerHeight
*/
export function rectAlmostCoversView(rect:ClientRect, w:number, h:number) {
const { left, right, top, bottom } = rect;
return numsAreClose(left, 0, w >> 4) &&
numsAreClose(right, w, w >> 4) &&
numsAreClose(top, 0, h >> 4) &&
numsAreClose(bottom, h, h >> 4);
}

0 comments on commit 43d1fa1

Please sign in to comment.