Skip to content

Add new props of scripts and metas #28

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<NewWindow scripts={scripts}>`

````javascript
[
{
src: "http://www.example.com/example.js"
},
{
code: "console.log('This is an example')"
}
];
````

will generate the following script tags:

````javascript
<script type="text/javascript" src="http://www.example.com/example.js"></script>
<script type="text/javascript">console.log('This is an example')</script>
````

### Property `metas`

Pass the following data to `<NewWindow metas={metas}>`


````javascript
[
{
charset: "UTF-8",
content: "IE=edge"
}
];
````

will generate the following script tags:

````javascript
<meta charset="UTF-8" content="IE=edge">
````

## Tests

Expand Down
18 changes: 18 additions & 0 deletions src/NewWindow.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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<IScript>

/**
* If specified, add meta elements to the head
*/
metas?: Array<Object>
}

declare export default class NewWindow extends React.PureComponent<INewWindowProps> {
Expand Down
56 changes: 55 additions & 1 deletion src/NewWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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
Expand Down