Skip to content

Commit 513d6c0

Browse files
authored
Merge pull request #25 from ylesaout/ylesaout-iframe-id
Added the possibility to set an id to the iframe
2 parents 3811461 + 1857c3b commit 513d6c0

File tree

5 files changed

+376
-239
lines changed

5 files changed

+376
-239
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ connection.promise.then(parent => {
9595

9696
`options.appendTo` (optional) The element to which the created iframe should be appended. If not provided, the iframe will be appended to `document.body`.
9797

98+
`options.iframe` (optional) The iframe element that Penpal will use instead of creating the iframe element itself. This iframe element must not be already attached to the DOM as it will be appended by Penpal. This option is useful if you need to set attributes to the iframe element before it is appended to the DOM, for example the sandbox attribute. Note that the src attribute will be set by Penpal with the `options.url` value, even if already set.
99+
98100
`options.methods` (optional) An object containing methods which should be exposed for the child iframe to call. The keys of the object are the method names and the values are the functions. If a function requires asynchronous processing to determine its return value, make the function immediately return a promise and resolve the promise once the value has been determined.
99101

100102
`options.timeout` (optional) The amount of time, in milliseconds, Penpal should wait for the child to respond before rejecting the connection promise. There is no timeout by default.
@@ -165,6 +167,39 @@ import {
165167

166168
This provides an opportunity for build optimization (using tools like Webpack or Rollup) in cases where code only needs access to the error constants and not the rest of Penpal.
167169

170+
## Security Note
171+
172+
Penpal does not set the sandbox attribute on the iframe element it creates. If you need to sandbox the iframe, you must, in the parent, create the iframe element, set its sandbox attribute and call the connectToChild API with the created iframe. Here is an example setting the sandbox attribute in the parent window :
173+
174+
175+
```javascript
176+
import Penpal from 'penpal';
177+
178+
const iframe = document.createElement('iframe');
179+
iframe.sandbox = 'allow-scripts';
180+
181+
const connection = Penpal.connectToChild({
182+
// URL of page to load into iframe.
183+
url: 'http://example.com/iframe.html',
184+
// Container to which the iframe should be appended.
185+
appendTo: document.getElementById('iframeContainer'),
186+
// The iframe element to use
187+
iframe: iframe,
188+
// Methods parent is exposing to child
189+
methods: {
190+
add(num1, num2) {
191+
return num1 + num2;
192+
}
193+
}
194+
});
195+
196+
connection.promise.then(child => {
197+
child.multiply(2, 6).then(total => console.log(total));
198+
child.divide(12, 4).then(total => console.log(total));
199+
});
200+
```
201+
202+
168203
## Supported Browsers
169204

170205
Penpal is designed to run successfully on the most recent versions of Internet Explorer, Edge, Chrome, Firefox, and Safari.

src/index.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const DATA_CLONE_ERROR = 'DataCloneError';
1010
export const ERR_CONNECTION_DESTROYED = 'ConnectionDestroyed';
1111
export const ERR_CONNECTION_TIMEOUT = 'ConnectionTimeout';
1212
export const ERR_NOT_IN_IFRAME = 'NotInIframe';
13+
export const ERR_IFRAME_ALREADY_ATTACHED_TO_DOM = 'IframeAlreadyAttachedToDom';
1314

1415
const DEFAULT_PORTS = {
1516
'http:': '80',
@@ -22,6 +23,7 @@ const Penpal = {
2223
ERR_CONNECTION_DESTROYED,
2324
ERR_CONNECTION_TIMEOUT,
2425
ERR_NOT_IN_IFRAME,
26+
ERR_IFRAME_ALREADY_ATTACHED_TO_DOM,
2527

2628
/**
2729
* Promise implementation.
@@ -336,26 +338,33 @@ const connectCallReceiver = (info, methods, destructionPromise) => {
336338
* for the child to respond before rejecting the connection promise.
337339
* @return {Child}
338340
*/
339-
Penpal.connectToChild = ({ url, appendTo, methods = {}, timeout }) => {
341+
Penpal.connectToChild = ({ url, appendTo, iframe, methods = {}, timeout }) => {
342+
if (iframe && iframe.parentNode) {
343+
const error = new Error(
344+
'connectToChild() must not be called with an iframe already attached to DOM'
345+
);
346+
error.code = ERR_IFRAME_ALREADY_ATTACHED_TO_DOM;
347+
throw error;
348+
}
349+
340350
let destroy;
351+
341352
const connectionDestructionPromise = new DestructionPromise(
342353
resolveConnectionDestructionPromise => {
343354
destroy = resolveConnectionDestructionPromise;
344355
}
345356
);
346357

347358
const parent = window;
348-
const iframe = document.createElement('iframe');
349-
350-
(appendTo || document.body).appendChild(iframe);
359+
iframe = iframe || document.createElement('iframe');
360+
iframe.src = url;
351361

352362
connectionDestructionPromise.then(() => {
353363
if (iframe.parentNode) {
354364
iframe.parentNode.removeChild(iframe);
355365
}
356366
});
357367

358-
const child = iframe.contentWindow || iframe.contentDocument.parentWindow;
359368
const childOrigin = getOriginFromUrl(url);
360369
const promise = new Penpal.Promise((resolveConnectionPromise, reject) => {
361370
let connectionTimeoutId;
@@ -380,6 +389,7 @@ Penpal.connectToChild = ({ url, appendTo, methods = {}, timeout }) => {
380389
let destroyCallReceiver;
381390

382391
const handleMessage = event => {
392+
const child = iframe.contentWindow || iframe.contentDocument.parentWindow;
383393
if (
384394
event.source === child &&
385395
event.origin === childOrigin &&
@@ -456,7 +466,7 @@ Penpal.connectToChild = ({ url, appendTo, methods = {}, timeout }) => {
456466
});
457467

458468
log('Parent: Loading iframe');
459-
iframe.src = url;
469+
(appendTo || document.body).appendChild(iframe);
460470
});
461471

462472
return {

0 commit comments

Comments
 (0)