QQuickWebEngineScript Class

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

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

This class was introduced in Qt 5.9.

Public Types

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

Properties

Public Functions

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

Signals

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

Detailed Description

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.

ConstantValueDescription
QQuickWebEngineScript::DocumentCreation2The script will be executed as soon as the document is created. This is not suitable for any DOM operation.
QQuickWebEngineScript::DocumentReady1The script will run as soon as the DOM is ready. This is equivalent to the DOMContentLoaded event firing in JavaScript.
QQuickWebEngineScript::Deferred0The 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. Besides these predefined IDs custom IDs can be used, but must be integers between 0 and 256.

ConstantValueDescription
QQuickWebEngineScript::MainWorld0The 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::ApplicationWorld1The default isolated world used for application level functionality implemented in JavaScript.
QQuickWebEngineScript::UserWorld2The 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:

QQuickWebEngineScript::InjectionPoint injectionPoint() const
void setInjectionPoint(QQuickWebEngineScript::InjectionPoint injectionPoint)[see note below]

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

Notifier signal:

void injectionPointChanged(QQuickWebEngineScript::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)[see note below]

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

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)[see note below]

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

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)[see note below]

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

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)[see note below]

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

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:

QQuickWebEngineScript::ScriptWorldId worldId() const
void setWorldId(QQuickWebEngineScript::ScriptWorldId scriptWorldId)[see note below]

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

Notifier signal:

void worldIdChanged(QQuickWebEngineScript::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.

Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.