From 38fd1f02bbdc5edda7daf8210a0d2b241bc9b412 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Thu, 27 Oct 2011 12:06:16 -0400 Subject: add request_handler hook This creates a handler `request_handler' in the same vein as scheme_handler, download_handler, etc., which is called whenever the page requests a URI. (Possible applications include Adblock and SSL enforcement.) The handler should get the URI from $1 and print the URI of the resource that should be loaded instead, or print nothing to allow the request unmodified. --- README | 12 ++++++++++-- examples/config/config | 1 + src/callbacks.c | 24 ++++++++++++++++++++++++ src/uzbl-core.h | 1 + src/variables.c | 1 + 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/README b/README index 925392c..ab58ab2 100644 --- a/README +++ b/README @@ -313,6 +313,9 @@ file). - `data`: The cookie data. Only included for "PUT" requests. * `scheme_handler`: handler to execute for each URI navigated to - the navigation request will be ignored if handler prints "USED\n" +* `request_handler`: Executed whenever any request is made. The handler can + print a URI to redirect the request (or `about:blank` to effectively cancel it). + If the handler does nothing, the request will continue unchanged. * `download_handler`: executed when a download is started. the handler script should print a path that the download should be saved to, or print nothing to cancel the download. @@ -507,8 +510,9 @@ access to the following environment variables: * `$UZBL_URI`: The URI of the current page. * `$UZBL_TITLE`: The current page title. -Handler scripts (`download_handler`, `cookie_handler`, `scheme_handler` and -`authentication_handler`) are called with special arguments: +Handler scripts (`download_handler`, `cookie_handler`, `scheme_handler`, +`request_handler`, and `authentication_handler`) are called with special +arguments: * download handler @@ -537,6 +541,10 @@ Handler scripts (`download_handler`, `cookie_handler`, `scheme_handler` and - `$1 URI` of the page to be navigated to +* request handler + + - `$1 URI` of the resource which is being requested + * authentication handler: - `$1`: authentication zone unique identifier diff --git a/examples/config/config b/examples/config/config index e061e12..92606a2 100644 --- a/examples/config/config +++ b/examples/config/config @@ -42,6 +42,7 @@ set scripts_dir = @data_home/uzbl:@prefix/share/uzbl/examples/data:scripts # These handlers can't be moved to the new event system yet as we don't # support events that can wait for a response from a script. set scheme_handler = sync_spawn @scripts_dir/scheme.py +#set request_handler = sync_spawn @scripts_dir/request.py set authentication_handler = sync_spawn @scripts_dir/auth.py set download_handler = sync_spawn @scripts_dir/download.sh diff --git a/src/callbacks.c b/src/callbacks.c index f939454..23b8d55 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -358,7 +358,31 @@ request_starting_cb(WebKitWebView *web_view, WebKitWebFrame *frame, WebKitWebRes (void) response; (void) user_data; + const gchar* uri = webkit_network_request_get_uri (request); + + if (uzbl.state.verbose) + printf("Request starting -> %s\n", uri); send_event (REQUEST_STARTING, NULL, TYPE_STR, webkit_network_request_get_uri(request), NULL); + + if (uzbl.behave.request_handler) { + GString *result = g_string_new (""); + GArray *a = g_array_new (TRUE, FALSE, sizeof(gchar*)); + const CommandInfo *c = parse_command_parts(uzbl.behave.request_handler, a); + + if(c) { + g_array_append_val(a, uri); + run_parsed_command(c, a, result); + } + g_array_free(a, TRUE); + + if(result->len > 0) { + char *p = strchr(result->str, '\n' ); + if ( p != NULL ) *p = '\0'; + webkit_network_request_set_uri(request, result->str); + } + + g_string_free(result, TRUE); + } } void diff --git a/src/uzbl-core.h b/src/uzbl-core.h index 35533f7..aa88feb 100644 --- a/src/uzbl-core.h +++ b/src/uzbl-core.h @@ -150,6 +150,7 @@ typedef struct { /* Handlers */ gchar* authentication_handler; gchar* scheme_handler; + gchar* request_handler; gchar* download_handler; gboolean forward_keys; diff --git a/src/variables.c b/src/variables.c index 3bd941b..e414dac 100644 --- a/src/variables.c +++ b/src/variables.c @@ -711,6 +711,7 @@ const struct var_name_to_ptr_t { { "authentication_handler", PTR_V_STR(uzbl.behave.authentication_handler, 1, set_authentication_handler)}, { "scheme_handler", PTR_V_STR(uzbl.behave.scheme_handler, 1, NULL)}, + { "request_handler", PTR_V_STR(uzbl.behave.request_handler, 1, NULL)}, { "download_handler", PTR_V_STR(uzbl.behave.download_handler, 1, NULL)}, { "fifo_dir", PTR_V_STR(uzbl.behave.fifo_dir, 1, set_fifo_dir)}, -- cgit v1.2.3