Commit 87c31b7e authored by Iain Murray's avatar Iain Murray
Browse files

Add links to adjacent sessions in page too.

Especially helps with userscript and on android.
Code quite messy / dumb.
parent bdfa84d3
......@@ -7,13 +7,13 @@ const URL = location.href;
const match = URL.match(/[0-9]{2}-[0-9]{2}(?=\/)/);
const SESSION = match && match[0];
// Navigate sessions with ctrl-left and ctrl-right
if (SESSION) {
const y1 = Number(SESSION.slice(0, 2));
const prev = sessionString(y1 - 1);
const next = sessionString(y1 + 1);
// Navigate sessions with ctrl-left and ctrl-right
for (const [key, newSession] of [['Right', next], ['Left', prev], ['Up', thisSession]]) {
document.addEventListener('keyup', e => {
if ((e.code == ('Arrow' + key)) && (e.ctrlKey || e.metaKey)) {
......@@ -21,6 +21,57 @@ if (SESSION) {
}
});
}
// Add navigation links to year in title (if present)
let h1 = document.getElementsByTagName('h1');
h1 = h1 && h1[0];
let canary = document.getElementById('drps_arrows_touched');
if (h1 && !canary) {
const year_re = /(20[0-9][0-9].20[0-9][0-9])/;
const year_match = h1.textContent.match(year_re);
if (year_match) {
const year_str = year_match[0];
h1.textContent = h1.textContent.replace(year_re, '');
//
let leftNode = document.createElement('a');
leftNode.textContent = '';
leftNode.style.textDecoration = 'none';
leftNode.style.color = 'white';
leftNode.style.fontWeight = 'bold';
leftNode.href = URL.replace(SESSION, prev);
leftNode.title = 'Previous session, Ctrl+left';
h1.appendChild(leftNode);
//
let pageY1 = Number(SESSION.slice(0, 2));
let nowY1 = Number(thisSession.slice(0, 2));
let yearNode;
if (pageY1 < nowY1) {
yearNode = document.createElement('a');
yearNode.href = URL.replace(SESSION, thisSession);
yearNode.title = 'Go to current session ' + thisSession + ', Ctrl+up';
yearNode.style.color = 'rgb(255, 35, 35)';
} else if (pageY1 > nowY1) {
yearNode = document.createElement('a');
yearNode.href = URL.replace(SESSION, thisSession);
yearNode.title = 'Go to current session ' + thisSession + ', Ctrl+up';
yearNode.style.color = 'rgb(35, 200, 35)';
} else {
yearNode = document.createElement('span');
}
yearNode.textContent = year_str;
yearNode.id = 'drps_arrows_touched';
h1.appendChild(yearNode);
//
let rightNode = document.createElement('a');
rightNode.textContent = '';
rightNode.style.textDecoration = 'none';
rightNode.style.color = 'white';
rightNode.style.fontWeight = 'bold';
rightNode.href = URL.replace(SESSION, next);
rightNode.title = 'Next session, Ctrl+right';
h1.appendChild(rightNode);
}
}
}
......
......@@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "DRPS Arrows",
"version": "0.0.2",
"version": "0.0.3",
"description": "Adds navigate button and keyboard shortcuts for browsing the DRPS. Creates links in plain-text DRPS descriptors from course codes.",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment