summaryrefslogtreecommitdiff
path: root/utils/surf/script.js
blob: f33fca3b209e65a389c54d2d4fc4c96d3feaf957 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// ==UserScript==
// @name vimkeybindings
// @namespace renevier.fdn.fr
// @author arno <arenevier@fdn.fr>
// @licence GPL/LGPL/MPL
// @description use vim keybingings (i, j, k, l, …) to navigate a web page.
// ==/UserScript==
/*
* If you're a vim addict, and you always find yourself typing j or k in a web
* page, then wondering why it just does not go up and down like any good
* software, that user script is what you have been looking for.
*/
function up() {
    if (window.scrollByLines)
	window.scrollByLines(-1); // gecko
    else
	window.scrollBy(0, -12); // webkit
}
function down() {
    if (window.scrollByLines)
	window.scrollByLines(1); // gecko
    else
	window.scrollBy(0, 12); // webkit
}
function pageup() {
    if (window.scrollByPages)
	window.scrollByPages(-1); // gecko
    else
	window.scrollBy(0, 0 - _pageScroll()); // webkit
}
function pagedown() {
    if (window.scrollByPages)
	window.scrollByPages(1); // gecko
    else
	window.scrollBy(0, _pageScroll()); // webkit
}
function right() {
    window.scrollBy(15, 0);
}
function left() {
    window.scrollBy(-15, 0);
}
function home() {
    window.scroll(0, 0);
}
function bottom() {
    window.scroll(document.width, document.height);
}
// If you don't like default key bindings, customize here. 
// if you want to use the combination 'Ctrl + b' (for example), use '^b'
var bindings = {
    'h' : left, 
    'l' : right,
    'k' : up,
    'j' : down,
    'g' : home,
    'G' : bottom,
    //'^b': pageup,
    //'^f': pagedown,
}
function isEditable(element) {
    
    if (element.nodeName.toLowerCase() == "textarea")
	return true;
    // we don't get keypress events for text input, but I don't known
    // if it's a bug, so let's test that
    if (element.nodeName.toLowerCase() == "input" && element.type == "text")
	return true;
    // element is editable
    if (document.designMode == "on" || element.contentEditable == "true") {
	return true;
    }
    
    return false;
}
function keypress(evt) {
    var target = evt.target;
	    
    // if we're on a editable element, we probably don't want to catch
    // keypress, we just want to write the typed character.
    if (isEditable(target))
	return;
    var key = String.fromCharCode(evt.charCode);
    if (evt.ctrlKey) {
	key = '^' + key;
    }
    var fun = bindings[key];
    if (fun)
	fun();
}
function _pageScroll() {
    // Gecko algorithm
    // ----------------
    // The page increment is the size of the page, minus the smaller of
    // 10% of the size or 2 lines.  
    return window.innerHeight - Math.min(window.innerHeight / 10, 24);
}
window.addEventListener("keypress", keypress, false);

// easy links for surf
// christian hahn <ch radamanthys de>, sep 2010
testcomplete = function() {
	if(document.readyState=="complete") {
		run(); return;
	}
	window.setTimeout("testcomplete()",200);
}
testcomplete();
run=function() {
	// config , any css
	var modkey      = 18;  //ctrl=17, alt=18
	var cancelkey   = 67;  // c
	var newwinkey   = 84;  // t
	var openkey     = 70;  // f
	var label_style = {"color":"black","fontSize":"10px","backgroundColor":"#27FF27","fontWeight":"normal","margin":"0px","padding":"0px","position":"absolute","zIndex":99};
	var hl_style    = {"backgroundColor":"#E3FF38","fontSize":"15px"};
	var nr_base     = 5;   // >=10 : normal integer,
	// globals
	var ankers     = document.getElementsByTagName("a");
	var labels     = new Object();
	var ui_visible = false;
	var input      = "";
	// functions
	hl=function(t) {
		for(var id in labels) {
			if (t && id.match("^"+t)==t)
				for(var s in hl_style)
					labels[id].rep.style[s]=hl_style[s];
			else
				for(var s in label_style)
					labels[id].rep.style[s]=label_style[s];
		}
	}
	open_link=function(id, new_win) {
		try {
			var a = labels[input].a;
			if(a && !new_win) window.location.href=a.href;
			if(a && new_win)  window.open(a.href,a.href);
		} catch (e) {}
	}
	set_ui=function(s) {
		var pos = "static";
		ui_visible = true;
		if(s == "hidden") {
			ui_visible = false;
			pos = "absolute";
			input="";
		}
		for(var id in labels) {
			labels[id].rep.style.visibility=s;
			labels[id].rep.style.position=pos;
		}
	}
	base=function(n, b) { 
		if(b>=10) return n.toString();
		var res = new Array();
		while(n) {
			res.push( (n%b +1).toString() )
			n=parseInt(n/b);
		}
		return res.reverse().join("");
	}
	// main
	// create labels
	for (var i=0; i<ankers.length; i++) {
		var a = ankers[i];
		if (!a.href) continue;
		var b = base(i+1,nr_base);
		var d = document.createElement("span");
		d.style.visibility="hidden";
		d.innerHTML=b;
		for(var s in label_style)
			d.style[s]=label_style[s];
		labels[b]={"a":a, "rep":d};
		a.parentNode.insertBefore(d, a.nextSibling);
	}
	// set key handler   
	window.onkeydown=function(e) {
		if (e.keyCode == modkey) {
			set_ui("visible");
		}
	}
	window.onkeyup=function(e) {
		if (e.keyCode == modkey ) {
			open_link(input);
			set_ui("hidden");
			hl(input);
		} else if (ui_visible) {
			if(e.keyCode == newwinkey) {
				open_link(input, true);
				set_ui("hidden");
			} else if(e.keyCode == cancelkey)
				input="";
			else if(e.keyCode == openkey) {
				open_link(input);
				set_ui("hidden");
			} else
				input += String.fromCharCode(e.keyCode);
			hl(input);
		}
	}
}
(function() {
	window.addEventListener("click", function(e) {
		if (
		      e.button == 1 // for middle click
		      //|| e.ctrlKey   // for ctrl + click
		   ) {
			var new_uri = e.srcElement.href;
			if (new_uri) {
				e.stopPropagation();
				e.preventDefault();
				window.open(new_uri);
			}
		}
	}, false);
})();

