From 6e15eb4b1a9bbec813a21fc55eccadddb49fe396 Mon Sep 17 00:00:00 2001 From: Sylvester Johansson Date: Thu, 28 May 2009 13:06:05 +0200 Subject: * added requireReturn setting * changed the hinting functions arguments --- examples/scripts/linkfollow.js | 69 ++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 32 deletions(-) (limited to 'examples') diff --git a/examples/scripts/linkfollow.js b/examples/scripts/linkfollow.js index 472f85d..2b381cc 100644 --- a/examples/scripts/linkfollow.js +++ b/examples/scripts/linkfollow.js @@ -2,14 +2,12 @@ // 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 /usr/share/examples/scripts/linkfollow.js" -// (currently, it is recommended to use load_finish_handler since the JS code seems to get -// flushed. Using a load_start_handler with a 1s delay works but not always) +// One way would be to use the load_commit_handler: +// set load_commit_handler = sh 'echo "act script /usr/share/uzbl/examples/scripts/linkfollow.js" > "$4"' // // when script is loaded, it can be invoked with -// bind f* = js hints.set("%s") -// bind f_ = js hints.follow("%s") +// bind f* = js hints.set("%s", hints.open) +// bind f_ = js hints.follow("%s",hints.open) // // At the moment, it may be useful to have way of forcing uzbl to load the script // bind :lf = script /usr/share/examples/scripts/linkfollow.js @@ -21,10 +19,17 @@ // based on follow_Numbers.js // // TODO: fix styling for the first element -// TODO: load the script as soon as the DOM is ready +// TODO: emulate mouseover events when visiting some elements +// TODO: rewrite the element->action handling function Hints(){ + + + // if set to true, you must explicitly call hints.follow(), otherwise it will + // follow the link if there is only one matching result + var requireReturn = true; + var uzblid = 'uzbl_hint'; var uzblclass = 'uzbl_highlight'; var uzblclassfirst = 'uzbl_h_first'; @@ -91,6 +96,7 @@ function Hints(){ this.node = node; this.isHinted = false; this.position = pos; + this.num = 0; this.addHint = function (labelNum) { // TODO: fix uzblclassfirst @@ -155,7 +161,7 @@ function Hints(){ } } - function update(str) { + function update(str,openFun) { var match = new Matcher(str); hintdiv = createHintDiv(); var i = 1; @@ -166,11 +172,16 @@ function Hints(){ } else { n.removeHint(); }}); + if(!requireReturn){ + if(i==2){ //only been incremented once + follow(str,openFun); + } + } } - function hint(str){ + function hint(str,openFun){ if(str.length == 0) init(); - update(str); + update(str,openFun); } function keyPressHandler(e) { @@ -181,32 +192,27 @@ function Hints(){ doc.body.removeAttribute("onkeyup"); } } - this.openInNewWindow = function(item){ - // TODO: this doesn't work - window.open(item.href,"uzbl new",""); + + this.openNewWindow = function(item){ + // TODO: this doesn't work yet + window.open(item.href,"uzblnew",""); } - this.openInThisWindow = function(item){ + this.open = function(item){ + simulateMouseOver(item); window.location = item.href; } -// found on stackoverflow -// function simulateMouseOver(item){ -// var evt = doc.createEvent("MouseEvents"); -// evt.initMouseEvent("mouseover",true,true, -// doc.defaultView,0,0,0,0,0, -// false,false,false,false,0,null); -// var canceled = !item.dispatchEvent(evt); -// if(canceled){ -// alert('Event Cancelled'); -// } -// } + function simulateMouseOver(item){ + var evt = doc.createEvent("MouseEvents"); + evt.initMouseEvent("MouseOver",true,true, + doc.defaultView,1,0,0,0,0, + false,false,false,false,0,null); + return item.dispatchEvent(evt); + } - function follow(str,opener){ + function follow(str,openFunction){ var m = new Matcher(str); - if(!opener){ - var opener = this.openInThisWindow; - } var items = visible.filter(function (n) { return n.isHinted }); clear(); var num = parseInt(m.numbers,10); @@ -226,7 +232,7 @@ function Hints(){ var name = item.tagName; if (name == 'A') { if(item.click) {item.click()}; - opener(item); + openFunction(item); } else if (name == 'INPUT') { var type = item.getAttribute('type').toUpperCase(); if (type == 'TEXT' || type == 'FILE' || type == 'PASSWORD') { @@ -240,14 +246,13 @@ function Hints(){ item.select(); } else { item.click(); - opener(item); + openFunction(item); } } } } var hints = new Hints(); -//document.attachEvent("onKeyUp",hints.keyPressHandler); // vim:set et sw=2: -- cgit v1.2.3