Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-76173

Greasemonkey-like JavaScript injection does not work with HTML documents loaded from .qrc resources

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • 5.14
    • 5.12.3
    • WebEngine
    • None

    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

      1. At minimum, docs should be clear about this.
      2. A better solution is to allow qrc URL scheme in @include and @match directives.
      3. Perhaps the best soution is to make configurable which URL schemes are supported.

      Existing workarounds

      Currently, the easiest workarounds are:

      1. 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.
      2. Inline scripts in HTML document. However, this makes maintenance more difficult, and prevents script from being reused.
      3. 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

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            davidsz Szabolcs David
            skalee Sebastian
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes