<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.10.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "WebKit",
  "identifier" : "/documentation/WebKit/WKUserScript",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "WebKit"
    ],
    "preciseIdentifier" : "c:objc(cs)WKUserScript"
  },
  "title" : "WKUserScript"
}
-->

# WKUserScript

A script that the web view injects into a webpage.

```
@MainActor class WKUserScript
```

## Overview

Create a [`WKUserScript`](/documentation/WebKit/WKUserScript) object when you want to inject custom script code into the pages of your web view. Use this object to specify the JavaScript code to inject, and parameters relating to when and how to inject that code. Before you create the web view, add this object to the [`WKUserContentController`](/documentation/WebKit/WKUserContentController) object associated with your web view’s configuration.

## Topics

### Creating a User Script Object

[`init(source:injectionTime:forMainFrameOnly:)`](/documentation/WebKit/WKUserScript/init(source:injectionTime:forMainFrameOnly:))

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

[`init(source:injectionTime:forMainFrameOnly:in:)`](/documentation/WebKit/WKUserScript/init(source:injectionTime:forMainFrameOnly:in:))

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

### Inspecting Script Information

[`source`](/documentation/WebKit/WKUserScript/source)

The script’s source code.

[`injectionTime`](/documentation/WebKit/WKUserScript/injectionTime)

The time at which to inject the script into the webpage.

[`WKUserScriptInjectionTime`](/documentation/WebKit/WKUserScriptInjectionTime)

Constants for the times at which to inject script content into a webpage.

[`isForMainFrameOnly`](/documentation/WebKit/WKUserScript/isForMainFrameOnly)

A Boolean value that indicates whether to inject the script into the main frame or all frames.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)