aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/scripts/linkfollow.js
blob: 327b82cb4e34e6a350738f057a377f2006621a28 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// 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 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)
//
// when script is loaded, it can be invoked with
// bind f* = js hints.set("%s")
// bind f_ = js hints.follow("%s")
//
// To enable hint highlighting, add:
// set stylesheet_uri = /usr/share/uzbl/examples/data/style.css
//
// based on follow_Numbers.js
//
// TODO: set CSS styles (done, but not working properly)
// TODO: load the script as soon as the DOM is ready



function Hints(){
	var uzblid = 'uzbl_hint';
	var uzblclass = "uzbl_hint_class";
	var uzblclassfirst = "uzbl_hint_first";
	var doc = document;
	this.set = setHints;
	this.follow = followHint;
	this.keyPressHandler = keyPressHandler;

	function hasClass(ele,cls) {
			return ele.className.split(/ /).some(function (n) { return n == cls });
	}
	 
	function addClass(ele,cls) {
			if (!hasClass(ele,cls)) ele.className += " "+cls;
	}
	 
	function removeClass(ele,cls) {
		ele.className = ele.className.split(/ /).filter(function (n) { n != cls}).join(" ");
	}

	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(pos, label) {
			var hint = doc.createElement('div');
			hint.setAttribute('name', uzblid);
			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 = '1000';
			hint.style.left = pos[1] + 'px';
			hint.style.top = pos[0] + 'px';
			//var img = el.getElementsByTagName('img');
			//if (img.length > 0) {
			//    hint.style.left = pos[1] + img[0].width / 2 + 'px';
			//}
			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(offset) {
			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.onkeyup = this.keyPressHandler;
		var re = new Matcher(r);
		clearHints();
		var hintdiv = doc.createElement('div');
		hintdiv.setAttribute('id', uzblid);
		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);
			var pos = elementPosition(item);
			if(re.test(item) && isVisible(item) && elementInViewport(pos)){
				var h = generateHint(pos,c);
				h.href = function () {return item};
				hintdiv.appendChild(h);
				if(c==1){
					addClass(item,uzblclassfirst);
		//			addClass(item,uzblclass);
				} else {
					addClass(item,uzblclass);
				}
				c++;
			}
		}
		if (document.body) {
				document.body.insertBefore(hintdiv,document.body.firstChild);
		}
	}

	function clearHints(){
		var hintdiv = doc.getElementById(uzblid);
		if(hintdiv){
			hintdiv.parentNode.removeChild(hintdiv);
		}
		var first = doc.getElementsByClassName(uzblclassfirst)[0];
		if(first){
			removeClass(first,uzblclassfirst);
		}
		// TODO: not all class attributes get removed
		var items = doc.getElementsByClassName(uzblclass);
		for (var i = 0; i<items.length; i++){
			removeClass(items[i],uzblclass);
		};
		
	}

	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 followHint(follow){
		var m = new Matcher(follow);
		var elements = doc.getElementsByClassName(uzblclass);
		// filter
		var matched = [];
		matched.push(doc.getElementsByClassName(uzblclassfirst)[0]);
		for (var i = 0; i < elements.length;i++){
			if(m.test(elements[i])){
				matched.push(elements[i]);
			}
		}
		clearHints();
		var n = parseInt(m.numbers,10);
		if(n){
			var item = matched[n-1];
		} else {
			var item = matched[0];
		}
		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;
				}
		}
	}
}
var hints;
//document.addEventListener("DOMContentLoaded",function () { hints = new Hints()},false);

var hints = new Hints();