Hi folks 

I added some logic to surf's uri loading to make it handle a few useful 
idioms not understood by webkit:
*   Loading a blank uri goes to homepage instead of nowhere. This works
any
    time, unlike the existing homepage patch that only does it on
invokation.
    So now, ctrl-G, shift-enter takes me to my search engine
(duckduckgo).
*   `javascript:alert('hello');` works as expected in other browsers.
    Kind of useful sometimes.
*   loading valid filenames will load the file even without file://.

I find that these (especially the homepage thing) really improve my surf

experience.

I also put the spacial nav option with the other ones like it
(loadimage, 
plugin, script). I don't use it (spacenav), but I left it on (surf's 
default) for the patch.

To use it, you need to apply and then add    

    static char *homepage = "<your homepage here>";

to your config.h.

I'll put it on the wiki as-is unless there is anything you guys can
think up
to improve.

Happy hacking!

/wolf

diff -r 7a931a352cf9 surf.c
--- a/surf.c	Thu Sep 09 11:15:02 2010 +0200
+++ b/surf.c	Sat Feb 19 13:47:23 2011 -0800
@@ -62,7 +62,7 @@
 static gboolean showxid = FALSE;
 static char winid[64];
 static char *progname;
-static gboolean loadimage = 1, plugin = 1, script = 1;
+static gboolean loadimage = 1, plugin = 1, script = 1, spacenav = 1;
 
 static char *buildpath(const char *path);
 static void cleanup(void);
@@ -382,18 +382,11 @@
 }
 
 void
-loaduri(Client *c, const Arg *arg) {
-	char *u;
-	const char *uri = (char *)arg->v;
-	Arg a = { .b = FALSE };
-
-	if(strcmp(uri, "") == 0)
-		return;
-	u = g_strrstr(uri, "://") ? g_strdup(uri)
-		: g_strdup_printf("http://%s";, uri);
-	/* prevents endless loop */
+loadrealuri(Client *c, char *u) {
 	if(c->uri && strcmp(u, c->uri) == 0) {
+		Arg a = { .b = FALSE };
 		reload(c, &a);
+		g_free(u);
 	}
 	else {
 		webkit_web_view_load_uri(c->view, u);
@@ -405,6 +398,27 @@
 }
 
 void
+loaduri(Client *c, const Arg *arg) {
+	char *u;
+	const char *uri = (char *)arg->v;
+
+	if(strcmp(uri, "") == 0)
+		uri = homepage;
+
+	if (g_strrstr(uri, "javascript:")) {
+		webkit_web_view_execute_script(c->view, uri + strlen("javascript:"));
+		return;
+	}
+	else if (g_strrstr(uri, "://"))
+		u = g_strdup(uri);
+	else if (g_file_test(uri, G_FILE_TEST_IS_REGULAR))
+		u = g_strdup_printf("file://%s", uri);
+	else
+		u = g_strdup_printf("http://%s";, uri);
+	loadrealuri(c, u);
+}
+
+void
 navigate(Client *c, const Arg *arg) {
 	int steps = *(int *)arg;
 	webkit_web_view_go_back_or_forward(c->view, steps);
@@ -501,7 +515,7 @@
 	g_object_set(G_OBJECT(settings), "auto-load-images", loadimage, NULL);
 	g_object_set(G_OBJECT(settings), "enable-plugins", plugin, NULL);
 	g_object_set(G_OBJECT(settings), "enable-scripts", script, NULL);
-	g_object_set(G_OBJECT(settings), "enable-spatial-navigation", true, NULL);
+	g_object_set(G_OBJECT(settings), "enable-spatial-navigation", spacenav, NULL);
 
 	g_free(uri);
 
@@ -834,12 +848,10 @@
 			usage();
 		}
 	}
-	if(i < argc)
-		arg.v = argv[i];
+	arg.v = (i < argc) ? argv[i] : "";
 	setup();
 	newclient();
-	if(arg.v)
-		loaduri(clients, &arg);
+	loaduri(clients, &arg);
 	gtk_main();
 	cleanup();
 	return EXIT_SUCCESS;

Reply via email to