WebEngineScript QML 类型

启用在 JavaScript 引擎中以编程方式注入脚本。 更多...

导入语句: import QtWebEngine 1.8
Since: QtWebEngine 1.1
Instantiates: QQuickWebEngineScript



WebEngineScript type enables the programmatic injection of so called 用户脚本 在 JavaScript 引擎中的不同点,确定通过 injectionPoint , during the loading of web content.

脚本可以执行于主 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. The worldId property provides some predefined IDs for this purpose.

下列 Greasemonkey 属性从 Qt 5.8 起支持: @exclude , @include , @name , @match ,和 @run-at .

使用 WebEngineView.userScripts to access a list of scripts attached to the web view.


injectionPoint : enumeration

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

常量 描述
WebEngineScript.DocumentCreation 文档被创建后尽快执行脚本。这不适合任何 DOM 操作。
WebEngineScript.DocumentReady DOM 就绪后尽快运行脚本。这相当于 DOMContentLoaded 事件激发于 JavaScript 中。
WebEngineScript.Deferred 当页面加载完成时,或文档就绪后 500 毫秒 (以先到的为准) 运行脚本。

name : string

The name of the script. Can be useful to retrieve a particular script from WebEngineView.userScripts .

runOnSubframes : int

把此特性设为 true 若脚本在页面每框架中执行,或 false if it is only ran for the main frame. The default value is false .

sourceCode : string

This property holds the JavaScript source code of the user script.

另请参阅 sourceUrl .

sourceUrl : url

This property holds the remote source location of the user script (if any).

不像 sourceCode , this property allows referring to user scripts that are not already loaded in memory, for instance, when stored on disk.

Setting this property will change the sourceCode of the script.

注意: At present, only file-based sources are supported.

另请参阅 sourceCode .

worldId : enumeration

The world ID defining which isolated world the script is executed in.

常量 描述
WebEngineScript.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.
WebEngineScript.ApplicationWorld The default isolated world used for application level functionality implemented in JavaScript.
WebEngineScript.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.