/* See COPYING file for copyright, license and warranty details. */

(function() {
	document.addEventListener('keydown', keybind, false);
})();

function keybind(e) {
	if(e.altKey && String.fromCharCode(e.keyCode) == "R")
		simplyread();
}
/* See COPYING file for copyright, license and warranty details. */

if(window.content && window.content.document && window.content.document.simplyread_original === undefined) window.content.document.simplyread_original = false;

function simplyread(nostyle, nolinks)
{
	/* count the number of <p> tags that are direct children of parenttag */
	function count_p(parenttag)
	{
		var n = 0;
		var c = parenttag.childNodes;
		for (var i = 0; i < c.length; i++) {
			if (c[i].tagName == "p" || c[i].tagName == "P")
				n++;
		}
		return n;
	}
	
	var doc;
	doc = (document.body === undefined)
	      ? window.content.document : document;
	
	/* if simplyread_original is set, then the simplyread version is currently active,
	 * so switch to the simplyread_original html */
	if (doc.simplyread_original) {
		doc.body.innerHTML = doc.simplyread_original;
		for (var i = 0; i < doc.styleSheets.length; i++)
			doc.styleSheets[i].disabled = false;
		doc.simplyread_original = false
		return 0;
	}
	
	doc.simplyread_original = doc.body.innerHTML;
	doc.body.innerHTML = doc.body.innerHTML.replace(/<br[^>]*>\s*<br[^>]*>/g, "<p>");
	
	var biggest_num = 0;
	var biggest_tag;
	
	/* search for tag with most direct children <p> tags */
	var t = doc.getElementsByTagName("*");
	for (var i = 0; i < t.length; i++) {
		var p_num = count_p(t[i]);
		if (p_num > biggest_num) {
			biggest_num = p_num;
			biggest_tag = t[i];
		}
	}
	
	if (biggest_num == 0) return 1;
	
	/* save and sanitise content of chosen tag */
	var fresh = doc.createElement("div");
	fresh.innerHTML = biggest_tag.innerHTML;
	fresh.innerHTML = fresh.innerHTML.replace(/<\/?font[^>]*>/g, "");
	fresh.innerHTML = fresh.innerHTML.replace(/style="[^"]*"/g, "");
	if(nolinks)
		fresh.innerHTML = fresh.innerHTML.replace(/<\/?a[^>]*>/g, "");
	fresh.innerHTML = fresh.innerHTML.replace(/<\/?span[^>]*>/g, "");
	fresh.innerHTML = fresh.innerHTML.replace(/<style[^>]*>/g, "<style media=\"aural\">"); /* ensures contents of style tag are ignored */
	
	for (var i = 0; i < doc.styleSheets.length; i++)
		doc.styleSheets[i].disabled = true;
	
	srstyle =
		"p{margin:0ex auto;} h1,h2,h3,h4{font-weight:normal}" +
		"p+p{text-indent:2em;} body{background:#cccccc none}" +
		"img{display:block; max-width: 32em; padding:1em; margin: auto}" +
		"h1{text-align:center;text-transform:uppercase}" +
		"div#sr{width:34em; padding:8em; padding-top:2em;" +
		"  background-color:white; margin:auto; line-height:1.4;" +
		"  text-align:justify; font-family:serif; hyphens:auto;}";
		/* text-rendering:optimizeLegibility; - someday this will work,
		 *   but at present it just ruins justify, so is disabled */
	
	doc.body.innerHTML =
		"<style type=\"text/css\">" + (nostyle ? "" : srstyle) + "</style>" +
		"<div id=\"sr\">" + "<h1>"+doc.title+"</h1>" + fresh.innerHTML + "</div>";
	
	return 0;
}