[webkit-gtk] webkit_web_view_run_javascript won't run if excuted after gtk_main

MrBn100ful mrbn100ful at neonroot.net
Wed Mar 24 10:12:00 PDT 2021


Hi,

I am trying to make a simple application that warp a web page and use some javascript to make i't run properly on a phone screen ( a pinephone ).

Right now i am trying to make a simple program that run some basic JavaScript in the DOM. the script 'alert("yes!")' run perfectly if it's run before the gtk_main(); but not after.
I get the error message in the log: void webkit_web_view_run_javascript(WebKitWebView*, const gchar*, GCancellable*, GAsyncReadyCallback, gpointer): assertion 'WEBKIT_IS_WEB_VIEW(webView)' failed
I have used this as a source: https://wiki.gnome.org/Projects/WebKitGtk/ProgrammingGuide/Cookbook#Executing_JavaScript
I am still new to C/GTK/WEBKIT, i am having a hard time understanding how all of this works together and what to make of this error.

From what i was able to understand WebKitScript would be a better way to do it but i am even more clueless on how it work.
There used to be example on here https://github.com/LeoUfimtsev/LeoGtk3, but it's gone.

I also found this in the mailing list but can't get the code working:
https://lists.webkit.org/pipermail/webkit-gtk/2019-March/003460.html

I know you can also do it with Webextension, but that's not clear to me:
https://lists.webkit.org/pipermail/webkit-gtk/2017-March/002975.html

If any of you have some basic code on how to do it. It would be awesome !

Sorry if my English is not the best.
Best regardes,
MrBn100ful.

Here is the code that i used:

#include <glib/gi18n.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>

static void destroyWinCb(GtkWidget* widget, GtkWidget* window)
{
gtk_main_quit();
}

static gboolean closeWebCb(WebKitWebView* webView, GtkWidget* window)
{
gtk_widget_destroy(window);

return TRUE;
}

int main(int argc, char* argv[])
{
GtkWidget *win;
WebKitWebView *web;
gchar *script;
gchar * url = "https://google.fr";

gtk_init(&argc, &argv);

if (argc == 2) url = argv[1];

win = gtk_window_new(GTK_WINDOW_TOPLEVEL);

gtk_window_set_default_size(GTK_WINDOW(win), 360, 720);

web = WEBKIT_WEB_VIEW(webkit_web_view_new());

gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(web));

g_signal_connect(win, "destroy", G_CALLBACK(destroyWinCb), NULL);
g_signal_connect(web, "close", G_CALLBACK(closeWebCb), win);

webkit_web_view_load_uri(web, url);

gtk_widget_show_all(win);

script = g_strdup_printf ("alert('yes!');", url);

webkit_web_view_run_javascript (web, script, NULL, NULL, NULL);

gtk_main();

webkit_web_view_run_javascript (web, script, NULL, NULL, NULL);

g_free (script);

return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-gtk/attachments/20210324/c5094eff/attachment.htm>


More information about the webkit-gtk mailing list