ACM Portal のキーバインド for OPERA

ACM Portal をよく使うので練習がてらOPERA の user.js を書く.

shortcut.js と最近流行のJavaScript-XPath

j: 次の論文へ
k: 前の論文へ
n: 次の20件
p: 前の20件
Enter: 現在選択している論文の詳細を開く
という感じにしたい.

なんだかまだ不安定.
parentの6連続はいかがなものか...
JavaScript よく分からん.


//
// ==UserScript==
// @name shortcut for ACM portal
// @description shortcut for ACM portal
// @include http://portal.acm.org/results.cfm*
// ==/UserScript==

(function(){
var focus = null;
var links = null;
shortcut.add("j", next);
shortcut.add("k", previous);
shortcut.add("n", next_page);
shortcut.add("p", previous_page);
shortcut.add("Enter", open);


function next() {
if (links == null) {
links = document.evaluate("//td/a[@class='medium-text']", document, null, 7, null);
}
if (focus == null) {
focus = 0;
} else if (focus >= links.snapshotLength - 1) {
// do nothing
} else {
window.scrollBy(0, links.snapshotItem(focus).parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.offsetHeight + 10);

links.snapshotItem(focus).parentNode.removeAttribute("bgcolor");
focus = focus + 1;
}
var link = links.snapshotItem(focus);
link.parentNode.setAttribute("bgcolor","#B0E0E6");
}

function previous() {
if (links == null) {
links = document.evaluate("//td/a[@class='medium-text']", document, null, 7, null);
}
if (focus == null) {
focus = 0;
} else if (focus <= 0) {
// do nothing
} else {
window.scrollBy(0, -links.snapshotItem(focus).parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.offsetHeight - 10);
links.snapshotItem(focus).parentNode.removeAttribute("bgcolor");
focus = focus - 1;
}
var link = links.snapshotItem(focus);
link.parentNode.setAttribute("bgcolor","#B0E0E6");
}

function next_page() {
links = document.evaluate("//a[normalize-space(text()) = 'next']", document, null, 7, null);
window.location = links.snapshotItem(0);
}

function previous_page() {
links = document.evaluate("//a[normalize-space(text()) = 'previous']", document, null, 7, null);
window.location = links.snapshotItem(0);
}

function open() {
if (focus != null) {
window.open(links.snapshotItem(focus));
}
}

})();