diff options
author | Dieter Plaetinck <dieter@plaetinck.be> | 2009-05-26 21:57:04 +0200 |
---|---|---|
committer | Dieter Plaetinck <dieter@plaetinck.be> | 2009-05-26 21:57:04 +0200 |
commit | 9a5c778d809f1d8e9fb016e56fae96cc41befa5f (patch) | |
tree | 824712b804194623beb9ef77c1e1698771e68d8b | |
parent | 10c5302f00d3339bd45a47b23075e9ea64a8d0ab (diff) | |
parent | aa66355dde399f9e2868a80617acca96213c03fe (diff) |
Merge commit 'gcj/master' into experimental
-rwxr-xr-x | examples/scripts/formfiller.pl | 99 | ||||
-rw-r--r-- | examples/scripts/linkfollow.js | 175 |
2 files changed, 274 insertions, 0 deletions
diff --git a/examples/scripts/formfiller.pl b/examples/scripts/formfiller.pl new file mode 100755 index 0000000..4c351b5 --- /dev/null +++ b/examples/scripts/formfiller.pl @@ -0,0 +1,99 @@ +#!/usr/bin/perl + +# a slightly more advanced form filler +# +# uses settings file like: $keydir/<domain> + +# user arg 1: +# edit: force editing of the file (fetches if file is missing) +# load: fill forms from file (fetches if file is missing) +# new: fetch new file + +# usage example: +# bind LL = spawn /usr/share/uzbl/examples/scripts/formfiller2.pl load +# bind LN = spawn /usr/share/uzbl/examples/scripts/formfiller2.pl new +# bind LE = spawn /usr/share/uzbl/examples/scripts/formfiller2.pl edit + +use strict; +use Switch; + +my $keydir = $ENV{XDG_CONFIG_HOME} . "/uzbl/keys"; +my ($config,$pid,$xid,$fifo,$socket,$url,$title,$cmd) = @ARGV; +if($fifo eq "") { die "No fifo"; }; + +sub domain { + my ($url) = @_; + $url =~ s#http(s)?://([A-Za-z0-9\.-]+)(/.*)?#$2#; + return $url; +}; + +#my $editor = "xterm -e vim"; +my $editor = "gvim"; + +# ideally, there would be some way to ask uzbl for the html content instead of having to redownload it with +# Also, you may need to fake the user-agent on some sites (like facebook) + my $downloader = "curl -A 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042810 GranParadiso/3.0.10' "; +#my $downloader = "curl -s"; + +my @fields = ("type","name","value"); + +my %command; + +$command{load} = sub { + my ($domain) = @_; + my $file = "$keydir/$domain"; + if( -e $file){ + open(FH,$file); + my (@lines) = <FH>; + close(FH); + $|++; + open(FIFO,">>$fifo"); + print "opened $fifo\n"; + foreach my $line (@lines) { + if($line !~ m/^#/){ + my ($type,$name,$value) = ($line =~ /\s*(\w+)\s*\|\s*(.*?)\s*\|\s*(.*?)\s*$/); + switch ($type) { + case "" {} + case "checkbox" { printf FIFO 'act js document.getElementsByName("%s")[0].checked = %s;', $name, $value} + case "submit" { printf FIFO 'act js function fs (n) {try{n.submit()} catch (e){fs(n.parentNode)}}; fs(document.getElementsByName("%s")[0]);', $name } + else { printf FIFO 'act js document.getElementsByName("%s")[0].value = "%s";', $name, $value} + } + + print FIFO "\n"; + } + } + $|--; + close(FIFO); + } else { + $command{new}->($domain); + $command{edit}->($domain); + } +}; +$command{edit} = sub { + my ($domain) = @_; + my $file = "$keydir/$domain"; + if(-e $file){ + system ($editor, $file); + } else { + $command{new}->($domain); +} +}; +$command{new} = sub { + my ($domain) = @_; + my $file = "$keydir/$domain"; + open(FILE,">>$file"); + $|++; + print FILE "#make sure that there are no extra submits, since it may trigger the wrong one\n"; + printf FILE "#%-10s | %-10s | %s\n", @fields; + print FILE "#------------------------------\n"; + my @data = `$downloader $url`; + foreach my $line (@data){ + if($line =~ m/<input ([^>].*?)>/i){ + $line =~ s/.*(<input ([^>].*?)>).*/\1/; + printf FILE " %-10s | %-10s | %s\n", map { my ($r) = $line =~ /.*$_=["'](.*?)["']/;$r } @fields; + }; + }; + close(FILE); + $|--; +}; +$command{$cmd}->(domain($url)); diff --git a/examples/scripts/linkfollow.js b/examples/scripts/linkfollow.js new file mode 100644 index 0000000..9b7d811 --- /dev/null +++ b/examples/scripts/linkfollow.js @@ -0,0 +1,175 @@ +// link follower for uzbl +// requires http://github.com/DuClare/uzbl/commit/6c11777067bdb8aac09bba78d54caea04f85e059 +// +// first, it needs to be loaded before every time it is used. +// One way would be to use something like load_start_handler to send +// "act script link_follower.js" +// +// when script is loaded, it can be invoked with +// bind f* = js setHints("%s") +// bind f_ = js followLink("%s") +// +// based on follow_Numbers.js +// +// TODO: add classes to hinted elements + + +var uzblid = 'uzbl_hint'; +var uzblclass = 'uzbl_hint_class' + +var doc = document; + +function elementPosition(el) { + var up = el.offsetTop; + var left = el.offsetLeft; var width = el.offsetWidth; + var height = el.offsetHeight; + + while (el.offsetParent) { + el = el.offsetParent; + up += el.offsetTop; + left += el.offsetLeft; + } + return [up, left, width, height]; +} + +function generateHint(el, label) { + var hint = doc.createElement('div'); + hint.setAttribute('class', uzblclass); + hint.innerText = label; + hint.style.display = 'inline'; + hint.style.backgroundColor = '#B9FF00'; + hint.style.border = '2px solid #4A6600'; + hint.style.color = 'black'; + hint.style.fontSize = '9px'; + hint.style.fontWeight = 'bold'; + hint.style.lineHeight = '9px'; + hint.style.margin = '0px'; + hint.style.padding = '1px'; + hint.style.position = 'absolute'; + hint.style.zIndex = '10000'; + hint.style.textDecoration = 'none'; + hint.style.webkitBorderRadius = '6px'; + // Play around with this, pretty funny things to do :) + hint.style.webkitTransform = 'scale(1) rotate(0deg) translate(-6px,-5px)'; + return hint; +} + +function elementInViewport(el) { + offset = elementPosition(el); + var up = offset[0]; + var left = offset[1]; + var width = offset[2]; + var height = offset[3]; + return (up < window.pageYOffset + window.innerHeight && + left < window.pageXOffset + window.innerWidth + && (up + height) > window.pageYOffset + && (left + width) > window.pageXOffset); +} + +function isVisible(el) { + + if (el == doc) { return true; } + if (!el) { return false; } + if (!el.parentNode) { return false; } + if (el.style) { + if (el.style.display == 'none') { + return false; + } + if (el.style.visibility == 'hidden') { + return false; + } + } + return isVisible(el.parentNode); +} + +var hintable = "//a[@href] | //img | //input"; + +function Matcher(str){ + var numbers = str.replace(/[^\d]/g,""); + var words = str.replace(/\d/g,"").split(/\s+/).map(function (n) { return new RegExp(n,"i")}); + this.test = test; + this.toString = toString; + this.numbers = numbers; + function test(element) { + // test all the regexp + return words.every(function (regex) { return element.textContent.match(regex)}); + } + function toString(){ + return "{"+numbers+"},{"+words+"}"; + } +} + + +function setHints(r){ + if(doc.body) doc.body.setAttribute("onkeyup","keyPressHandler(event)"); + var re = new Matcher(r); + clearHints(); + var c = 1; + var items = doc.evaluate(hintable,doc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null); + for (var i = 0; i < items.snapshotLength;i++){ + var item = items.snapshotItem(i); + if(re.test(item) && isVisible(item) && elementInViewport(item)){ + var h = generateHint(item,c); + item.appendChild(h); + c++; + } + } +} + +function clearHints(){ + var items = doc.evaluate("//div[@class='" + uzblclass + "']",doc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null); + for (var i = 0; i < items.snapshotLength;i++){ + var item = items.snapshotItem(i); + item.parentNode.removeChild(item); + } +} + +function keyPressHandler(e) { + var kC = window.event ? event.keyCode: e.keyCode; + var Esc = window.event ? 27 : e.DOM_VK_ESCAPE; + if (kC == Esc) { + clearHints(); + doc.body.removeAttribute("onkeyup"); + } +} +function followLink(follow){ + var m = new Matcher(follow); + var elements = doc.evaluate("//*/div[@class='"+uzblclass+"']",doc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null); + // filter + var matched = []; + for (var i = 0; i < elements.snapshotLength;i++){ + var item = elements.snapshotItem(i); + if(m.test(item.parentNode)){ + matched.push(item.parentNode); + } + } + clearHints(); + if(matched.length == 1) { + var item = matched[0]; + } else { + var item = matched[parseInt(m.numbers,10)-1]; + } + if (item) { + item.style.backgroundColor = "blue"; + + var name = item.tagName; + if (name == 'A') { + if(item.click) {item.click()}; + window.location = item.href; + } else if (name == 'INPUT') { + var type = item.getAttribute('type').toUpperCase(); + if (type == 'TEXT' || type == 'FILE' || type == 'PASSWORD') { + item.focus(); + item.select(); + } else { + item.click(); + } + } else if (name == 'TEXTAREA' || name == 'SELECT') { + item.focus(); + item.select(); + } else { + item.click(); + window.location = item.href; + } + } +} |