Skip to content
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit 9eb89bb

Browse files
committed
Fix cross-origin iframe bug. Fixes #46
1 parent 4629f9e commit 9eb89bb

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/react-image-lightbox.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
translate,
1111
getWindowWidth,
1212
getWindowHeight,
13-
isInIframe,
13+
isInSameOriginIframe,
1414
getIEVersion,
1515
} from './util';
1616
import {
@@ -239,7 +239,7 @@ class ReactImageLightbox extends Component {
239239
window.addEventListener('pointercancel', this.handlePointerEvent);
240240
// Have to add an extra mouseup handler to catch mouseup events outside of the window
241241
// if the page containing the lightbox is displayed in an iframe
242-
if (isInIframe()) {
242+
if (isInSameOriginIframe()) {
243243
window.top.addEventListener('mouseup', this.handleMouseUp);
244244
window.top.addEventListener('touchend', this.handleTouchEnd);
245245
window.top.addEventListener('touchcancel', this.handleTouchEnd);
@@ -351,7 +351,7 @@ class ReactImageLightbox extends Component {
351351
window.removeEventListener('pointermove', this.handlePointerEvent);
352352
window.removeEventListener('pointerup', this.handlePointerEvent);
353353
window.removeEventListener('pointercancel', this.handlePointerEvent);
354-
if (isInIframe()) {
354+
if (isInSameOriginIframe()) {
355355
window.top.removeEventListener('mouseup', this.handleMouseUp);
356356
window.top.removeEventListener('touchend', this.handleTouchEnd);
357357
window.top.removeEventListener('touchcancel', this.handleTouchEnd);

src/util.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ export function getWindowHeight() {
5252
}
5353

5454
// Returns true if this window is rendered as an iframe inside another window
55-
export function isInIframe() {
55+
// with the same origin.
56+
export function isInSameOriginIframe() {
5657
try {
57-
return window.self !== window.top;
58+
return (window.self !== window.top) && window.top.document;
5859
} catch (e) {
59-
return true;
60+
return false;
6061
}
6162
}

0 commit comments

Comments
 (0)