PySide6.QtWebEngineCore.QWebEngineScript¶
- class QWebEngineScript¶
The
QWebEngineScriptclass encapsulates a JavaScript program.Details
QWebEngineScriptenables the programmatic injection of so called user scripts in the JavaScript engine at different points, determined byinjectionPoint(), 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.
ScriptWorldIdprovides some predefined IDs for this purpose.The following Greasemonkey attributes are supported:
@exclude,@include,@name,@match, and@run-at.Use
scripts()andscripts()to access the collection of scripts associated with a single page or a number of pages sharing the same profile.See also
Synopsis¶
Properties¶
injectionPointᅟ- Point in the loading process at which the script will be executednameᅟ- Of the scriptrunsOnSubFramesᅟ- Whether the script is executed on every frame in the page, or only on the main framesourceCodeᅟ- Source of the scriptsourceUrlᅟ- Remote source location of the user script (if any)worldIdᅟ- World ID defining which world the script is executed in
Methods¶
def
__init__()def
injectionPoint()def
name()def
__ne__()def
__eq__()def
setName()def
setSourceCode()def
setSourceUrl()def
setWorldId()def
sourceCode()def
sourceUrl()def
swap()def
worldId()
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
- class InjectionPoint¶
This enum describes the timing of the script injection:
Constant
Description
QWebEngineScript.InjectionPoint.DocumentCreation
The script will be executed as soon as the document is created. This is not suitable for any DOM operation.
QWebEngineScript.InjectionPoint.DocumentReady
The script will run as soon as the DOM is ready. This is equivalent to the
DOMContentLoadedevent firing in JavaScript.QWebEngineScript.InjectionPoint.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.ScriptWorldId.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.ScriptWorldId.ApplicationWorld
The default isolated world used for application-level functionality implemented in JavaScript.
QWebEngineScript.ScriptWorldId.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_propertyis used or via accessor functions otherwise.- property injectionPointᅟ: QWebEngineScript.InjectionPoint¶
This property holds The point in the loading process at which the script will be executed..
The default value is
Deferred.See also
- Access functions:
- property nameᅟ: str¶
This property holds The name of the script..
Can be useful to retrieve a particular script from a
QWebEngineScriptCollection.See also
- property runsOnSubFramesᅟ: bool¶
This property holds Whether the script is executed on every frame in the page, or only on the main frame..
- Access functions:
- property sourceCodeᅟ: str¶
This property holds The source of the script..
- Access functions:
This property holds The remote source location of the user script (if any)..
Unlike
setSourceCode(), setting this property allows referring to user scripts that are not already loaded in memory, for instance, when stored on disk.Setting this value will change the
sourceCodeof the script.- Access functions:
- property worldIdᅟ: int¶
This property holds The world ID defining which world the script is executed in..
Must be between
0and256.- Access functions:
- __init__()¶
Constructs a null script.
- __init__(other)
- Parameters:
other –
QWebEngineScript
Constructs a user script using the contents of
other.- injectionPoint()¶
- Return type:
See also
Getter of property
injectionPointᅟ.Getter of property
nameᅟ.- __ne__(other)¶
- Parameters:
other –
QWebEngineScript- Return type:
bool
Returns
trueif the script is not equal toother, otherwise returnsfalse.- __eq__(other)¶
- Parameters:
other –
QWebEngineScript- Return type:
bool
Returns
trueif the script is equal toother, otherwise returnsfalse.- runsOnSubFrames()¶
- Return type:
bool
See also
Getter of property
runsOnSubFramesᅟ.- setInjectionPoint(arg__1)¶
- Parameters:
arg__1 –
InjectionPoint
See also
Setter of property
injectionPointᅟ.Setter of property
nameᅟ.- setRunsOnSubFrames(on)¶
- Parameters:
on – bool
See also
Setter of property
runsOnSubFramesᅟ.- setSourceCode(arg__1)¶
- Parameters:
arg__1 – str
See also
Setter of property
sourceCodeᅟ.Setter of property
sourceUrlᅟ.Setter of property
worldIdᅟ.- sourceCode()¶
- Return type:
str
See also
Getter of property
sourceCodeᅟ.- sourceUrl()¶
- Return type:
See also
Getter of property
sourceUrlᅟ.- swap(other)¶
- Parameters:
other –
QWebEngineScript
Swaps the contents of the script with the contents of
other.- worldId()¶
- Return type:
int
See also
Getter of property
worldIdᅟ.