[webkit-gtk] Proper way to load javascript

Daniel Berek berekdaniel at gmail.com
Sun Mar 3 04:27:24 PST 2019


Awesome, thanks!

On Sun, 3 Mar 2019, 13:11 Adrian Perez de Castro, <aperez at igalia.com> wrote:

> Hello Daniel,
>
> On Sun, 03 Mar 2019 09:04:03 +0100, Daniel Berek <berekdaniel at gmail.com>
> wrote:
>
> > Hi, I am using gtkmm-3.0 and the new webkit2gtk-4.0 (where the
> javascript is
> > running on a new web process).
> >
> > I have two problems to solve, which connecting together.
> >
> > 1. At first, I am not sure whats the proper way to load an external
> > javascript file. In this version of webkit, the <script
> > src="script.js"></script> tag from html file doesnt work, even the html
> cant
> > be loaded.  So I have to read the 'script.js' manually, and after each
> > webkit_web_view_load_html() I have to execute
> > webkit_web_view_run_javascript().  Is there a more sophisticated method
> to
> > load external javascript? ex loading once, so all html sites could use
> it?
>
> This sounds like you would want to have your JavaScript file loaded (and
> available) to all the web pages loaded in the Web view. The recommended
> way of
> achieving that is loading the script once with “webkit_user_script_new()”
> or
> “webkit_user_script_new_for_world()” [1], then you would add it to a
> WebKitUserContentManager [2] (which you are already using in you example
> code), and finally make sure that your web view makes use of your user
> content
> manager (WebKitWebView has a “user-content-manager” property [3]). Then
> WebKit
> will automatically inject your JavaScript code into loaded pages.
>
> > 2. I have a webview widget (described at the end of this message) and I
> > periodically called its destructor (default destructor) to get rid off
> the
> > cached data and made a new instance. Unfortunately, deleting the webview
> > widget leaks almost 400kB of memory. So I stopped calling its destructor,
> > which is perfectly fine for me (no more leaks, I cant even see the
> > increasing cache) but after a few javascript execution, my functions in
> the
> > external 'script.js' file doesnt work properly, athough
> > the run_javascript_finished_discard_result() doesnt show any error.
> >  It seems, the javascript process cant handle that much executions.
>
> It should be fine to call the same JavaScript repeatedly. But I still
> recommend you to use WebKitUserContentManager to inject your JavaScript
> code
> because it *will* make your life easier :)
>
> > Somebody knows whats the proper way for deleting a webview widget
> > programically?
> > Or any help is appreciated.
>
> I think destructing the WebKitWebView should correctly deallocate all the
> memory used, those 400 KiB of memory lost are suspicious. Are you sure that
> there are no other references to the widget (installed signal callbacks,
> for
> example)? Though it might be a problem inside WebKitGTK itself, dunno…
>
> Which kind of “cached data” do you need to get rid of exactly? You may be
> able
> to keep your Web view around, with the user content manager taking care of
> injecting your JavaScript code, and then using WebKitWebsiteDataManager to
> clear up caches and other transient data.
>
> Hopefully these tips will help you out a bit :)
>
> Cheers,
>
>
> -Adrián
>
> ---
> [1]
> https://webkitgtk.org/reference/webkit2gtk/stable/webkit2gtk-4.0-WebKitUserContent.html#webkit-user-script-new
> [2]
> https://webkitgtk.org/reference/webkit2gtk/stable/WebKitUserContentManager.html#webkit-user-content-manager-add-script
> [3]
> https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#WebKitWebView--user-content-manager
> [4]
> https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebsiteDataManager.html
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-gtk/attachments/20190303/1eac0af0/attachment-0001.html>


More information about the webkit-gtk mailing list