Renamed "script" to "js".
authorBarrucadu <mike@barrucadu.co.uk>
Mon, 18 May 2009 19:08:56 +0000 (20:08 +0100)
committerBarrucadu <mike@barrucadu.co.uk>
Mon, 18 May 2009 19:08:56 +0000 (20:08 +0100)
Added "script" command which executes an external JS file.
Moved hinting to an external JS file.

I renamed "script" to "js" because we use "sh" for one-line *sh scripts, so "js" for one-line JS scripts is nice and consistent.

README
examples/configs/sampleconfig
examples/configs/sampleconfig-dev
examples/scripts/hint.js [new file with mode: 0644]
uzbl.c
uzbl.h

diff --git a/README b/README
index 733d4c8..b46c236 100644 (file)
--- a/README
+++ b/README
@@ -147,9 +147,11 @@ actions follows:
 * `zoom_in`
 * `zoom_out`
 * `uri <address>`
-* `script <body>`
+* `js <body>`
    - execute the javascript in `<body>`
    - remember that the commands, and thus actions, must not contain line breaks
+* `script <file>`
+   - execute the javascript in `<file>`
 * `toggle_status`
 * `spawn <executable> <additonal args>`
    - runs a command; see EXTERNAL SCRIPTS for details
index bca4314..db4abc2 100644 (file)
@@ -91,16 +91,16 @@ bind    p         = sh "echo act uri `xclip -selection primary -o` > $4"
 # go to the page in clipboard
 bind    P         = sh "echo act uri `xclip -selection clipboard -o` > $4"
 bind    ZZ        = exit
-bind    S         = script alert("hi");
+bind    S         = js alert("hi");
 # example showing how to use sh
 # it sends a command to the fifo, whose path is told via a positional param
 # if fifo_dir is not set, it'll echo to a file named (null) somewhere >:) remember to delete it
 bind    XS        = sh 'echo "act script alert (\"This is sent by the shell via a fifo\")" > "$4"'
 # Keyboard based link following: work in progress! No C DOM bindings yet, no click() event for hyperlinks so no referrer set..Quite basic but does the job for now...
 #hit F to toggle the Hints (now in form of link numbering)
-bind    F= script for (var i=0; i < document.links.length; i++) {var uzblid = 'uzbl_link_hint_';var li = document.links[i];var pre = document.getElementById(uzblid+i);if (pre) {li.removeChild(pre);} else {var hint = document.createElement('div');hint.setAttribute('id',uzblid+i);hint.innerHTML = i;hint.style.display='inline';hint.style.lineHeight='90%';hint.style.backgroundColor='red';hint.style.color='white';hint.style.fontSize='small-xx';hint.style.fontWeight='light';hint.style.margin='0px';hint.style.padding='2px';hint.style.position='absolute';hint.style.textDecoration='none';hint.style.left=li.style.left;hint.style.top=li.style.top;li.insertAdjacentElement('afterBegin',hint);}}
+bind    F= script /usr/share/uzbl/examples/scripts/hint.js
 #hit f followed by linknumber and ENTER to follow that link
-bind    f_        = script window.location = document.links[%s].href;
+bind    f_        = js window.location = document.links[%s].href;
 
 # "home" page if you will
 set uri = uzbl.org
index ba916cc..fb240dd 100644 (file)
@@ -92,16 +92,16 @@ bind    p         = sh "echo act uri `xclip -selection primary -o` > $4"
 # go to the page in clipboard
 bind    P         = sh "echo act uri `xclip -selection clipboard -o` > $4"
 bind    ZZ        = exit
-bind    S         = script alert("hi");
+bind    S         = js alert("hi");
 # example showing how to use sh
 # it sends a command to the fifo, whose path is told via a positional param
 # if fifo_dir is not set, it'll echo to a file named (null) somewhere >:) remember to delete it
 bind    XS        = sh 'echo "act script alert (\"This is sent by the shell via a fifo\")" > "$4"'
 # Keyboard based link following: work in progress! No C DOM bindings yet, no click() event for hyperlinks so no referrer set..Quite basic but does the job for now...
 #hit F to toggle the Hints (now in form of link numbering)
