Created
June 26, 2018 13:34
-
-
Save LasaleFamine/51eb08ed9f36be0b699dc77ba9cc9dd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FrameResizer { | |
| constructor (frameSelector) { | |
| this.frameSelector = frameSelector; | |
| this.iFrame = document.querySelector(this.frameSelector); | |
| this.iFrameOrigin = new URL(this.iFrame.src).origin; | |
| } | |
| _addEvent (element, type, callback) { | |
| return element.addEventListener(type, callback, false); | |
| } | |
| _resizeFrame (event) { | |
| if (!event.origin.includes(this.iFrameOrigin)) { | |
| return; | |
| } | |
| const height = Number(event.data); | |
| this.iFrame.height = height; | |
| } | |
| notifyResize () { | |
| this.iFrame.contentWindow.postMessage('resize', '*'); | |
| } | |
| initialize () { | |
| this._addEvent(window, 'message', this._resizeFrame.bind(this)); | |
| this._addEvent(window, 'resize', this.notifyResize.bind(this)); | |
| this.notifyResize(); | |
| } | |
| } | |
| window.FrameResizer = FrameResizer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment