Skip to content

Instantly share code, notes, and snippets.

@zzeleznick
Created September 6, 2016 16:39
Show Gist options
  • Save zzeleznick/c64e89ec895d90ba4e36acb5d2adb17c to your computer and use it in GitHub Desktop.
Save zzeleznick/c64e89ec895d90ba4e36acb5d2adb17c to your computer and use it in GitHub Desktop.
Provides hotkey support for piazza. Paste into the Chrome Dev Console for use.
var piazza_hotkeys = function() {
/*
Provides hotkey support for navigating many posts on piazza.
1) a + alt: archives the current post and navigates to the previous post
2) i + alt: marks the current post as unread and navigates to the previous post
3) up + alt: navigates to the more recent post from current
4) down + alt: navigates to the older post
*/
// keycodes: https://css-tricks.com/snippets/javascript/javascript-keycodes/
const A = 65;
const I = 73;
const up = 38;
const down = 40;
const getSelected = function() {
s = document.querySelectorAll("li.selected");
return s.length > 0 ? s[0] : null;
};
document.addEventListener("keydown", function(event) {
const code = event.keyCode;
const alt = event.altKey;
if ( (code == A || code == I) && alt) { // alt key is pressed
var split = window.location.search.split("=");
var questionID = split.length > 1 ? split[split.length - 1] : "?";
var current = getSelected();
if (current) {
var prev = current.previousElementSibling;
var dropdown = document.getElementById('FeedItemDropdown_' + current.id);
if (code == A) {
var archive = dropdown.children[1].children[1];
archive.click();
if (!archive) {
console.log('Failed to archive post ' + questionID);
}
}
else {
var unread = dropdown.children[1].children[3];
unread.click();
if (!unread) {
console.log('Failed to mark post ' + questionID + 'as unread');
}
}
if (prev) { // go to the next most recent post.
prev.click();
}
}
else {
console.log("Did not select a question");
}
}
else if ( (code == up || code == down) && alt) { // up + alt, down + alt
event.preventDefault();
current = getSelected();
if (current) {
if (code == up) {
current.previousElementSibling.click();
}
else {
current.nextElementSibling.click();
}
}
else {
console.log("No question selected");
}
}
else {
// pass
}
});
}
piazza_hotkeys();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment