aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/scripts/linkfollow.js
blob: 2adc03bdc7795df128b5d91cfdc658c6b7c44905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// 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.parentNode.insertBefore(h,item);
			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 next(elem){
	do {
		elem = elem.nextSibling;
	} while (elem && elem.nodeType != 1);
	return elem;
}
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(next(item))){
			matched.push(next(item));
		}
	}
	clearHints();
	if(matched.length == 1) {
		var item = matched[0];
	} else {
		var item = matched[parseInt(m.numbers,10)-1];
	}
    if (item) {
			item.style.borderStyle = "dotted";
			item.style.borderWidth = "thin";

        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;
        }
    }
}