-bind    F= script for (var i=0; i < document.links.length; i++) {var uzblid = 'uzbl_link_hint_';var li = document.links[i];var pre = document.getElementById(uzblid+i);if (pre) {li.removeChild(pre);} else {var hint = document.createElement('div');hint.setAttribute('id',uzblid+i);hint.innerHTML = i;hint.style.display='inline';hint.style.lineHeight='90%';hint.style.backgroundColor='red';hint.style.color='white';hint.style.fontSize='small-xx';hint.style.fontWeight='light';hint.style.margin='0px';hint.style.padding='2px';hint.style.position='absolute';hint.style.textDecoration='none';hint.style.left=li.style.left;hint.style.top=li.style.top;li.insertAdjacentElement('afterBegin',hint);}}
+bind    F= script ./examples/scripts/hint.js
 #hit f followed by linknumber and ENTER to follow that link
-bind    f_        = script window.location = document.links[%s].href;
+bind    f_        = js window.location = document.links[%s].href;
 
 # "home" page if you will
 set uri = uzbl.org
diff --git a/examples/scripts/hint.js b/examples/scripts/hint.js
new file mode 100644 (file)
index 0000000..ec7f1e2
--- /dev/null
@@ -0,0 +1,26 @@
+for (var i=0; i < document.links.length; i++) {
+    var uzblid = 'uzbl_link_hint_';
+    var li = document.links[i];
+    var pre = document.getElementById(uzblid+i);
+
+    if (pre) {
+        li.removeChild(pre);
+    } else {
+        var hint = document.createElement('div');
+        hint.setAttribute('id',uzblid+i);
+        hint.innerHTML = i;
+        hint.style.display='inline';
+        hint.style.lineHeight='90%';
+        hint.style.backgroundColor='red';
+        hint.style.color='white';
+        hint.style.fontSize='small-xx';
+        hint.style.fontWeight='light';
+        hint.style.margin='0px';
+        hint.style.padding='2px';
+        hint.style.position='absolute';
+        hint.style.textDecoration='none';
+        hint.style.left=li.style.left;
+        hint.style.top=li.style.top;
+        li.insertAdjacentElement('afterBegin',hint);
+    }
+}
diff --git a/uzbl.c b/uzbl.c
index 4606130..d0a06b4 100644 (file)
--- a/uzbl.c
+++ b/uzbl.c
@@ -429,7 +429,8 @@ static struct {char *name; Command command;} cmdlist[] =
     { "zoom_in",            view_zoom_in,           }, //Can crash (when max zoom reached?).
     { "zoom_out",           view_zoom_out,          },
     { "uri",                load_uri                },
-    { "script",             run_js                  },
+    { "js",                 run_js                  },
+    { "script",             run_external_js         },
     { "toggle_status",      toggle_status_cb        },
     { "spawn",              spawn                   },
     { "sh",                 spawn_sh                },
@@ -516,6 +517,33 @@ run_js (WebKitWebView * web_view, const gchar *param) {
 }
 
 static void
+run_external_js (WebKitWebView * web_view, const gchar *param) {
+    if (param) {
+        FILE*  fp = fopen (param, "r");
+        gchar* buffer = malloc(512);
+        gchar* js = NULL;
+
+        if (fp != NULL) {
+            while (fgets (buffer, 512, fp) != NULL) {
+                if (js == NULL) {
+                    js = g_strdup ((const gchar*) buffer);
+                } else {
+                    gchar* newjs = g_strconcat (js, buffer, NULL);
+                    js = newjs;
+                }
+                bzero (buffer, strlen (buffer));
+            }
+            webkit_web_view_execute_script (web_view, js);
+        } else {
+            fprintf (stderr, "JavaScript file '%s' not be read.\n", param);
+        }
+        fclose (fp);
+        g_free (buffer);
+        g_free (js);
+    }
+}
+
+static void
 search_text (WebKitWebView *page, const char *param, const gboolean forward) {
     if ((param) && (param[0] != '\0')) {
         strcpy(uzbl.state.searchtx, param);
diff --git a/uzbl.h b/uzbl.h
index 4ae42a6..0d8be65 100644 (file)
--- a/uzbl.h
+++ b/uzbl.h
@@ -360,6 +360,9 @@ search_reverse_text (WebKitWebView *page, const char *param);
 static void
 run_js (WebKitWebView * web_view, const gchar *param);
 
+static void
+run_external_js (WebKitWebView * web_view, const gchar *param);
+
 static void handle_cookies (SoupSession *session,
                                                        SoupMessage *msg,
                                                        gpointer     user_data);