All Projects → videsk → iFrameX

videsk / iFrameX

Licence: LGPL-2.1 license
Iframe generator with dynamic content injection like HTML, Javascript, CSS, etc. and two ways communication, parent <-> iframe.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to iFrameX

Zoid
Cross domain components
Stars: ✭ 1,672 (+9188.89%)
Mutual labels:  iframe, iframes
Wheelchair
An introduction to the battle between JavaScript cheats and anti cheats.
Stars: ✭ 84 (+366.67%)
Mutual labels:  injection, iframe
jekyll-loading-lazy
🧙🏽‍♀️ Automatically adds loading="lazy" to <img> and <iframe> tags. Load images on your sites lazily without JavaScript.
Stars: ✭ 41 (+127.78%)
Mutual labels:  iframe, iframes
consono
The most correct, informative, appealing and configurable variable inspector for JavaScript
Stars: ✭ 17 (-5.56%)
Mutual labels:  javascript-library
rutils
ruitls.js 涵盖了前端开发常用的工具方法,有字符串、数字、数组、缓存、文件等,尽可能的避免前端在开发中重复造轮子
Stars: ✭ 14 (-22.22%)
Mutual labels:  javascript-library
react-lite-yt-embed
React version of lite-youtube-embed iframe which render fast 🚀
Stars: ✭ 79 (+338.89%)
Mutual labels:  iframe
staballoy
Reactive UI framework for Titanium Alloy
Stars: ✭ 18 (+0%)
Mutual labels:  javascript-library
DaggerAutoInject
Inject automatically your Activities & Fragments, just with a simple annotation
Stars: ✭ 49 (+172.22%)
Mutual labels:  injection
xGallerify
A lightweight, responsive, smart gallery based on jQuery
Stars: ✭ 52 (+188.89%)
Mutual labels:  javascript-library
animate
PixiJS runtime library for content from Adobe Animate CC
Stars: ✭ 142 (+688.89%)
Mutual labels:  javascript-library
tsdi
Dependency Injection container (IoC) for TypeScript
Stars: ✭ 50 (+177.78%)
Mutual labels:  injection
vue-frame
Dynamic component for creation of interfaces with iframes
Stars: ✭ 47 (+161.11%)
Mutual labels:  iframes
costa-rica-iban
Funciones utiles para extraer y validar información general de números de cuenta IBAN de Costa Rica
Stars: ✭ 16 (-11.11%)
Mutual labels:  javascript-library
vue-iframe-print
一款支持局部打印的 vue插件
Stars: ✭ 26 (+44.44%)
Mutual labels:  iframe
Mono.Cecil.Inject
An extension to Mono.Cecil that provides helper methods for simple method injection.
Stars: ✭ 65 (+261.11%)
Mutual labels:  injection
resizerjs
Simple resizing for flexbox
Stars: ✭ 16 (-11.11%)
Mutual labels:  javascript-library
CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (+66.67%)
Mutual labels:  injection
constant-time-js
Constant-time JavaScript functions
Stars: ✭ 43 (+138.89%)
Mutual labels:  javascript-library
menu-hamburger
🍔 A clean, simple and easy to use library to create a Menu Hamburger
Stars: ✭ 17 (-5.56%)
Mutual labels:  javascript-library
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (+66.67%)
Mutual labels:  injection

iFrameX

iFrameX is a javascript class for generate iframes with a really simple schema, also have a custom event listener.

This library is not compatible with IE11. Is designed only for modern browsers.

🚨 CAUTION! Never send passwords or credentials via a custom event, can be intercepted by others scripts.

Index

Demo

Demo here

How to use

Use iFrameX is really easy, only need do two things:

const iframe = new iFrameX(options);
iframe.create();

Params

Params is an object can accept these parameters:

  • attributes: Object with attributes of iframe.
  • content: Object or Array with a content of iframe.
  • container: String or DOM element where append iframe in query format.
  • options: Object with some settings parameters.

