Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise event on navigation through the child window opening #2754

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/index.ts
Expand Up @@ -93,7 +93,7 @@ class Hammerhead {
this.transport = new Transport();
this.sandbox = new Sandbox(this.transport);
this.pageNavigationWatch = new PageNavigationWatch(this.sandbox.event, this.sandbox.codeInstrumentation,
this.sandbox.node.element);
this.sandbox.node.element, this.sandbox.childWindow);

this.EVENTS = {
beforeFormSubmit: this.sandbox.node.element.BEFORE_FORM_SUBMIT_EVENT,
Expand Down
13 changes: 12 additions & 1 deletion src/client/page-navigation-watch.ts
Expand Up @@ -11,6 +11,7 @@ import EventSandbox from './sandbox/event';
import CodeInstrumentation from './sandbox/code-instrumentation';
import ElementSandbox from './sandbox/node/element';
import { ElementSandboxBeforeFormSubmitEvent } from '../typings/client';
import ChildWindowSandbox from './sandbox/child-window';

export default class PageNavigationWatch extends EventEmiter {
PAGE_NAVIGATION_TRIGGERED_EVENT = 'hammerhead|event|page-navigation-triggered';
Expand All @@ -19,7 +20,8 @@ export default class PageNavigationWatch extends EventEmiter {

constructor (private readonly _eventSandbox: EventSandbox,
private readonly _codeInstrumentation: CodeInstrumentation,
private readonly _elementSandbox: ElementSandbox) {
private readonly _elementSandbox: ElementSandbox,
private readonly _childWindowSandbox) {
super();

this._lastLocationValue = window.location.toString();
Expand Down Expand Up @@ -125,6 +127,14 @@ export default class PageNavigationWatch extends EventEmiter {
}
}

_childWindowWatch (childWindow: ChildWindowSandbox): void {
const self = this;

childWindow.on(childWindow.BEFORE_WINDOW_OPEN_IN_SAME_TAB, ({ url }) => {
self.onNavigationTriggered(url);
});
}

onNavigationTriggered (url: string): void {
const currentLocation = this._lastLocationValue;

Expand All @@ -146,5 +156,6 @@ export default class PageNavigationWatch extends EventEmiter {
this._locationWatch(this._codeInstrumentation);
this._linkWatch(this._eventSandbox);
this._formWatch(this._elementSandbox, this._eventSandbox);
this._childWindowWatch(this._childWindowSandbox);
}
}
9 changes: 8 additions & 1 deletion src/client/sandbox/child-window/index.ts
Expand Up @@ -15,12 +15,14 @@ import INTERNAL_PROPS from '../../../processing/dom/internal-properties';
import getTopOpenerWindow from '../../utils/get-top-opener-window';
import { isIframeWindow } from '../../utils/dom';
import nextTick from '../../utils/next-tick';
import { version, isSafari } from '../../utils/browser';

const DEFAULT_WINDOW_PARAMETERS = 'width=500px, height=500px';
const STORE_CHILD_WINDOW_CMD = 'hammerhead|command|store-child-window';

export default class ChildWindowSandbox extends SandboxBase {
readonly WINDOW_OPENED_EVENT = 'hammerhead|event|window-opened';
public readonly WINDOW_OPENED_EVENT = 'hammerhead|event|window-opened';
public readonly BEFORE_WINDOW_OPEN_IN_SAME_TAB = 'hammerhead|event|before-window-open-in-same-tab';
private _childWindows: Set<Window> | null;

constructor (private readonly _messageSandbox: MessageSandbox,
Expand Down Expand Up @@ -133,6 +135,11 @@ export default class ChildWindowSandbox extends SandboxBase {
return openedWindowInfo.wnd;
}

// NOTE: Safari stopped throwing the 'unload' event for this case starting from 14 version.
// We are forced using the pageNavigationWatch to guarantee working the storages transfer between pages.
if (isSafari && version >= 15)
this.emit(this.BEFORE_WINDOW_OPEN_IN_SAME_TAB, { url });

return nativeMethods.windowOpen.apply(window, args);
}

Expand Down
9 changes: 9 additions & 0 deletions test/client/fixtures/page-navigation-watch-test.js
Expand Up @@ -498,6 +498,15 @@ if (!browserUtils.isIE || browserUtils.isMSEdge) {
});
}

if (browserUtils.isSafari && browserUtils.version >= 15) {
test('Navigation through the opening window in the same tab', function () {
return navigateIframe("window.open('./index.html')")
.then(function (url) {
strictEqual(url, iframeLocation + 'index.html');
});
});
}

module('regression');

test('the onNavigationTriggered function should not throw an error when receives only hash (GH-917)', function () {
Expand Down