diff options
-rw-r--r-- | README | 7 | ||||
-rw-r--r-- | examples/configs/sampleconfig | 10 | ||||
-rw-r--r-- | examples/configs/sampleconfig-dev | 10 | ||||
-rw-r--r-- | examples/scripts/hint.js | 26 | ||||
-rw-r--r-- | uzbl.c | 164 | ||||
-rw-r--r-- | uzbl.h | 14 |
6 files changed, 187 insertions, 44 deletions
@@ -150,9 +150,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 @@ -164,7 +166,8 @@ actions follows: * `exit` * `search <string>` * `search_reverse <string>` -* `insert_mode` +* `toggle_insert_mode <optional state>` + - if the optional state is 0, disable insert mode. If 1, enable insert mode. * `runcmd` - can be used for running a command such as SET or BIND diff --git a/examples/configs/sampleconfig b/examples/configs/sampleconfig index ada1134..db4abc2 100644 --- a/examples/configs/sampleconfig +++ b/examples/configs/sampleconfig @@ -76,8 +76,8 @@ bind gh = uri http://www.uzbl.org bind o _ = uri %s bind :wiki _ = uri http://wiki.archlinux.org/index.php/Special:Search?search=%s&go=Go bind gg _ = uri http://www.google.com/search?q=%s -bind i = insert_mode -#TODO: no 'toggle' command? +bind i = toggle_insert_mode +bind I = toggle_insert_mode 0 # disable insert mode (1 to enable) bind B = spawn /usr/share/uzbl/examples/scripts/insert_bookmark.sh bind U = spawn /usr/share/uzbl/examples/scripts/load_url_from_history.sh bind u = spawn /usr/share/uzbl/examples/scripts/load_url_from_bookmarks.sh @@ -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 diff --git a/examples/configs/sampleconfig-dev b/examples/configs/sampleconfig-dev index 337da7b..fb240dd 100644 --- a/examples/configs/sampleconfig-dev +++ b/examples/configs/sampleconfig-dev @@ -77,8 +77,8 @@ bind gh = uri http://www.uzbl.org bind o _ = uri %s bind :wiki _ = uri http://wiki.archlinux.org/index.php/Special:Search?search=%s&go=Go bind gg _ = uri http://www.google.com/search?q=%s -bind i = insert_mode -#TODO: no 'toggle' command? +bind i = toggle_insert_mode +bind I = toggle_insert_mode 0 # disable insert mode (1 to enable) bind B = spawn ./examples/scripts/insert_bookmark.sh bind U = spawn ./examples/scripts/load_url_from_history.sh bind u = spawn ./examples/scripts/load_url_from_bookmarks.sh @@ -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 index 0000000..ec7f1e2 --- /dev/null +++ b/examples/scripts/hint.js @@ -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); + } +} @@ -159,6 +159,56 @@ str_replace (const char* search, const char* replace, const char* string) { return ret; } +static gchar** +read_file_by_line (gchar *path) { + GIOChannel *chan = NULL; + gchar *readbuf = NULL; + gsize len; + gchar *lines[512]; + int i; + + chan = g_io_channel_new_file(path, "r", NULL); + + if (chan) { + while (g_io_channel_read_line(chan, &readbuf, &len, NULL, NULL) + == G_IO_STATUS_NORMAL) { + lines[i] = g_strdup (readbuf); + g_free (readbuf); + i ++; + } + + g_io_channel_unref (chan); + } else { + fprintf(stderr, "File '%s' not be read.\n", path); + } + + lines[i] = NULL; + return lines; +} + +static +gchar* parseenv (const char* string) { + extern char** environ; + gchar* newstring = g_strdup (string); + int i = 0; + + while (environ[i] != NULL) { + gchar** env = g_strsplit (environ[i], "=", 0); + gchar* envname = malloc (strlen (env[0]) + 1); + + strcat (envname, "$"); + strcat (envname, env[0]); + + newstring = str_replace(envname, env[1], newstring); + + g_free (envname); + //g_strfreev (env); - This still breaks uzbl, but shouldn't. The mystery thickens... + i ++; + } + + return newstring; +} + static sigfunc* setup_signal(int signr, sigfunc *shandler) { struct sigaction nh, oh; @@ -403,27 +453,28 @@ VIEWFUNC(go_forward) static struct {char *name; Command command;} cmdlist[] = { - { "back", view_go_back }, - { "forward", view_go_forward }, - { "scroll_vert", scroll_vert }, - { "scroll_horz", scroll_horz }, - { "scroll_begin", scroll_begin }, - { "scroll_end", scroll_end }, - { "reload", view_reload, }, - { "reload_ign_cache", view_reload_bypass_cache}, - { "stop", view_stop_loading, }, - { "zoom_in", view_zoom_in, }, //Can crash (when max zoom reached?). - { "zoom_out", view_zoom_out, }, - { "uri", load_uri }, - { "script", run_js }, - { "toggle_status", toggle_status_cb }, - { "spawn", spawn }, - { "sh", spawn_sh }, - { "exit", close_uzbl }, - { "search", search_forward_text }, - { "search_reverse", search_reverse_text }, - { "insert_mode", set_insert_mode }, - { "runcmd", runcmd } + { "back", view_go_back }, + { "forward", view_go_forward }, + { "scroll_vert", scroll_vert }, + { "scroll_horz", scroll_horz }, + { "scroll_begin", scroll_begin }, + { "scroll_end", scroll_end }, + { "reload", view_reload, }, + { "reload_ign_cache", view_reload_bypass_cache}, + { "stop", view_stop_loading, }, + { "zoom_in", view_zoom_in, }, //Can crash (when max zoom reached?). + { "zoom_out", view_zoom_out, }, + { "uri", load_uri }, + { "js", run_js }, + { "script", run_external_js }, + { "toggle_status", toggle_status_cb }, + { "spawn", spawn }, + { "sh", spawn_sh }, + { "exit", close_uzbl }, + { "search", search_forward_text }, + { "search_reverse", search_reverse_text }, + { "toggle_insert_mode", toggle_insert_mode }, + { "runcmd", runcmd } }; static void @@ -465,12 +516,21 @@ file_exists (const char * filename) { return (access(filename, F_OK) == 0); } -static void -set_insert_mode(WebKitWebView *page, const gchar *param) { +void +toggle_insert_mode(WebKitWebView *page, const gchar *param) { (void)page; (void)param; - uzbl.behave.insert_mode = TRUE; + if (param != NULL) { + if (strcmp (param, "0") == 0) { + uzbl.behave.insert_mode = FALSE; + } else { + uzbl.behave.insert_mode = TRUE; + } + } else { + uzbl.behave.insert_mode = ! uzbl.behave.insert_mode; + } + update_title(); } @@ -493,6 +553,39 @@ run_js (WebKitWebView * web_view, const gchar *param) { } static void +run_external_js (WebKitWebView * web_view, const gchar *param) { + if (param) { + gchar** splitted = g_strsplit (param, " ", 2); + gchar** lines = read_file_by_line (splitted[0]); + gchar* js = NULL; + int i; + + if (lines[0] != NULL) { + for (i = 0; lines[i] != NULL; i ++) { + if (js == NULL) { + js = g_strdup (lines[i]); + } else { + gchar* newjs = g_strconcat (js, lines[i], NULL); + js = newjs; + } + } + + if (uzbl.state.verbose) + printf ("External JavaScript file %s loaded\n", splitted[0]); + + if (splitted[1]) { + gchar* newjs = str_replace("%s", splitted[1], js); + js = newjs; + } + webkit_web_view_execute_script (web_view, js); + g_free (js); + } else { + fprintf(stderr, "JavaScript file '%s' not be read.\n", splitted[0]); + } + } +} + +static void search_text (WebKitWebView *page, const char *param, const gboolean forward) { if ((param) && (param[0] != '\0')) { uzbl.state.searchtx = g_strdup(param); @@ -500,8 +593,17 @@ search_text (WebKitWebView *page, const char *param, const gboolean forward) { if (uzbl.state.searchtx != NULL) { if (uzbl.state.verbose) printf ("Searching: %s\n", uzbl.state.searchtx); - webkit_web_view_unmark_text_matches (page); - webkit_web_view_mark_text_matches (page, uzbl.state.searchtx, FALSE, 0); + + if (g_strcmp0 (uzbl.state.searchtx, uzbl.state.searchold) != 0) { + webkit_web_view_unmark_text_matches (page); + webkit_web_view_mark_text_matches (page, uzbl.state.searchtx, FALSE, 0); + + if (uzbl.state.searchold != NULL) + g_free (uzbl.state.searchold); + + uzbl.state.searchold = g_strdup (uzbl.state.searchtx); + } + webkit_web_view_set_highlight_text_matches (page, TRUE); webkit_web_view_search_text (page, uzbl.state.searchtx, FALSE, forward, TRUE); g_free(uzbl.state.searchtx); @@ -1038,8 +1140,10 @@ parse_cmd_line(const char *ctl_line) { if(ctl_line[0] == 's' || ctl_line[0] == 'S') { tokens = g_regex_split(uzbl.comm.set_regex, ctl_line, 0); if(tokens[0][0] == 0) { - set_var_value(tokens[1], tokens[2]); + gchar* value = parseenv (tokens[2]); + set_var_value(tokens[1], value); g_strfreev(tokens); + g_free(value); } else printf("Error in command: %s\n", tokens[0]); @@ -1058,8 +1162,10 @@ parse_cmd_line(const char *ctl_line) { else if(ctl_line[0] == 'b' || ctl_line[0] == 'B') { tokens = g_regex_split(uzbl.comm.bind_regex, ctl_line, 0); if(tokens[0][0] == 0) { - add_binding(tokens[1], tokens[2]); + gchar* value = parseenv (tokens[2]); + add_binding(tokens[1], value); g_strfreev(tokens); + g_free(value); } else printf("Error in command: %s\n", tokens[0]); @@ -1676,7 +1782,6 @@ save_cookies (SoupMessage *msg, gpointer user_data){ g_slist_free(ck); } - int main (int argc, char* argv[]) { gtk_init (&argc, &argv); @@ -1711,7 +1816,6 @@ main (int argc, char* argv[]) { commands_hash (); make_var_to_name_hash(); - uzbl.gui.vbox = gtk_vbox_new (FALSE, 0); uzbl.gui.scrolled_win = create_browser(); @@ -108,6 +108,7 @@ typedef struct { gchar *executable_path; GString* keycmd; gchar *searchtx; + gchar* searchold; struct utsname unameinfo; /* system info */ gboolean verbose; } State; @@ -202,6 +203,12 @@ itos(int val); static char * str_replace (const char* search, const char* replace, const char* string); +static gchar** +read_file_by_line (gchar *path); + +static +gchar* parseenv (const char* string); + static void clean_up(void); @@ -256,8 +263,8 @@ new_action(const gchar *name, const gchar *param); static bool file_exists (const char * filename); -static void -set_insert_mode(WebKitWebView *page, const gchar *param); +void +toggle_insert_mode(WebKitWebView *page, const gchar *param); static void load_uri (WebKitWebView * web_view, const gchar *param); @@ -355,6 +362,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); |