-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotukdeals_keyboard_navigation.user.js
More file actions
98 lines (87 loc) · 2.63 KB
/
hotukdeals_keyboard_navigation.user.js
File metadata and controls
98 lines (87 loc) · 2.63 KB
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
// ==UserScript==
// @name HotUKDeals keyboard nav
// @description Navigates through HotUKDeals listings and moves pics to left hand side
// @version 0.3
// @author Chris Poole <chris@hackernet.co.uk>
// @namespace http://hackernet.co.uk/
// @homepage http://userscripts.org/scripts/show/12581
// @id hukdnav@hackernet.co.uk
// @run-at document-end
// @include *.hotukdeals.com*
// ==/UserScript==
var next = null; //Define this with the URL of the next page.
var prev = null; //Difine this with the URL for the previous page.
var dealid = -1;
var listings = new Array();
var oldbg;
document.addEventListener('keydown', keyListener, false); //Add a listener for the keyboard
var links = document.getElementsByTagName('a');
for (var i = 0, l = links.length; i < l; i++) {
if (links[i].innerHTML == "Next") {
next = links[i].href;
}
if (links[i].innerHTML == "Previous") {
prev = links[i].href;
}
}
var deals = document.getElementsByTagName("li");
for(var i = 0, l = deals.length; i < l; i++){
if (deals[i].hasAttribute("class")) {
if (deals[i].getAttribute("class").indexOf("item-listing") >= 0) {
listings.push(deals[i].getAttribute("id"));
}
}
}
function beforemove() {
if ((dealid >= 0) && (dealid < listings.length)) {
document.getElementById(listings[dealid]).style.background = oldbg;
}
}
function navto(it) {
var href = window.location.href;
if (href.indexOf("#") != -1) {
var index = href.indexOf("#") + 1;
window.location.href = href.substring(0, index) + it;
} else {
window.location.href = href + "#" + it;
}
}
function aftermove() {
if (dealid >= listings.length) {
// go forward a page
if (next) {
window.location.href = next;
}
} else if (dealid < 0) {
// go back a page
if (prev) {
window.location.href = prev;
}
} else {
navto(listings[dealid]);
oldbg = document.getElementById(listings[dealid]).style.background;
document.getElementById(listings[dealid]).style.background = '#f4f8ff';
}
}
function keyListener(k) {
if (k.keyCode == 74) { // j
beforemove();
dealid++;
aftermove();
}
if (k.keyCode == 75) { // k
beforemove();
dealid--;
aftermove();
}
}
function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
addGlobalStyle('.item-listing div.side { float: left; padding: 10px; } .item-listing div.side div { display: none; } .item-listing div.vote { background: inherit; } ');