QWebEngineScript Class

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

Header: #include <QWebEngineScript>
qmake: QT += webenginewidgets
Since: Qt 5.5

Public Types

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

Public Functions

QWebEngineScript()
QWebEngineScript(const QWebEngineScript & other)
~QWebEngineScript()
InjectionPoint injectionPoint() const
bool isNull() const
QString name() const
bool runsOnSubFrames() const
void setInjectionPoint(InjectionPoint p)
void setName(const QString & scriptName)
void setRunsOnSubFrames(bool on)
void setSourceCode(const QString & scriptSource)
void setWorldId(quint32 id)
QString sourceCode() 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

The QWebEngineScript class encapsulates a JavaScript program.

QWebEngineScript allows 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 a function defined in one world are not accessible from a different one. ScriptWorldId provides some predefined ids for this purpose.

Member Type Documentation

enum QWebEngineScript::InjectionPoint

This enum describes the timing for when the script injection should happen.

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 thumbs, if that functionality is exposed to the application users, each individual script should probably get its own isolated world.

Member Function Documentation

QWebEngineScript::QWebEngineScript()

QWebEngineScript::QWebEngineScript

Constructs a null script.

QWebEngineScript::QWebEngineScript(const QWebEngineScript & other)

QWebEngineScript::isNull Returns true is the script is null, false otherwise.

QWebEngineScript::~QWebEngineScript()

InjectionPoint QWebEngineScript::injectionPoint() const

QWebEngineScript::injectionPoint Returns the point in the loading process at which the script will be executed. The default value is QWebEngineScript::Deferred.

See also setInjectionPoint.

bool QWebEngineScript::isNull() const

QString QWebEngineScript::name() const

QWebEngineScript::name Returns The name of the script. Can be useful to retrieve a given script from a QWebEngineScriptCollection.

See also setName(), QWebEngineScriptCollection::findScript(), and QWebEngineScriptCollection::findScripts().

bool QWebEngineScript::runsOnSubFrames() const

QWebEngineScript::runsOnSubFrames Returns true if the script is executed on every frame in the page, false if it is only ran for the main frame.

See also setRunsOnSubFrames().

void QWebEngineScript::setInjectionPoint(InjectionPoint p)

QWebEngineScript::setInjectionPoint p Sets the point at which to execute the script to be \p.

See also injectionPoint() and QWebEngineScript::InjectionPoint.

void QWebEngineScript::setName(const QString & scriptName)

QWebEngineScript::setName scriptName

Sets the script name to scriptName.

See also name().

void QWebEngineScript::setRunsOnSubFrames(bool on)

QWebEngineScript::setRunsOnSubFrames on Sets whether or not the script is executed on sub frames in addition to the main frame.

See also runsOnSubFrames().

void QWebEngineScript::setSourceCode(const QString & scriptSource)

QWebEngineScript::setSourceCode scriptSource Sets the script source to scriptSource.

See also sourceCode().

void QWebEngineScript::setWorldId(quint32 id)

QWebEngineScript::setWorldId id Sets the world id of the isolated world to use when running this script.

See also worldId().

QString QWebEngineScript::sourceCode() const

QWebEngineScript::sourceCode Returns the source of the script.

See also setSourceCode().

void QWebEngineScript::swap(QWebEngineScript & other)

quint32 QWebEngineScript::worldId() const

QWebEngineScript::worldId Returns the world id defining which world the script is executed in.

See also setWorldId().

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

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

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

QWebEngineScript::operator == other Returns true if this QWebEngineScript is equal to other, otherwise returns false.

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