Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate Embedding UI as Iframe #22

Closed
morsdyce opened this issue Nov 30, 2015 · 6 comments
Closed

Investigate Embedding UI as Iframe #22

morsdyce opened this issue Nov 30, 2015 · 6 comments
Assignees

Comments

@morsdyce
Copy link
Owner

Investigate embedding the UI as an Iframe while still being able to communicate with the local API to capture requests and forward them to the UI.

@alexilyaev
Copy link
Contributor

@ilyagelman @morsdyce Should work just fine, add this to any page to test (ES6 must be supported):

/**
 * Shredder API Dummy
 */

window.Shredder = {
  sayHi: (one, two) => {
    console.log('API sayHi', one, two);
  }
};

/**
 * Add an iFrame element for the Shredder UI
 */

let iframeElm = document.createElement('iframe');
iframeElm.src = '';
iframeElm.width = '100%';
iframeElm.height = '100%';

document.body.appendChild(iframeElm);

/**
 * Get the iFrame's document, body and head
 */

let iframeRoot = iframeElm.contentDocument;
let iframeBody = iframeRoot.body || iframeRoot.querySelector('body');
let iframeHead = iframeRoot.querySelector('head') || iframeBody;

/**
 * Prepare Shredder UI Styles
 */

let uiStyle = document.createElement('style');
uiStyle.textContent = `
  h1 {
    background: lightblue;
  }
`;

/**
 * Prepare Shredder UI View
 */

let uiHtml = document.createElement('div');
uiHtml.innerHTML = `
  <div>
    <h1>Bad Ass API Mocking</h1>
  </div>
`;

/**
 * Prepare Shredder UI Code
 */

let uiScript = document.createElement('script');
uiScript.type = 'text/javascript';
uiScript.text = `
  // Execute methods from the Shredder API
  window.parent.Shredder.sayHi('Foo', 'Bar')
`;

/**
 * Inject the Shredder UI
 */

iframeBody.appendChild(uiHtml);
iframeHead.appendChild(uiStyle);
iframeBody.appendChild(uiScript);

Open questions

  • The code and styles must be post transpilation, that's no issue on production, we could have them as strings and just inject them, but how would it work in development?
  • We would still need some CSS to position the iframe and handle showing/hiding it, this would have to be added to the original page, the iframe styles would have to be as explicit as possible to avoid external styles from affecting it.

@ilyagelman
Copy link
Contributor

@alexilyaev What about mocking xhr requests within iframe?

@alexilyaev
Copy link
Contributor

@ilyagelman You don't, you mock them outside as usual, the iFrame is just the carrier that will communicate with the Shredder API.

@alexilyaev
Copy link
Contributor

Maayan wrote:
Please keep in mind for whoever implements the iframe, you need to test it in both loading scenarios.

  1. Global var via script injection
  2. Common js import (no global var is created at this method currently)

@morsdyce
Copy link
Owner Author

morsdyce commented Dec 2, 2015

+1 for including this here.

On Wed, Dec 2, 2015 at 7:47 PM, Alex Ilyaev notifications@github.com
wrote:

Maayan wrote:
Please keep in mind for whoever implements the iframe, you need to test it
in both loading scenarios.

  1. Global var via script injection
  2. Common js import (no global var is created at this method currently)


Reply to this email directly or view it on GitHub
#22 (comment).

alexilyaev added a commit that referenced this issue Dec 3, 2015
load ui in iframe (closes #22)
@alexilyaev
Copy link
Contributor

@ilyagelman Does it work with the 2 points Maayan mentioned?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants