added js-fix by Troels Henriksen. Thanks :)
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index aa29780..3ac7665 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -18,6 +18,7 @@
 #include <webkit/webkit.h>
 #include <glib/gstdio.h>
 #include <JavaScriptCore/JavaScript.h>
+#include <sys/file.h>
 
 #define LENGTH(x)               (sizeof x / sizeof x[0])
 #define CLEANMASK(mask)         (mask & ~(GDK_MOD2_MASK))
@@ -149,6 +150,19 @@ cleanup(void) {
        g_free(stylefile);
 }
 
+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);
+       }
+}
+
 void
 clipboard(Client *c, const Arg *arg) {
        gboolean paste = *(gboolean *)arg;
@@ -306,7 +320,11 @@ find(Client *c, const Arg *arg) {
 
 const char *
 getcookies(SoupURI *uri) {
-       return NULL;
+       const char *c;
+       SoupCookieJar *j = soup_cookie_jar_text_new(cookiefile, TRUE);
+       c = soup_cookie_jar_get_cookies(j, uri, TRUE);
+       g_object_unref(j);
+       return c;
 }
 
 const char *
@@ -426,8 +444,7 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
                c->progress = 0;
                update(c);
                break;
-       case WEBKIT_LOAD_PROVISIONAL:
-       case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
+       default:
                break;
        }
 }
@@ -466,6 +483,7 @@ newclient(void) {
        int i;
        Client *c;
        WebKitWebSettings *settings;
+       WebKitWebFrame *frame;
        GdkGeometry hints = { 1, 1 };
        char *uri, *ua;
 
@@ -552,6 +570,8 @@ newclient(void) {
        gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
        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));
        settings = webkit_web_view_get_settings(c->view);
        if(!(ua = getenv("SURF_USERAGENT")))
                ua = useragent;
@@ -609,7 +629,7 @@ newwindow(Client *c, const Arg *arg) {
        if(!plugin)
                cmd[i++] = "-p";
        if(!loadimage)
-               cmd[i++] = "-l";
+               cmd[i++] = "-i";
        if(showxid)
                cmd[i++] = "-x";
        cmd[i++] = "--";
@@ -700,7 +720,21 @@ scroll(Client *c, const Arg *arg) {
 
 void
 setcookie(SoupCookie *c) {
-
+       int lock;
+
+       lock = open(cookiefile, 0);
+       flock(lock, LOCK_EX);
+       SoupDate *e;
+       SoupCookieJar *j = soup_cookie_jar_text_new(cookiefile, FALSE);
+       c = soup_cookie_copy(c);
+       if(c->expires == NULL) {
+               e = soup_date_new_from_time_t(time(NULL) + sessiontime);
+               soup_cookie_set_expires(c, e);
+       }
+       soup_cookie_jar_add_cookie(j, c);
+       g_object_unref(j);
+       flock(lock, LOCK_UN);
+       close(lock);
 }
 
 void
@@ -837,20 +871,12 @@ updatewinid(Client *c) {
 void
 usage(void) {
        fputs("surf - simple browser\n", stderr);
-       die("usage: surf [-e Window] [-x] [-i] [-p] [-s] [uri]\n");
+       die("usage: surf [-e xid] [-i] [-p] [-s] [-v] [-x] [uri]\n");
 }
 
 void
 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
-       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);
-       }
+       runscript(frame, js);
 }
 
 void
@@ -880,9 +906,6 @@ main(int argc, char *argv[]) {
                        break;
                }
                switch(argv[i][1]) {
-               case 'x':
-                       showxid = TRUE;
-                       break;
                case 'e':
                        if(++i < argc)
                                embed = atoi(argv[i]);
@@ -898,6 +921,9 @@ main(int argc, char *argv[]) {
                case 's':
                        script = 0;
                        break;
+               case 'x':
+                       showxid = TRUE;
+                       break;
                case 'v':
                        die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
                default: