IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QWebEngineScript Class

The QWebEngineScript class encapsulates a JavaScript program.

This class was introduced in Qt 5.5.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QWebEngineScript Class

  • Header: QWebEngineScript

  • Since: Qt 5.5

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS WebEngineCore)

    target_link_libraries(mytarget PRIVATE Qt6::WebEngineCore)

  • qmake: QT += webenginecore

  • Inherited By:

  • Instantiated By: qml-qtwebengine-webenginescript.xml

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.

See Also

See also Script Injection

Member Type Documentation

 

enum QWebEngineScript::InjectionPoint

This enum describes the timing of the script injection:

Constant

Value

Description

QWebEngineScript::DocumentCreation

2

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

QWebEngineScript::DocumentReady

1

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

QWebEngineScript::Deferred

0

The 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:

Constant

Value

Description

QWebEngineScript::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.

QWebEngineScript::ApplicationWorld

1

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

QWebEngineScript::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.

Member Function Documentation

 

QWebEngineScript::QWebEngineScript()

Constructs a null script.

QWebEngineScript::QWebEngineScript(const QWebEngineScript &other)

Constructs a user script using the contents of other.

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.

Getter function for property injectionPoint.

See Also

See also setInjectionPoint()

QString QWebEngineScript::name() const

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

Getter function for property name.

See Also

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.

Getter function for property runsOnSubFrames.

See Also

See also setRunsOnSubFrames()

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

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

Setter function for property injectionPoint.

See Also

void QWebEngineScript::setName(const QString &scriptName)

Sets the script name to scriptName.

Setter function for property name.

See Also

See also name()

void QWebEngineScript::setRunsOnSubFrames(bool on)

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

Setter function for property runsOnSubFrames.

See Also

See also runsOnSubFrames()

void QWebEngineScript::setSourceCode(const QString &scriptSource)

Sets the script source to scriptSource.

Setter function for property sourceCode.

See Also

See also sourceCode()

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.

Setter function for property worldId.

See Also

See also worldId()

QString QWebEngineScript::sourceCode() const

Returns the source of the script.

Getter function for property sourceCode.

See Also

See also setSourceCode()

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.

Getter function for property worldId.

See Also

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.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+