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

# init(source:injectionTime:forMainFrameOnly:in:)

Creates a user script object that is scoped to a particular content world.

## Declaration

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

## 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.
- `contentWorld`: The namespace in which to evaluate the script. This parameter doesn’t apply to changes your script makes to the underlying web content, such as the document’s DOM structure. Those changes remain visible to all scripts, regardless of which content world you specify. For more information about content worlds, see doc://com.apple.webkit/documentation/WebKit/WKContentWorld.

## Return Value

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

## See Also

### Creating a User Script Object

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