The attached patch lets you bind keys to small lines of Javascript to
run.

The intent is that the usual ~/.surf/script.js defines various methods,
which are then run by the bindings.  This is much cleaner than having
script.js create its own event listeners for keybindings going outside
surf's own system.

diff -r 8671f9860efe surf.c
--- a/surf.c	Tue May 11 10:16:22 2010 +0200
+++ b/surf.c	Tue May 11 11:06:55 2010 +0200
@@ -67,6 +67,8 @@
 
 static char *buildpath(const char *path);
 static void cleanup(void);
+static void evalscript(WebKitWebFrame *frame, JSContextRef js, char *script, char* scriptname);
+static void evalscriptfile(WebKitWebFrame *frame, JSContextRef js, char *scriptpath);
 static void clipboard(Client *c, const Arg *arg);
 static void context(WebKitWebView *v, GtkMenu *m, Client *c);
 static char *copystr(char **str, const char *src);
@@ -107,6 +109,8 @@
 static void sigchld(int unused);
 static void source(Client *c, const Arg *arg);
 static void spawn(Client *c, const Arg *arg);
+static void runscript(Client *c, const Arg *arg);
+static void eval(Client *c, const Arg *arg);
 static void stop(Client *c, const Arg *arg);
 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c);
 static void update(Client *c);
@@ -151,15 +155,23 @@
 }
 
 void
-runscript(WebKitWebFrame *frame, JSContextRef js) {
-	JSStringRef jsscript;
-	char *script;
+evalscript(WebKitWebFrame *frame, JSContextRef js, char *script, char* scriptname) {
+	JSStringRef jsscript, jsscriptname;
 	JSValueRef exception = NULL;
-	GError *error;
 	
-	if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
-		jsscript = JSStringCreateWithUTF8CString(script);
-		JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception);
+	jsscript = JSStringCreateWithUTF8CString(script);
+	jsscriptname = JSStringCreateWithUTF8CString(scriptname);
+	JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), jsscriptname, 0, &exception);
+	JSStringRelease(jsscript);
+	JSStringRelease(jsscriptname);
+}
+
+void
+evalscriptfile(WebKitWebFrame *frame, JSContextRef js, char *scriptpath) {
+	GError *error = NULL;
+	char* script = NULL;
+	if(g_file_get_contents(scriptpath, &script, NULL, &error)) {
+		evalscript(frame, webkit_web_frame_get_global_context(frame), script, scriptpath);
 	}
 }
 
@@ -576,7 +588,7 @@
 	gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
 	webkit_web_view_set_full_content_zoom(c->view, TRUE);
 	frame = webkit_web_view_get_main_frame(c->view);
-	runscript(frame, webkit_web_frame_get_global_context(frame));
+	evalscriptfile(frame, webkit_web_frame_get_global_context(frame), scriptfile);
 	settings = webkit_web_view_get_settings(c->view);
 	if(!(ua = getenv("SURF_USERAGENT")))
 		ua = useragent;
@@ -825,6 +837,18 @@
 }
 
 void
+runscript(Client *c, const Arg *arg) {
+	WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
+	evalscriptfile(frame, webkit_web_frame_get_global_context(frame), ((char **)arg->v)[0]);
+}
+
+void
+eval(Client *c, const Arg *arg) {
+	WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
+	evalscript(frame, webkit_web_frame_get_global_context(frame), ((char **)arg->v)[0], "");
+}
+
+void
 stop(Client *c, const Arg *arg) {
 	if(c->download)
 		webkit_download_cancel(c->download);
@@ -881,7 +905,7 @@
 
 void
 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
-	runscript(frame, js);
+	evalscriptfile(frame, js, scriptfile);
 }
 
 void
-- 
\  Troels
/\ Henriksen

Reply via email to