On this page

QWebEngineScript Class

The QWebEngineScript class encapsulates a JavaScript program. More...

Header: #include <QWebEngineScript>
CMake: find_package(Qt6 REQUIRED COMPONENTS WebEngineCore)
target_link_libraries(mytarget PRIVATE Qt6::WebEngineCore)
qmake: QT += webenginecore
In QML: webEngineScript

Public Types

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

Properties

Public Functions

QWebEngineScript()
QWebEngineScript(const QWebEngineScript &other)
~QWebEngineScript()
QWebEngineScript::InjectionPoint injectionPoint() const
QString name() const
bool runsOnSubFrames() const
void setInjectionPoint(QWebEngineScript::InjectionPoint)
void setName(const QString &)
void setRunsOnSubFrames(bool on)
void setSourceCode(const QString &)
void setSourceUrl(const QUrl &url)
void setWorldId(quint32)
QString sourceCode() const
QUrl sourceUrl() const
void swap(QWebEngineScript &other)
quint32 worldId() const
bool operator!=(const QWebEngineScript &other) const
QWebEngineScript &operator=(const QWebEngineScript &other)
bool operator==(const QWebEngineScript &other) const

Detailed Description

QWebEngineScript enables the programmatic injection of so called user scripts in the JavaScript engine at different points, determined by injectionPoint(), during the loading of web contents.

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. ScriptWorldId provides some predefined IDs for this purpose.

The following Greasemonkey attributes are supported since Qt 5.8: @exclude, @include, @name, @match, and @run-at.

Use QWebEnginePage::scripts() and QWebEngineProfile::scripts() to access the collection of scripts associated with a single page or a number of pages sharing the same profile.

See also Script Injection.

Member Type Documentation

enum QWebEngineScript::InjectionPoint

This enum describes the timing of the script injection:

ConstantValueDescription
QWebEngineScript::DocumentCreation2The script will be executed as soon as the document is created. This is not suitable for any DOM operation.
QWebEngineScript::DocumentReady1The script will run as soon as the DOM is ready. This is equivalent to the DOMContentLoaded event firing in JavaScript.
QWebEngineScript::Deferred0The script will run when the page load finishes, or 500ms after the document is ready, whichever comes first.

enum QWebEngineScript::ScriptWorldId

This enum provides pre-defined world IDs for isolating user scripts into different worlds:

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

Access functions:

QWebEngineScript::InjectionPoint injectionPoint() const
void setInjectionPoint(QWebEngineScript::InjectionPoint)

See also InjectionPoint.

name : QString

This property holds the name of the script.

Can be useful to retrieve a particular script from a QWebEngineScriptCollection.

Access functions:

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

See also QWebEngineScriptCollection::find().

runsOnSubFrames : bool

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

Access functions:

bool runsOnSubFrames() const
void setRunsOnSubFrames(bool on)

sourceCode : QString

This property holds the source of the script.

Access functions:

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

sourceUrl : QUrl

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

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

Setting this value 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 also sourceCode.

worldId : quint32

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

Must be between 0 and 256.

Access functions:

quint32 worldId() const
void setWorldId(quint32)

Member Function Documentation

QWebEngineScript::QWebEngineScript()

Constructs a null script.

QWebEngineScript::QWebEngineScript(const QWebEngineScript &other)

Constructs a user script using the contents of other.

[noexcept] QWebEngineScript::~QWebEngineScript()

Destroys a script.

[noexcept] void QWebEngineScript::swap(QWebEngineScript &other)

Swaps the contents of the script with the contents of other.

bool QWebEngineScript::operator!=(const QWebEngineScript &other) const

Returns true if the script is not equal to other, otherwise returns false.

QWebEngineScript &QWebEngineScript::operator=(const QWebEngineScript &other)

Assigns other to the script.

bool QWebEngineScript::operator==(const QWebEngineScript &other) const

Returns true if the script is equal to other, otherwise returns false.

© 2025 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.