Problems with injecting js-code on first load
Hi, I'm trying to inject javascript code on each page load but it fails on the first load every time. If I reload the page it works. How do I fix this? Is there something I've missed in the code below? The printfs work but the js-code does not get injected. #include <gtk/gtk.h> #include <webkit/webkit.h> GtkWidget *window; GtkWidget *webview; static void handle_script_message(WebKitUserContentManager *manager, JSCValue* value, gpointer user_data) { printf("handle_script_message()%s\n", value); } static void web_view_load_changed(WebKitWebView *web_view, WebKitLoadEvent load_event, gpointer user_data) { switch(load_event) { case WEBKIT_LOAD_FINISHED: printf("WEBKIT_LOAD_FINISHED %s\n", webkit_web_view_get_uri(web_view)); WebKitUserScript *script = webkit_user_script_new( "window.test = 12345; console.log(window.test);", WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END, NULL, NULL ); WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager(web_view); webkit_user_content_manager_add_script( manager, script ); webkit_user_script_unref(script); printf("SCRIPTS LOADED\n"); break; } } static void activate(GtkApplication* app, gpointer user_data) { window = gtk_application_window_new (app); gtk_window_set_title(GTK_WINDOW (window), "Webkit Test"); gtk_window_set_default_size(GTK_WINDOW (window), 800, 600); // Create a browser instance webview = webkit_web_view_new(); WebKitSettings *settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW(webview)); g_object_set(G_OBJECT(settings), "enable-developer-extras", TRUE, NULL); WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager(WEBKIT_WEB_VIEW(webview)); g_signal_connect(manager, "script-message-received::testjs", G_CALLBACK(handle_script_message), NULL); webkit_user_content_manager_register_script_message_handler(manager, "testjs", NULL); g_signal_connect(webview, "load-changed", G_CALLBACK(web_view_load_changed), NULL); webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), "https://mytestserver"); gtk_window_set_child(GTK_WINDOW (window), webview); gtk_window_present(GTK_WINDOW(window)); WebKitWebInspector *inspector = webkit_web_view_get_inspector (WEBKIT_WEB_VIEW(webview)); webkit_web_inspector_show (WEBKIT_WEB_INSPECTOR(inspector)); } int main(int argc, char *argv[]) { GtkApplication *app; int status; app = gtk_application_new ("my.test.app", G_APPLICATION_DEFAULT_FLAGS); g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return status; } Regards, Erik
On Thu, Aug 29 2024 at 06:41:45 AM +00:00:00, Erik Lundin via webkit-dev <webkit-dev@lists.webkit.org> wrote:
I’m trying to inject javascript code on each page load but it fails on the first load every time. If I reload the page it works. How do I fix this? Is there something I’ve missed in the code below?
You're injecting the script after the load has already finished, so it's too late. Try injecting it when creating the web view instead.
participants (2)
-
Erik Lundin
-
Michael Catanzaro