From fe7726c1abeb37b4715d50dd2aee66f27fe37aee Mon Sep 17 00:00:00 2001 From: Hans-Peter Deifel Date: Sun, 18 Jul 2010 14:29:37 +0200 Subject: [PATCH] Allow hints activation while the page is loading. Split the javascript code into two parts: One is loaded before the page and the other after it. --- Makefile | 6 ++--- input_hinting_mode.js => hinting.js | 42 +++++++---------------------- input-focus.js | 29 ++++++++++++++++++++ js-merge-helper.pl | 38 +++++++++++++++++--------- main.c | 6 +++-- 5 files changed, 71 insertions(+), 50 deletions(-) rename input_hinting_mode.js => hinting.js (87%) create mode 100644 input-focus.js diff --git a/Makefile b/Makefile index 2c3a814..f59fa28 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ MAN = vimprobable2.1 vimprobablerc.1 # Used libraries to get needed CFLAGS and LDFLAGS form pkg-config LIBS = gtk+-2.0 webkit-1.0 libsoup-2.4 # Files to removo by clean target -CLEAN = $(TARGET) $(OBJ) $(DEPS) hintingmode.h +CLEAN = $(TARGET) $(OBJ) $(DEPS) javascript.h # Files to install by install target or remove by uninstall target INSTALL = $(BINDIR)/$(TARGET) $(addprefix $(MANDIR)/man1/,$(MAN)) @@ -33,8 +33,8 @@ all: $(TARGET) -include $(DEPS) -main.o: hintingmode.h -hintingmode.h: input_hinting_mode.js +main.o: javascript.h +javascript.h: input-focus.js hinting.js perl ./js-merge-helper.pl $(TARGET): $(OBJ) diff --git a/input_hinting_mode.js b/hinting.js similarity index 87% rename from input_hinting_mode.js rename to hinting.js index 6118cd4..c59237a 100644 --- a/input_hinting_mode.js +++ b/hinting.js @@ -9,34 +9,6 @@ function clearfocus() { document.activeElement.blur(); } -function v(e, y) { - t = e.nodeName.toLowerCase(); - if((t == 'input' && /^(text|password|checkbox|radio)$/.test(e.type)) - || /^(select|textarea)$/.test(t) - || e.contentEditable == 'true') - console.log('insertmode_'+(y=='focus'?'on':'off')); -} - -if(document.activeElement) - v(document.activeElement,'focus'); - -m=['focus','blur']; - -if (document.getElementsByTagName("body")[0] !== null && typeof(document.getElementsByTagName("body")[0]) == "object") { - for(i in m) - document.getElementsByTagName('body')[0].addEventListener(m[i], function(x) { - v(x.target,x.type); - }, true); - - document.getElementsByTagName("body")[0].appendChild(document.createElement("style")); - document.styleSheets[0].addRule('.hinting_mode_hint', 'color: #000; background: #ff0;'); - document.styleSheets[0].addRule('.hinting_mode_hint_focus', 'color: #000; background: #8f0;'); -} - -self.onunload = function() { - v(document.activeElement, ''); -}; - function show_hints(inputText) { if (document.getElementsByTagName("body")[0] !== null && typeof(document.getElementsByTagName("body")[0]) == "object") { var height = window.innerHeight; @@ -59,10 +31,11 @@ function show_hints(inputText) { }, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); div = document.createElement("div"); /* due to the different XPath result type, we will need two counter variables */ - j = 1; + j = 0; var i; a = []; colors = []; + backgrounds = []; for (i = 0; i < r.snapshotLength; i++) { var elem = r.snapshotItem(i); @@ -91,8 +64,10 @@ function show_hints(inputText) { div.appendChild(hint); /* remember site-defined colour of this element */ colors[j] = elem.style.color; + backgrounds[j] = elem.style.background; /* make the link black to ensure it's readable */ elem.style.color = "#000"; + elem.style.background = "#ff0"; j++; } i = 0; @@ -148,6 +123,7 @@ function cleanup() a[e].className = a[e].className.replace(/hinting_mode_hint/,''); /* reset to site-defined colour */ a[e].style.color = colors[e]; + a[e].style.background = backgrounds[e]; } } div.parentNode.removeChild(div); @@ -159,7 +135,7 @@ function clear() console.log("hintmode_off") } -function update_hints(n) +function update_hints(n) { if(h != null) h.className = h.className.replace("_focus",""); @@ -167,8 +143,10 @@ function update_hints(n) /* return signal to follow the link */ return "fire;" + n; } else - if (typeof(a[n - 1]) != "undefined") + if (typeof(a[n - 1]) != "undefined") { (h = a[n - 1]).className = a[n - 1].className.replace("hinting_mode_hint", "hinting_mode_hint_focus"); + h.style.background = "8f0"; + } } function focus_input() @@ -202,7 +180,7 @@ function focus_input() if (j == 0) { /* no appropriate field found focused - focus the first one */ if (first !== null) { - first.focus(); + first.focus(); var tag = elem.nodeName.toLowerCase(); if (tag == "textarea" || tag == "input") console.log('insertmode_on'); diff --git a/input-focus.js b/input-focus.js new file mode 100644 index 0000000..0ef8817 --- /dev/null +++ b/input-focus.js @@ -0,0 +1,29 @@ +/* + (c) 2009 by Leon Winter + (c) 2009 by Hannes Schueller + see LICENSE file +*/ + +function v(e, y) { + t = e.nodeName.toLowerCase(); + if((t == 'input' && /^(text|password|checkbox|radio)$/.test(e.type)) + || /^(select|textarea)$/.test(t) + || e.contentEditable == 'true') + console.log('insertmode_'+(y=='focus'?'on':'off')); +} + +if(document.activeElement) + v(document.activeElement,'focus'); + +m=['focus','blur']; + +if (document.getElementsByTagName("body")[0] !== null && typeof(document.getElementsByTagName("body")[0]) == "object") { + for(i in m) + document.getElementsByTagName('body')[0].addEventListener(m[i], function(x) { + v(x.target,x.type); + }, true); +} + +self.onunload = function() { + v(document.activeElement, ''); +}; diff --git a/js-merge-helper.pl b/js-merge-helper.pl index 7ea5663..050d871 100644 --- a/js-merge-helper.pl +++ b/js-merge-helper.pl @@ -1,21 +1,33 @@ #!/bin/env perl -w use strict; -my $js; -open(JSFILE, "input_hinting_mode.js") or die "Failed to open file: $!"; +sub escape_c_string { + shift; + s/\t|\r|\n/ /g; # convert spacings to whitespaces + s/[^\/]\/\*.*?\*\///g; # remove comments (careful: hinttags look like comment!) + s/ {2,}/ /g; # strip whitespaces + s/(^|\(|\)|;|,|:|\}|\{|=|\+|\-|\*|\&|\||\<|\>|!) +/$1/g; + s/ +($|\(|\)|;|,|:|\}|\{|=|\+|\-|\*|\&|\||\<|\>|!)/$1/g; + s/\\/\\\\/g; # escape backslashes + s/\"/\\\"/g; # escape quotes + return $_ +} + +open(JSFILE, "hinting.js") or die "Failed to open file: $!"; $_ = do { local $/; }; close(JSFILE); -s/\t|\r|\n/ /g; # convert spacings to whitespaces -s/[^\/]\/\*.*?\*\///g; # remove comments (careful: hinttags look like comment!) -s/ {2,}/ /g; # strip whitespaces -s/(^|\(|\)|;|,|:|\}|\{|=|\+|\-|\*|\&|\||\<|\>|!) +/$1/g; -s/ +($|\(|\)|;|,|:|\}|\{|=|\+|\-|\*|\&|\||\<|\>|!)/$1/g; -s/\\/\\\\/g; # escape backslashes -s/\"/\\\"/g; # escape quotes -$js = $_; +my $js_hints = escape_c_string($_); -open(HFILE, ">hintingmode.h") or die "Failed to open hintingmode.h: $!"; -print HFILE "#define JS_SETUP "; -printf HFILE "\"%s\"\n", $js; +open(JSFILE, "input-focus.js") or die "Failed to open file: $!"; +$_ = do { local $/; }; +close(JSFILE); +my $js_input = escape_c_string($_); + +open(HFILE, ">javascript.h") or die "Failed to open javascript.h: $!"; +print HFILE "#define JS_SETUP_HINTS "; +printf HFILE "\"%s\"\n", $js_hints; +print HFILE "#define JS_SETUP_INPUT_FOCUS "; +printf HFILE "\"%s\"\n", $js_input; close(HFILE); + exit; diff --git a/main.c b/main.c index 53931e6..a3cf8d5 100644 --- a/main.c +++ b/main.c @@ -9,7 +9,7 @@ #include "vimprobable.h" #include "utilities.h" #include "callbacks.h" -#include "hintingmode.h" +#include "javascript.h" /* remove numlock symbol from keymask */ #define CLEAN(mask) (mask & ~(GDK_MOD2_MASK) & ~(GDK_BUTTON1_MASK) & ~(GDK_BUTTON2_MASK) & ~(GDK_BUTTON3_MASK) & ~(GDK_BUTTON4_MASK) & ~(GDK_BUTTON5_MASK)) @@ -171,14 +171,16 @@ ascii_bar(int total, int state, char *string) { void webview_load_committed_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data) { + Arg a = { .i = Silent, .s = JS_SETUP_HINTS }; const char *uri = webkit_web_view_get_uri(webview); update_url(uri); + script(&a); } void webview_load_finished_cb(WebKitWebView *webview, WebKitWebFrame *frame, gpointer user_data) { - Arg a = { .i = Silent, .s = JS_SETUP }; + Arg a = { .i = Silent, .s = JS_SETUP_INPUT_FOCUS }; if (HISTORY_MAX_ENTRIES > 0) history();