Skip to content

Commit a16ab01

Browse files
committed
MouseManager.isTop is a new boolean read-only property that flags if the mouse event listeners were attached to window.top (true), or just window (false). By default Phaser will attempt window.top, but this isn't possible in all environments, such as cross-origin iframes, so it will fall back to window in those cases and set this property to false
1 parent 6d5348b commit a16ab01

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

src/input/mouse/MouseManager.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,20 @@ var MouseManager = new Class({
202202
*/
203203
this.pointerLockChange = NOOP;
204204

205+
/**
206+
* Are the event listeners hooked into `window.top` or `window`?
207+
*
208+
* This is set during the `boot` sequence. If the browser does not have access to `window.top`,
209+
* such as in cross-origin iframe environments, this property gets set to `false` and the events
210+
* are hooked into `window` instead.
211+
*
212+
* @name Phaser.Input.Mouse.MouseManager#isTop
213+
* @type {boolean}
214+
* @readonly
215+
* @since 3.50.0
216+
*/
217+
this.isTop = true;
218+
205219
inputManager.events.once(InputEvents.MANAGER_BOOT, this.boot, this);
206220
},
207221

@@ -436,8 +450,18 @@ var MouseManager = new Class({
436450

437451
if (window && manager.game.config.inputWindowEvents)
438452
{
439-
window.top.addEventListener('mousedown', this.onMouseDownWindow, passive);
440-
window.top.addEventListener('mouseup', this.onMouseUpWindow, passive);
453+
try
454+
{
455+
window.top.addEventListener('mousedown', this.onMouseDownWindow, passive);
456+
window.top.addEventListener('mouseup', this.onMouseUpWindow, passive);
457+
}
458+
catch (exception)
459+
{
460+
window.addEventListener('mousedown', this.onMouseDownWindow, passive);
461+
window.addEventListener('mouseup', this.onMouseUpWindow, passive);
462+
463+
this.isTop = false;
464+
}
441465
}
442466

443467
if (Features.pointerLock)
@@ -478,8 +502,10 @@ var MouseManager = new Class({
478502

479503
if (window)
480504
{
481-
window.top.removeEventListener('mousedown', this.onMouseDownWindow);
482-
window.top.removeEventListener('mouseup', this.onMouseUpWindow);
505+
target = (this.isTop) ? window.top : window;
506+
507+
target.removeEventListener('mousedown', this.onMouseDownWindow);
508+
target.removeEventListener('mouseup', this.onMouseUpWindow);
483509
}
484510

485511
if (Features.pointerLock)

0 commit comments

Comments
 (0)