QWebEngineScript Class | Qt WebEngine (original) (raw)

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.

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.

QWebEngineScript::InjectionPoint QWebEngineScript::injectionPoint() const

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().

QString QWebEngineScript::name() const

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().

bool QWebEngineScript::runsOnSubFrames() const

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().

void QWebEngineScript::setInjectionPoint(QWebEngineScript::InjectionPoint p)

Sets the point at which to execute the script to be p.

See also injectionPoint() and InjectionPoint.

void QWebEngineScript::setName(const QString &scriptName)

Sets the script name to scriptName.

Note: Setter function for property name.

See also name().

void QWebEngineScript::setRunsOnSubFrames(bool on)

Executes the script on sub frames in addition to the main frame if on returns true.

See also runsOnSubFrames().

void QWebEngineScript::setSourceCode(const QString &scriptSource)

Sets the script source to scriptSource.

Note: Setter function for property sourceCode.

See also sourceCode().

void QWebEngineScript::setSourceUrl(const QUrl &url)

Sets the remote source location of the user script to url.

Unlike setSourceCode(), this function 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.

Note: Setter function for property sourceUrl.

See also sourceUrl() and setSourceCode().

void QWebEngineScript::setWorldId(quint32 id)

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().

QString QWebEngineScript::sourceCode() const

Returns the source of the script.

Note: Getter function for property sourceCode.

See also setSourceCode().

QUrl QWebEngineScript::sourceUrl() const

Returns the remote source location of the user script (if any).

Note: Getter function for property sourceUrl.

See also setSourceUrl().

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

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

quint32 QWebEngineScript::worldId() const

Returns the world ID defining which world the script is executed in.

Note: Getter function for property worldId.

See also setWorldId().

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.