QWebEngineScript — PyQt Documentation v5.15.7 (original) (raw)

PyQt5.QtWebEngineWidgets.QWebEngineScript

Description

The QWebEngineScript class encapsulates a JavaScript program.

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 scripts() and QWebEngineProfile::scripts() to access the collection of scripts associated with a single page or a number of pages sharing the same profile.

Enums

InjectionPoint

This enum describes the timing of the script injection:

Member Value Description
Deferred 0 The script will run when the page load finishes, or 500ms after the document is ready, whichever comes first.
DocumentCreation 2 The script will be executed as soon as the document is created. This is not suitable for any DOM operation.
DocumentReady 1 The script will run as soon as the DOM is ready. This is equivalent to the DOMContentLoaded event firing in JavaScript.

ScriptWorldId

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

Member Value Description
ApplicationWorld 1 The default isolated world used for application level functionality implemented in JavaScript.
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.
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.

Methods

__init__()

TODO


__init__(QWebEngineScript)

TODO


__eq__(QWebEngineScript) → bool

TODO


injectionPoint() → InjectionPoint

See also

setInjectionPoint().


isNull() → bool

TODO


name() → str


__ne__(QWebEngineScript) → bool

TODO


runsOnSubFrames() → bool

See also

setRunsOnSubFrames().


setInjectionPoint(InjectionPoint)

See also

injectionPoint().


setName(str)


setRunsOnSubFrames(bool)

See also

runsOnSubFrames().


setSourceCode(str)


setWorldId(int)


sourceCode() → str

See also

setSourceCode().


swap(QWebEngineScript)

TODO


worldId() → int