attributes

const attributes = {
  width: 100,
  height: 100,
  class: 'myiframe'
};

content

This allows adding elements in Object schema. Can set these parameters in object:

  • type: String value of element to create. REQUIRED
  • append: String value of element where append the new element to create. By default is body. OPTIONAL
  • content: String value of content element, can be HTML, Javascript, CSS, etc. OPTIONAL
  • attr: Object value of attributes of element. OPTIONAL

Object example

const content = {
  type: script,
  append: 'body',
  content: `alert('This executed from iframe')`,
  attr: {
    async: true
  },
};

Array example

const content = [
  {
    type: 'link',
    append: 'head',
    attr: {
      href: 'https://cdn.example.com/assets/css/main.css' ,
      rel: 'stylesheet'
    },
  },
  {
    type: 'script',
    append: 'body',
    content: `alert('This executed from iframe')`,
    attr: {
      async: true
    },
  },
  {
    type: 'button',
    content: `My button`,
    attr: {
      class: 'mybutton',
      onclick: 'myFunction()'
    },
  },
];

container

Set where is appended the iframe, and the append value need be in elements query selector format. Read more about elements query selector format here.

By default, will be appended into body tag.

const append = '#myid';
const append = '[data-id="893283420949032"]';
const append = document.querySelector('#my-container');

options

In options parameter you can set:

  • ìd: String Custom if of iFrame
  • eventName: String Custom event name from iframe
  • gateway: Function Function to handle the custom event from iframe
const options = {
  id: 'my-custom-iframe-id',
  eventName: 'MyCustomEventName',
  gateway: function HandleEvent(data) {
    doSomething(data);
  },
};

Custom event listener and PostMessage

This provides the ability of listen custom events from iframe in a simple way.

To use it you need set eventName in options and gateway with a function can handle the event.

Listen event comes from iframe in parent

If you want provide states of HTML elements, data or anything you want from iframe to parent can use this feature, like this:

const options = {
    eventName: 'CustomEventName',
    gateway: function HandleEvent(data) {
        // Here data schema depend how you send from iframe
        doSomething(data);
    },
}; 

// Example content code in multiple lines
(() => {
    // This is how you can send from iframe to parent
    const event = new CustomEvent("CustomEventName", { detail: {date: new Date()} });
    window.parent.document.dispatchEvent(event);
})();

const content = {
    type: 'script',
    content: '(() => window.parent.document.dispatchEvent(new CustomEvent("CustomEventName", { detail: {date: new Date()} })))()', // Example content in one line
}; 

const iframe = new IframeX({content, options});
iframe.create();

The above example code, create an iframe and when this will render, sent custom event CustomEventName with data, that contains an Object with date: new Date(). (Obviously data is completely customizable)

Listen event in iframe from parent

To listen events in iframe that comes from outside is really simple:

In iframe

function newMessage(event) {
    const data = event.detail;
    doSomething(data);
}

window.addEventListener('CustomEventName', newMessage);

In parent

iframe.sendMessage('CustomEventName', {date: new Date()});

Is too important the event listener on iframe will set before send the event from the parent. Is highly recommended set event listener on iframe before all scripts on the body!

Why not use PostMessage?

Because you need set message listener before iframe will render on a parent. And can't create multiples custom events before and after iframe was rendered.

That means pass all data via PostMessage making too complex handle different events and data.

If you're curious is possible handle multiple events with PostMessage with the following schema:

window.addEventListener('message', newMessage);

function newMessage(event) {
    const {event, data} = event.data;
    eventHandler(event, data); // In this function need use if or key object access by event name.   
}

You can use PostMessage in parallel with iFrameX!

Some known bugs

If have error with injection of content, try change order in object content of scripts that block the DOM draw, and move to the final. For example alert('hi') block DOM drawing, try move to the final and works!.

Issue #1.

License

LGPL-2.1 License - By Videsk™

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].