Add eval() function for executing Javascript. Handy for keybindings.

This commit is contained in:
Troels Henriksen 2011-11-03 14:09:19 +01:00
parent 3b104416d3
commit 2e62372969
1 changed files with 22 additions and 6 deletions

26
surf.c
View File

@ -102,6 +102,7 @@ static void setup(void);
static void sigchld(int unused); static void sigchld(int unused);
static void source(Client *c, const Arg *arg); static void source(Client *c, const Arg *arg);
static void spawn(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 stop(Client *c, const Arg *arg);
static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c); static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c);
static void update(Client *c); static void update(Client *c);
@ -144,15 +145,24 @@ cleanup(void) {
} }
void void
runscript(WebKitWebFrame *frame, JSContextRef js) { evalscript(WebKitWebFrame *frame, JSContextRef js, char *script, char* scriptname) {
JSStringRef jsscript; JSStringRef jsscript, jsscriptname;
char *script;
JSValueRef exception = NULL; 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) {
char *script;
GError *error; GError *error;
if(g_file_get_contents(scriptfile, &script, NULL, &error)) { if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
jsscript = JSStringCreateWithUTF8CString(script); evalscript(frame, webkit_web_frame_get_global_context(frame), script, scriptfile);
JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception);
} }
} }
@ -739,6 +749,12 @@ spawn(Client *c, const Arg *arg) {
} }
} }
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 void
stop(Client *c, const Arg *arg) { stop(Client *c, const Arg *arg) {
webkit_web_view_stop_loading(c->view); webkit_web_view_stop_loading(c->view);