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

Adrian Perez de Castro aperez at igalia.com
Wed Jul 28 03:59:31 PDT 2021


Hello Michael,

On Wed, 28 Jul 2021 11:46:58 +0300 Michael Pantazoglou <michael.pantazoglou at oceanbluesoftware.co.uk> wrote:
 
> 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_signal_connect(webkit_script_world_get_default(),
>     "window-object-cleared",
>     G_CALLBACK (OnWindowObjectCleared),
>     NULL);
> }
> 
> 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?

The WebKitUserContentManager needs to be used from the UI process (that is:
your browser process, where the WebKitWebView lives as well). I was under
the impression that you were working on your own browser. I am out of ideas
about how to achieve the same from a WebExtension, sorry. Maybe someone else
in the mailing list has some other ideas ��

Cheers,
-Adrian
 
> On Tue, 27 Jul 2021 at 17:40, Adrian Perez de Castro <aperez at igalia.com>
> wrote:
> 
> > 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 --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
URL: <http://lists.webkit.org/pipermail/webkit-wpe/attachments/20210728/c1b0de10/attachment.bin>


More information about the webkit-wpe mailing list