[wpe-webkit] Use WebKit-WPE Web Extensions to append custom <scipt> element to HTML DOM

Michael Pantazoglou michael.pantazoglou at oceanbluesoftware.co.uk
Wed Jul 28 01:46:58 PDT 2021

Hi Adrian,

Thanks for the feedback. I was wondering how the WebKitUserContentManager
can be used within the Web extension. My code flow is currently as follows:

1. Connect to the window-object-cleared signal:

void webkit_web_extension_initialize(WebKitWebExtension *extension) {
    G_CALLBACK (OnWindowObjectCleared),

2. Implement the callback:

static void OnWindowObjectCleared(WebKitScriptWorld *world,
    WebKitWebPage *page,
    WebKitFrame *frame,
    gpointer userData)
  /* Here I should be able to inject the custom <script> element */

I could not find any way to retrieve the WebKitUserContentManager from
either the WebKitWebPage nor the WebKitFrame. It seems it is only possible
to do so from the WebKitWebView, which however I don't have access to. Am I
missing something here?

Thanks and regards,

On Tue, 27 Jul 2021 at 17:40, Adrian Perez de Castro <aperez at igalia.com>

> Hi Michael,
> On Tue, 27 Jul 2021 10:36:46 +0300 Michael Pantazoglou <
> michael.pantazoglou at oceanbluesoftware.co.uk> wrote:
> > I have recently started working with the WebKit-WPE Web Extensions API to
> > develop some additional functionality for the WPE browser. In
> particular, I
> > need to 'inject' a custom <script> element to the HTML DOM *before* the
> > latter is loaded/processed by the WebPage/WebFrame.
> >
> > As far as I have seen, the WebKit-WPE Extensions API allows for executing
> > custom JavaScript as well as exposing native code to JS via the JSC API,
> in
> > response to the window-object-cleared signal. However, those techniques
> do
> > not serve my needs, because my custom <script> element executes JS code
> > that needs the DOM document and document.documentElement to exist and be
> > fully populated in advance.
> The window-cleared-signal is intentionally triggered early during the
> initialization of the JS context (hence the “cleared” in its name: it
> will have a very minimal set of contents), before loaded content has had
> any chance to touch it.
> > So, I was wondering if the WebKit-WPE Extensions API provides any other
> > signal that I could connect to and respond by altering the HTML DOM tree
> > -or the raw network reply- before it is passed to the
> > WebPage/WebFrame/HTMLDocumentParser. Has anybody else tried anything
> > similar in the past? What is the best way to move forward besides
> patching
> > WebKit itself?
> The public API does not provide a way of replacing the raw network replies.
> You can probably achieve your goal by using an injected script, see:
> https://webkitgtk.org/reference/webkit2gtk/stable/WebKitUserContentManager.html#webkit-user-content-manager-add-script
> https://webkitgtk.org/reference/webkit2gtk/stable/webkit2gtk-4.0-WebKitUserContent.html#webkit-user-script-new
> I hope this helps :)
> Cheers,
> -Adrian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-wpe/attachments/20210728/aec1cf1f/attachment.htm>

More information about the webkit-wpe mailing list