[hackers] [surf] Add eval() function for executing Javascript. Handy for keybindings. || Troels Henriksen
changeset: 226:c7cbbad781af
tag: tip
user: Troels Henriksen <athas_AT_sigkill.dk>
date: Thu Nov 03 14:09:19 2011 +0100
files: surf.c
description:
Add eval() function for executing Javascript. Handy for keybindings.
diff -r 46a72ccc435f -r c7cbbad781af surf.c
--- a/surf.c Sat Oct 29 10:59:34 2011 +0200
+++ b/surf.c Thu Nov 03 14:09:19 2011 +0100
_AT_@ -102,6 +102,7 @@
static void sigchld(int unused);
static void source(Client *c, const Arg *arg);
static void spawn(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);
_AT_@ -144,15 +145,24 @@
}
void
+evalscript(WebKitWebFrame *frame, JSContextRef js, char *script, char* scriptname) {
+ JSStringRef jsscript, jsscriptname;
+ JSValueRef exception = NULL;
+
+ jsscript = JSStringCreateWithUTF8CString(script);
+ jsscriptname = JSStringCreateWithUTF8CString(scriptname);
+ JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), jsscriptname, 0, &exception);
+ JSStringRelease(jsscript);
+ JSStringRelease(jsscriptname);
+}
+
+void
runscript(WebKitWebFrame *frame, JSContextRef js) {
- JSStringRef jsscript;
char *script;
- 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);
+ evalscript(frame, webkit_web_frame_get_global_context(frame), script, scriptfile);
}
}
_AT_@ -740,6 +750,12 @@
}
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) {
webkit_web_view_stop_loading(c->view);
}
Received on Thu Nov 03 2011 - 14:10:31 CET
This archive was generated by hypermail 2.3.0
: Thu Nov 03 2011 - 14:12:05 CET