summaryrefslogtreecommitdiff
path: root/surf.c
diff options
context:
space:
mode:
authorTroels Henriksen <athas@sigkill.dk>2011-11-03 14:09:19 +0100
committerTroels Henriksen <athas@sigkill.dk>2011-11-03 14:09:19 +0100
commit2e62372969239285705504187b0211039b5ae619 (patch)
treebe1222951afdb4b6a25bd1d5599a563851db2cfd /surf.c
parent3b104416d350f25db098d6c8dc9ddff11311e065 (diff)
downloadsurf-2e62372969239285705504187b0211039b5ae619.tar.gz
surf-2e62372969239285705504187b0211039b5ae619.tar.bz2
surf-2e62372969239285705504187b0211039b5ae619.zip
Add eval() function for executing Javascript. Handy for keybindings.
Diffstat (limited to 'surf.c')
-rw-r--r--surf.c26
1 files changed, 21 insertions, 5 deletions
diff --git a/surf.c b/surf.c
index 85155b5..60d1b14 100644
--- a/surf.c
+++ b/surf.c
@@ -102,6 +102,7 @@ static void setup(void);
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);
@@ -144,15 +145,24 @@ cleanup(void) {
}
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);
}
}
@@ -740,6 +750,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
stop(Client *c, const Arg *arg) {
webkit_web_view_stop_loading(c->view);
}