Description
Background
Since Qt 5.8, QtWebEngine supports injecting scripts to HTML documents in a Greasemonkey way, for example:
// ==UserScript== // @include http://*.qt.io/* // @exclude http://wiki.qt.io/* // ==/UserScript== // // alert("a script was injected to some pages")
Problem description
However, injection does not work with pages loaded from .qrc resources. For example, given QWebEngine instance loads an HTML document from qrc:/res/page.html, following script will not be injected, despite of variety of @include and @match directives:
// ==UserScript== // @match qrc:///res/page.html* // @match qrc://res/page.html* // @match qrc:/res/page.html* // @match * // @include qrc:///res/page.html* // @include qrc://res/page.html* // @include qrc:/res/page.html* // @include * // @run-at document-end // ==/UserScript== alert("it works")
It appears that this way of injection does not work with qrc URL scheme at all.
Original Greasemonkey also whitelists only certain URL shemes by default, however this is configurable: https://wiki.greasespot.net/Include_and_exclude_rules#Greaseable_schemes.
Expected resolution
- At minimum, docs should be clear about this.
- A better solution is to allow qrc URL scheme in @include and @match directives.
- Perhaps the best soution is to make configurable which URL schemes are supported.
Existing workarounds
Currently, the easiest workarounds are:
- Inject scripts with C++ API. However it adds maintenance burden, especially if some scripts are loaded in a Greasemonkey way, whereas others via C++ API.
- Inline scripts in HTML document. However, this makes maintenance more difficult, and prevents script from being reused.
- Remove ==UserScript== header, and filter pages in JavaScript by constructs like if (location === ...). This is also harder to maintain, and may come with some performance penalty.
Attachments
For Gerrit Dashboard: QTBUG-76173 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
273469,2 | Support qrc protocol in UrlPattern | 75-based | qt/qtwebengine-chromium | Status: MERGED | +2 | 0 |
273470,9 | Support qrc protocol in @match rules of user scripts | 5.14 | qt/qtwebengine | Status: MERGED | +2 | 0 |