class QWebEngineScript#

The QWebEngineScript class encapsulates a JavaScript program. More

Synopsis#

Properties#

Methods#

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

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 scripts() and 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

class InjectionPoint#

This enum describes the timing of the script injection:

Constant

Description

QWebEngineScript.DocumentCreation

The script will be executed as soon as the document is created. This is not suitable for any DOM operation.

QWebEngineScript.DocumentReady

The script will run as soon as the DOM is ready. This is equivalent to the DOMContentLoaded event firing in JavaScript.

QWebEngineScript.Deferred

The script will run when the page load finishes, or 500ms after the document is ready, whichever comes first.

class ScriptWorldId#

(inherits enum.IntEnum) This enum provides pre-defined world IDs for isolating user scripts into different worlds:

Constant

Description

QWebEngineScript.MainWorld

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

The default isolated world used for application level functionality implemented in JavaScript.

QWebEngineScript.UserWorld

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.

Note

Properties can be used directly when from __feature__ import true_property is used or via accessor functions otherwise.

property injectionPointᅟ: QWebEngineScript.InjectionPoint#
Access functions:
property nameᅟ: str#
Access functions:
property runsOnSubFramesᅟ: bool#
Access functions:
property sourceCodeᅟ: str#
Access functions:
property sourceUrlᅟ: QUrl#
Access functions:
property worldIdᅟ: int#
Access functions:
__init__(other)#
Parameters:

otherQWebEngineScript

Constructs a user script using the contents of other.

__init__()

Constructs a null script.

injectionPoint()#
Return type:

InjectionPoint

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

Getter of property injectionPointᅟ .

name()#
Return type:

str

Returns the name of the script. Can be useful to retrieve a particular script from a QWebEngineScriptCollection .

See also

setName() find()

Getter of property nameᅟ .

__ne__(other)#
Parameters:

otherQWebEngineScript

Return type:

bool

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

__eq__(other)#
Parameters:

otherQWebEngineScript

Return type:

bool

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

runsOnSubFrames()#
Return type:

bool

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

Getter of property runsOnSubFramesᅟ .

setInjectionPoint(arg__1)#
Parameters:

arg__1InjectionPoint

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

Setter of property injectionPointᅟ .

setName(arg__1)#
Parameters:

arg__1 – str

Sets the script name to scriptName.

See also

name()

Setter of property nameᅟ .

setRunsOnSubFrames(on)#
Parameters:

on – bool

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

Setter of property runsOnSubFramesᅟ .

setSourceCode(arg__1)#
Parameters:

arg__1 – str

Sets the script source to scriptSource.

See also

sourceCode()

Setter of property sourceCodeᅟ .

setSourceUrl(url)#
Parameters:

urlQUrl

See also

sourceUrl()

Setter of property sourceUrlᅟ .

setWorldId(arg__1)#
Parameters:

arg__1 – int

Sets the world ID of the isolated world to id when running this script.

Must be between 0 and 256.

See also

worldId()

Setter of property worldIdᅟ .

sourceCode()#
Return type:

str

Returns the source of the script.

See also

setSourceCode()

Getter of property sourceCodeᅟ .

sourceUrl()#
Return type:

QUrl

See also

setSourceUrl()

Getter of property sourceUrlᅟ .

swap(other)#
Parameters:

otherQWebEngineScript

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

worldId()#
Return type:

int

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

See also

setWorldId()

Getter of property worldIdᅟ .