QQuickWebEngineScript Class | Qt WebEngine 5.9

Qt 5.9 | Qt WebEngine | C++ Classes | QQuickWebEngineScript

Qt 5.9.9 Reference Documentation

# QQuickWebEngineScript Class

Enables the injection of scripts in the JavaScript engine. More...

Header: | #include <QQuickWebEngineScript>
Since: | Qt 5.9
Instantiated By: | WebEngineScript

- List of all members, including inherited members

## Public Types

enum | InjectionPoint { DocumentCreation, DocumentReady, Deferred }
enum | ScriptWorldId { MainWorld, ApplicationWorld, UserWorld }

## Properties

injectionPoint : InjectionPoint name : QString runOnSubframes : bool | sourceCode : QString sourceUrl : QUrl worldId : ScriptWorldId

## Public Functions

| QQuickWebEngineScript(QObject *parent = Q_NULLPTR)
InjectionPoint | injectionPoint() const
QString | name() const
bool | runOnSubframes() const
void | setInjectionPoint(InjectionPoint injectionPoint)
void | setName(const QString &name)
void | setRunOnSubframes(bool on)
void | setSourceCode(const QString &code)
void | setSourceUrl(const QUrl &url)
void | setWorldId(ScriptWorldId scriptWorldId)
QString | sourceCode() const
QUrl | sourceUrl() const
QString | toString() const
ScriptWorldId | worldId() const

## Signals

void | injectionPointChanged(InjectionPoint injectionPoint)
void | nameChanged(const QString &name)
void | runOnSubframesChanged(bool on)
void | sourceCodeChanged(const QString &code)
void | sourceUrlChanged(const QUrl &url)
void | worldIdChanged(ScriptWorldId scriptWorldId)

## Detailed Description

Enables the injection of scripts in the JavaScript engine.

The QQuickWebEngineScript type enables the programmatic injection of so called user scripts in the JavaScript engine at different points, determined by injectionPoint, during the loading of web content.

Scripts can be executed either in the main JavaScript world, along with the rest of the JavaScript coming from the web contents, or in their own isolated world. While the DOM of the page can be accessed from any world, JavaScript variables of a function defined in one world are not accessible from a different one. The worldId property provides some predefined IDs for this purpose.

## Member Type Documentation

### enum QQuickWebEngineScript::InjectionPoint

The point in the loading process at which the script will be executed.

Constant | Value | Description
--- | --- | ---
`QQuickWebEngineScript::DocumentCreation` | `2` | The script will be executed as soon as the document is created. This is not suitable for any DOM operation.
`QQuickWebEngineScript::DocumentReady` | `1` | The script will run as soon as the DOM is ready. This is equivalent to the `DOMContentLoaded` event firing in JavaScript.
`QQuickWebEngineScript::Deferred` | `0` | The script will run when the page load finishes, or 500 ms after the document is ready, whichever comes first.

### enum QQuickWebEngineScript::ScriptWorldId

The world ID defining which isolated world the script is executed in.

Constant | Value | Description
--- | --- | ---
`QQuickWebEngineScript::MainWorld` | `0` | The world used by the page's web contents. It can be useful in order to expose custom functionality to web contents in certain scenarios.
`QQuickWebEngineScript::ApplicationWorld` | `1` | The default isolated world used for application level functionality implemented in JavaScript.
`QQuickWebEngineScript::UserWorld` | `2` | The first isolated world to be used by scripts set by users if the application is not making use of more worlds. As a rule of thumb, if that functionality is exposed to the application users, each individual script should probably get its own isolated world.

## Property Documentation

### injectionPoint : InjectionPoint

This property holds the point in the loading process at which the script will be executed.

The default value is `Deferred`.

Access functions:

InjectionPoint | injectionPoint() const
void | setInjectionPoint(InjectionPoint injectionPoint)

Notifier signal:

void | injectionPointChanged(InjectionPoint injectionPoint)

### name : QString

This property holds the name of the script.

Can be useful to retrieve a particular script from QQuickWebEngineProfile::userScripts.

Access functions:

QString | name() const
void | setName(const QString &name)

Notifier signal:

void | nameChanged(const QString &name)

### runOnSubframes : bool

This property holds whether the script is executed on every frame or only on the main frame.

Set this property to `true` if the script is executed on every frame in the page, or `false` if it is only run for the main frame. The default value is `false`.

Access functions:

bool | runOnSubframes() const
void | setRunOnSubframes(bool on)

Notifier signal:

void | runOnSubframesChanged(bool on)

### sourceCode : QString

This property holds the JavaScript source code of the user script.

Access functions:

QString | sourceCode() const
void | setSourceCode(const QString &code)

Notifier signal:

void | sourceCodeChanged(const QString &code)

See also QQuickWebEngineScript::sourceUrl.

### sourceUrl : QUrl

This property holds the remote source location of the user script (if any).

Unlike sourceCode, this property allows referring to user scripts that are not already loaded in memory, for instance, when stored on disk.

Setting this property will change the sourceCode of the script.

Note: At present, only file-based sources are supported.

Access functions:

QUrl | sourceUrl() const
void | setSourceUrl(const QUrl &url)

Notifier signal:

void | sourceUrlChanged(const QUrl &url)

See also QQuickWebEngineScript::sourceCode.

### worldId : ScriptWorldId

This property holds the world ID defining which isolated world the script is executed in.

Access functions:

ScriptWorldId | worldId() const
void | setWorldId(ScriptWorldId scriptWorldId)

Notifier signal:

void | worldIdChanged(ScriptWorldId scriptWorldId)

## Member Function Documentation

### QQuickWebEngineScript::QQuickWebEngineScript(QObject *parent = Q_NULLPTR)

Constructs a new QQuickWebEngineScript with the parent parent.

### QString QQuickWebEngineScript::toString() const

Returns the script object as string.