autocmd: implement new LoadStarting event
authorPatrick Steinhardt <ps@pks.im>
Sat, 14 Sep 2019 09:46:35 +0000 (11:46 +0200)
committerPatrick Steinhardt <ps@pks.im>
Mon, 11 Nov 2019 13:39:45 +0000 (14:39 +0100)
The first autocommand event that is being triggered when loading a
website is the "LoadStarted" event. LoadStarted gets triggered when the
web view has started loading of the page, notably after the initial
request has been sent. Thus, this event comes too late to change
settings that would modify the initial request, like e.g. the user
agent.

Implement a new event LoadStarting that triggers immediately before
performing the initial load and thus before LoadStarted. As WebKit does
not provide any signal for this, we have to manually trigger this event
when executing any load or navigation actions. The best place to
piggy-back on WebKit itself is in fact `decide_navigation_action`, which
will get executed on clicks, history navigation and `load_uri`. Like
this, there is only a single location that needs to trigger the new
event.

This change enables one to modify configuration like the user agent for
certain websites, which was not possible with "LoadStarted".

doc/vimb.1
src/autocmd.c
src/autocmd.h
src/main.c

index e86d561..7d50fd6 100644 (file)
@@ -709,6 +709,10 @@ No white space can be used in this list.
 .PD 0
 Events:
 .TP
+.B LoadStarting
+Fired before a new page is going to be opened.
+No data has been sent or received yet, the load may still fail for transport issues.
+.TP
 .B LoadStarted
 Fired if a new page is going to be opened.
 No data has been received yet, the load may still fail for transport issues.
index 823b895..860a92f 100644 (file)
@@ -44,15 +44,16 @@ static struct {
     const char *name;
     guint      bits;
 } events[] = {
-    {"*",                0x00ff},
-    {"LoadStarted",      0x0001},
-    {"LoadCommitted",    0x0002},
-    /*{"LoadFirstLayout",  0x0004},*/
-    {"LoadFinished",     0x0008},
-    /*{"LoadFailed",       0x0010},*/
-    {"DownloadStarted",  0x0020},
-    {"DownloadFinished", 0x0040},
-    {"DownloadFailed",   0x0080},
+    {"*",                0x01ff},
+    {"LoadStarting",     0x0001},
+    {"LoadStarted",      0x0002},
+    {"LoadCommitted",    0x0004},
+    /*{"LoadFirstLayout",  0x0008},*/
+    {"LoadFinished",     0x0010},
+    /*{"LoadFailed",       0x0020},*/
+    {"DownloadStarted",  0x0040},
+    {"DownloadFinished", 0x0080},
+    {"DownloadFailed",   0x0100},
 };
 
 static GSList *get_group(Client *c, const char *name);
index 94e3873..6828c68 100644 (file)
@@ -28,6 +28,7 @@
 /* this values correspond to indices in events[] array in autocmd.c */
 typedef enum {
     AU_ALL,
+    AU_LOAD_STARTING,
     AU_LOAD_STARTED,
     AU_LOAD_COMMITTED,
     /*AU_LOAD_FIRST_LAYOUT,*/
index 3d7b8ca..d6ace19 100644 (file)
@@ -1374,6 +1374,10 @@ static void decide_navigation_action(Client *c, WebKitPolicyDecision *dec)
         webkit_policy_decision_ignore(dec);
         spawn_new_instance(uri);
     } else {
+#ifdef FEATURE_AUTOCMD
+        if (strcmp(uri, "about:blank"))
+            autocmd_run(c, AU_LOAD_STARTING, uri, NULL);
+#endif
         webkit_policy_decision_use(dec);
     }
 }