UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

150 lines (95 loc) โ€ข 3.48 kB
# ๐Ÿงฉ TinyIframeEvents A flexible event routing system for structured communication between a **parent window** and its **iframe** using postMessage. This module abstracts the complexity of origin validation, window detection, and message direction. It allows **modular, bidirectional, and secure communication** across frames. --- ## โœจ Features - ๐Ÿ” Bidirectional communication (`iframe` โ‡„ `parent`) - ๐ŸŽฏ Named event routes with custom payloads - ๐Ÿ” Target window and origin validation - ๐Ÿง  Smart context detection (no config required inside iframe) - ๐Ÿงน Clean-up support with `.destroy()` --- ## ๐Ÿš€ Usage ### In the parent: ```js import TinyIframeEvents from './TinyIframeEvents.mjs'; const iframe = document.querySelector('iframe'); const parentEvents = new TinyIframeEvents({ targetIframe: iframe, // Will use iframe.contentWindow targetOrigin: window.location.origin }); parentEvents.emit('hello:iframe', { text: '๐Ÿ‘‹ From parent!' }); parentEvents.on('reply:fromIframe', (data, event) => { console.log('๐Ÿ“จ Received from iframe:', data, event); }); ``` --- ### In the iframe: ```js import TinyIframeEvents from './TinyIframeEvents.mjs'; const iframeEvents = new TinyIframeEvents(); iframeEvents.on('hello:iframe', (data, event) => { console.log('๐Ÿ“ฅ Message from parent:', event); }); iframeEvents.emit('reply:fromIframe', { text: '๐Ÿ™‹โ€โ™€๏ธ Hi parent!' }); ``` --- ## ๐Ÿง  API Reference ### `new TinyIframeEvents(config?)` Creates a new instance for communication. #### Parameters: | Name | Type | Description | | -------------- | -------------------- | --------------------------------------------------------------------- | | `targetIframe` | `HTMLIFrameElement?` | The iframe element to communicate with (used in the parent only). | | `targetOrigin` | `string?` | Expected origin (for security). Defaults to `window.location.origin`. | --- ### `emit(eventName, payload)` Sends a message to the target frame. | Param | Type | Description | | ----------- | -------- | ------------------------------------------ | | `eventName` | `string` | Unique identifier of the event. | | `payload` | `any` | The data to send (any serializable value). | #### Throws: * `TypeError` if `eventName` is not a string. --- ### `on(eventName, handler)` Registers a listener for a specific event. ```js iframeEvents.on('my:event', (payload, event) => { // event = { origin, source } }); ``` --- ### `destroy()` Removes all listeners and message event binding. Call this when the instance is no longer needed. ```js iframeEvents.destroy(); ``` --- ### `isDestroyed()` Returns `true` if the instance has been destroyed via `.destroy()`. ```js if (events.isDestroyed()) { console.warn('Event system is no longer active.'); } ``` --- ## ๐Ÿ”’ Security Notes * Only messages with the flag `__tinyEvent: true` are accepted. * The source window is checked strictly (`source === targetWindow`). * Messages are ignored unless their `direction` matches the current side (`iframe` or `parent`). --- ## ๐Ÿ“š Internals (for advanced users) This class internally wraps: * postMessage * A minimal event router: `TinyEvents` --- ## ๐Ÿ“„ Type Definitions ### `@callback handler` ```ts type handler = ( payload: any, event: MessageEvent<any> ) => void; ```