The QWebEngineScript class encapsulates a JavaScript program. More...
Header: | #include <QWebEngineScript> |
CMake: | find_package(Qt6 COMPONENTS WebEngineCore REQUIRED) target_link_libraries(mytarget PRIVATE Qt6::WebEngineCore) |
qmake: | QT += webenginecore |
Since: | Qt 5.5 |
enum | InjectionPoint { DocumentCreation, DocumentReady, Deferred } |
enum | ScriptWorldId { MainWorld, ApplicationWorld, UserWorld } |
QWebEngineScript(const QWebEngineScript &other) | |
QWebEngineScript() | |
QWebEngineScript & | operator=(const QWebEngineScript &other) |
~QWebEngineScript() | |
QWebEngineScript::InjectionPoint | injectionPoint() const |
QString | name() const |
bool | runsOnSubFrames() const |
void | setInjectionPoint(QWebEngineScript::InjectionPoint p) |
void | setName(const QString &scriptName) |
void | setRunsOnSubFrames(bool on) |
void | setSourceCode(const QString &scriptSource) |
void | setSourceUrl(const QUrl &url) |
void | setWorldId(quint32 id) |
QString | sourceCode() const |
QUrl | sourceUrl() const |
void | swap(QWebEngineScript &other) |
quint32 | worldId() const |
bool | operator!=(const QWebEngineScript &other) const |
bool | operator==(const QWebEngineScript &other) const |
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.
This enum describes the timing of the script injection:
Constant | Value | Description |
---|---|---|
QWebEngineScript::DocumentCreation |
2 |
The script will be executed as soon as the document is created. This is not suitable for any DOM operation. |
QWebEngineScript::DocumentReady |
1 |
The script will run as soon as the DOM is ready. This is equivalent to the DOMContentLoaded event firing in JavaScript. |
QWebEngineScript::Deferred |
0 |
The script will run when the page load finishes, or 500ms after the document is ready, whichever comes first. |
This enum provides pre-defined world IDs for isolating user scripts into different worlds:
Constant | Value | Description |
---|---|---|
QWebEngineScript::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. |
QWebEngineScript::ApplicationWorld |
1 |
The default isolated world used for application level functionality implemented in JavaScript. |
QWebEngineScript::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. |
Constructs a user script using the contents of other.
Constructs a null script.
Assigns other to the script.
Destroys a script.
Returns the point in the loading process at which the script will be executed. The default value is QWebEngineScript::Deferred.
Note: Getter function for property injectionPoint.
See also setInjectionPoint().
Returns the name of the script. Can be useful to retrieve a particular script from a QWebEngineScriptCollection.
Note: Getter function for property name.
See also setName() and QWebEngineScriptCollection::find().
Returns true
if the script is executed on every frame in the page, or false
if it is only ran for the main frame.
Note: Getter function for property runsOnSubFrames.
See also setRunsOnSubFrames().
Sets the point at which to execute the script to be p.
Note: Setter function for property injectionPoint.
See also injectionPoint() and InjectionPoint.
Sets the script name to scriptName.
Note: Setter function for property name.
See also name().
Executes the script on sub frames in addition to the main frame if on returns true
.
Note: Setter function for property runsOnSubFrames.
See also runsOnSubFrames().
Sets the script source to scriptSource.
Note: Setter function for property sourceCode.
See also sourceCode().
Sets the world ID of the isolated world to id when running this script.
Must be between 0
and 256
.
Note: Setter function for property worldId.
See also worldId().
Returns the source of the script.
Note: Getter function for property sourceCode.
See also setSourceCode().
Swaps the contents of the script with the contents of other.
Returns the world ID defining which world the script is executed in.
Note: Getter function for property worldId.
See also setWorldId().
Returns true
if the script is not equal to other, otherwise returns false
.
Returns true
if the script is equal to other, otherwise returns false
.
© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-6.2/qwebenginescript.html