Adapt JavaScript functions
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index a93e951..e8b2c3d 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -120,12 +120,12 @@ static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
 static void decideresource(WebKitPolicyDecision *d, Client *c);
-static gboolean deletion_interface(WebKitWebView *view,
-                                   WebKitDOMHTMLElement *arg1, Client *c);
+static void closeview(WebKitWebView *v, Client *c);
 static void destroyclient(Client *c);
 static void destroywin(GtkWidget* w, Client *c);
 static void die(const char *errstr, ...);
-static void eval(Client *c, const Arg *arg);
+static void evalscript(Client *c, const char *jsstr, ...);
+static void runscript(Client *c);
 static void find(Client *c, const Arg *arg);
 static void fullscreen(Client *c, const Arg *arg);
 static gboolean permissionrequested(WebKitWebView *v,
@@ -162,6 +162,7 @@ static Client *newclient(Client *c);
 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
 static void showview(WebKitWebView *v, Client *c);
 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
+static GtkWidget *createwindow(Client *c);
 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
 static void print(Client *c, const Arg *arg);
 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
@@ -188,8 +189,6 @@ static void togglestyle(Client *c, const Arg *arg);
 static void updatetitle(Client *c);
 static void updatewinid(Client *c);
 static void usage(void);
-static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
-                                JSContextRef js, JSObjectRef win, Client *c);
 static void zoom(Client *c, const Arg *arg);
 
 /* configuration, allows nested code to access above variables */
@@ -338,29 +337,28 @@ cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
 }
 
 void
-evalscript(JSContextRef js, char *script, char* scriptname)
+evalscript(Client *c, const char *jsstr, ...)
 {
-       JSStringRef jsscript, jsscriptname;
-       JSValueRef exception = NULL;
+       va_list ap;
+       gchar *script;
+
+       va_start(ap, jsstr);
+       script = g_strdup_vprintf(jsstr, ap);
+       va_end(ap);
 
-       jsscript = JSStringCreateWithUTF8CString(script);
-       jsscriptname = JSStringCreateWithUTF8CString(scriptname);
-       JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
-                        jsscriptname, 0, &exception);
-       JSStringRelease(jsscript);
-       JSStringRelease(jsscriptname);
+       webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
+       g_free(script);
 }
 
 void
-runscript(WebKitWebFrame *frame)
+runscript(Client *c)
 {
-       char *script;
-       GError *error;
+       gchar *script;
+       gsize l;
 
-       if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
-               evalscript(webkit_web_frame_get_global_context(frame), script,
-                          scriptfile);
-       }
+       if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
+               evalscript(c, script);
+       g_free(script);
 }
 
 void
@@ -547,22 +545,15 @@ decideresource(WebKitPolicyDecision *d, Client *c)
        }
 }
 
-gboolean
-deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
-{
-       return FALSE;
-}
-
 void
 destroyclient(Client *c)
 {
        Client *p;
 
        webkit_web_view_stop_loading(c->view);
-       gtk_widget_destroy(GTK_WIDGET(c->view));
-       gtk_widget_destroy(c->scroll);
-       gtk_widget_destroy(c->vbox);
+       /* Not needed, has already been called
        gtk_widget_destroy(c->win);
+        */
 
        for (p = clients; p && p->next != c; p = p->next)
                ;
@@ -571,14 +562,20 @@ destroyclient(Client *c)
        else
                clients = c->next;
        free(c);
-       if (clients == NULL)
-               gtk_main_quit();
+}
+
+void
+closeview(WebKitWebView *v, Client *c)
+{
+       gtk_widget_destroy(c->win);
 }
 
 void
 destroywin(GtkWidget* w, Client *c)
 {
        destroyclient(c);
+       if (clients == NULL)
+               gtk_main_quit();
 }
 
 void
@@ -994,9 +991,6 @@ newview(Client *c, WebKitWebView *rv)
        g_signal_connect(G_OBJECT(v),
                         "decide-policy",
                         G_CALLBACK(decidepolicy), c);
-       g_signal_connect(G_OBJECT(v),
-                        "window-object-cleared",
-                        G_CALLBACK(windowobjectcleared), c);
        g_signal_connect(G_OBJECT(v),
                         "load-changed",
                         G_CALLBACK(loadchanged), c);
