diff --git a/README.md b/README.md index 55e3d05..70d0dcc 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,50 @@ The `children` contents is what will be rendered into the new popup window. In t | `onBlock` | `Function` | `undefined` | A function to be triggered when the new window could not be opened. | | `center` | `String` | `parent` | Indicate how to center the new window. Valid values are: `parent` or `screen`. `parent` will center the new window according to its _parent_ window. `screen` will center the new window according to the _screen_. | | `copyStyles` | `Boolean` | `true` | If specified, copy styles from parent window's document. | + | `scripts` | `Array` | ` ` | If specified, add script elements to the head of new window's document. | + | `metas` | `Array` | ` ` | If specified, add meta elements to the head of new window's document. | + +### Property `scripts` + +Pass the following data to `` + +````javascript +[ + { + src: "http://www.example.com/example.js" + }, + { + code: "console.log('This is an example')" + } +]; +```` + +will generate the following script tags: + +````javascript + + +```` + +### Property `metas` + +Pass the following data to `` + + +````javascript +[ + { + charset: "UTF-8", + content: "IE=edge" + } +]; +```` + +will generate the following script tags: + +````javascript + +```` ## Tests diff --git a/src/NewWindow.d.ts b/src/NewWindow.d.ts index 7b7bac0..0fafd7e 100644 --- a/src/NewWindow.d.ts +++ b/src/NewWindow.d.ts @@ -12,6 +12,14 @@ export interface IWindowFeatures { [i: string]: boolean | number | string } +/** + * Represents a script tag in the head + */ +export interface IScript { + src: string + code: string +} + /** * Props for opening a new window. * @@ -57,6 +65,16 @@ declare export interface INewWindowProps { * If specified, copy styles from parent window's document. */ copyStyles?: boolean + + /** + * If specified, add script elements to the head + */ + scripts?: Array + + /** + * If specified, add meta elements to the head + */ + metas?: Array } declare export default class NewWindow extends React.PureComponent { diff --git a/src/NewWindow.js b/src/NewWindow.js index ce3775b..b65693a 100644 --- a/src/NewWindow.js +++ b/src/NewWindow.js @@ -117,6 +117,22 @@ class NewWindow extends React.PureComponent { setTimeout(() => copyStyles(document, this.window.document), 0) } + // If specified, add script elements + if (this.props.scripts) { + const popupDocument = this.window.document + this.props.scripts.forEach(function(script) { + addScript(popupDocument, script) + }) + } + + // If specified, add meta elements + if (this.props.metas) { + const popupDocument = this.window.document + this.props.metas.forEach(function(meta) { + addMeta(popupDocument, meta) + }) + } + // Release anything bound to this component before the new window unload. this.window.addEventListener('beforeunload', () => this.release()) } else { @@ -169,7 +185,9 @@ NewWindow.propTypes = { onUnload: PropTypes.func, onBlock: PropTypes.func, center: PropTypes.oneOf(['parent', 'screen']), - copyStyles: PropTypes.bool + copyStyles: PropTypes.bool, + scripts: PropTypes.array, + metas: PropTypes.array } /** @@ -251,6 +269,42 @@ function toWindowFeatures(obj) { .join(',') } +/** + * Add a new script element to the head + * @private + */ +function addScript(document, scriptObject) { + const script = document.createElement('script') + script.type = 'text/javascript' + + if (scriptObject.src) { + script.src = scriptObject.src + } + + if (scriptObject.code) { + script.text = scriptObject.code + } + + document.head.appendChild(script) +} + +/** + * Add a new meta element to the head + * @private + */ +function addMeta(document, metaObject) { + let meta = document.createElement('meta') + + for (const key in metaObject) { + if (metaObject.hasOwnProperty(key)) { + //Not a property from prototype chain + meta.setAttribute(key, metaObject[key]) + } + } + + document.head.appendChild(meta) +} + /** * Component export. * @private