---
title: "init(source:injectionTime:forMainFrameOnly:)"
framework: webkit
role: symbol
role_heading: Initializer
path: "webkit/wkuserscript/init(source:injectiontime:formainframeonly:)"
---

# init(source:injectionTime:forMainFrameOnly:)

Creates a user script object that contains the specified source code and attributes.

## Declaration

```swift
init(source: String, injectionTime: WKUserScriptInjectionTime, forMainFrameOnly: Bool)
```

## Parameters

- `source`: The script’s source code.
- `injectionTime`: The time at which to inject the script into the webpage. For a list of possible values, see doc://com.apple.webkit/documentation/WebKit/WKUserScriptInjectionTime.
- `forMainFrameOnly`: A Boolean value that indicates whether to inject the script into the main frame. Specify doc://com.apple.documentation/documentation/Swift/true to inject the script only into the main frame, or doc://com.apple.documentation/documentation/Swift/false to inject it into all frames.

## Return Value

Return Value An initialized user script, or nil if initialization failed.

## Discussion

Discussion This method sets the script’s content world to the object in the page property of WKContentWorld.

## See Also

### Creating a User Script Object

- [init(source:injectionTime:forMainFrameOnly:in:)](webkit/wkuserscript/init(source:injectiontime:formainframeonly:in:).md)