@@ -1006,9 +1000,8 @@ newview(Client *c, WebKitWebView *rv)
        g_signal_connect(G_OBJECT(v),
                         "button-release-event",
                         G_CALLBACK(buttonreleased), c);
-       g_signal_connect(G_OBJECT(v),
-                        "should-show-delete-interface-for-element",
-                        G_CALLBACK(deletion_interface), c);
+       g_signal_connect(G_OBJECT(v), "close",
+                       G_CALLBACK(closeview), c);
 
        return v;
 }
@@ -1020,33 +1013,7 @@ showview(WebKitWebView *v, Client *c)
        GdkRGBA bgcolor = { 0 };
        GdkWindow *gwin;
 
-       /* Window */
-       if (embed) {
-               c->win = gtk_plug_new(embed);
-       } else {
-               c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-
-               /* TA:  20091214:  Despite what the GNOME docs say, the ICCCM
-                * is always correct, so we should still call this function.
-                * But when doing so, we *must* differentiate between a
-                * WM_CLASS and a resource on the window.  By convention, the
-                * window class (WM_CLASS) is capped, while the resource is in
-                * lowercase.   Both these values come as a pair.
-                */
-               gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
-
-               /* TA:  20091214:  And set the role here as well -- so that
-                * sessions can pick this up.
-                */
-               gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
-       }
-       gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
-       g_signal_connect(G_OBJECT(c->win),
-                        "destroy",
-                        G_CALLBACK(destroywin), c);
-       g_signal_connect(G_OBJECT(c->win),
-                        "leave_notify_event",
-                        G_CALLBACK(titlechangeleave), c);
+       c->win = createwindow(c);
 
        if (!kioskmode)
                addaccelgroup(c);
@@ -1065,8 +1032,6 @@ showview(WebKitWebView *v, Client *c)
        gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
        gdk_window_add_filter(gwin, processx, c);
 
-       runscript(frame);
-
        /* This might conflict with _zoomto96dpi_. */
        if (zoomlevel != 1.0)
                webkit_web_view_set_zoom_level(c->view, zoomlevel);
@@ -1131,6 +1096,41 @@ newwindow(Client *c, const Arg *arg, gboolean noembed)
        spawn(NULL, &a);
 }
 
+GtkWidget *
+createwindow(Client *c)
+{
+       GtkWidget *w;
+
+       if (embed) {
+               w = gtk_plug_new(embed);
+       } else {
+               w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+
+               /* TA:  20091214:  Despite what the GNOME docs say, the ICCCM
+                * is always correct, so we should still call this function.
+                * But when doing so, we *must* differentiate between a
+                * WM_CLASS and a resource on the window.  By convention, the
+                * window class (WM_CLASS) is capped, while the resource is in
+                * lowercase.   Both these values come as a pair.
+                */
+               gtk_window_set_wmclass(GTK_WINDOW(w), "surf", "Surf");
+
+               /* TA:  20091214:  And set the role here as well -- so that
+                * sessions can pick this up.
+                */
+               gtk_window_set_role(GTK_WINDOW(w), "Surf");
+
+               gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);
+       }
+
+       g_signal_connect(G_OBJECT(w), "destroy",
+           G_CALLBACK(destroywin), c);
+       g_signal_connect(G_OBJECT(w), "leave_notify_event",
+           G_CALLBACK(titlechangeleave), c);
+
+       return w;
+}
+
 void
 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
 {
@@ -1318,14 +1318,6 @@ spawn(Client *c, const Arg *arg)
        }
 }
 
-void
-eval(Client *c, const Arg *arg)
-{
-       WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
-       evalscript(webkit_web_frame_get_global_context(frame),
-                  ((char **)arg->v)[0], "");
-}
-
 void
 stop(Client *c, const Arg *arg)
 {
@@ -1521,13 +1513,6 @@ usage(void)
            "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
 }
 
-void
-windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
-                    JSObjectRef win, Client *c)
-{
-       runscript(frame);
-}
-
 void
 zoom(Client *c, const Arg *arg)
 {