Skip to content

Instantly share code, notes, and snippets.

@billmei
Forked from zzeleznick/piazza_hotkeys.js
Last active September 17, 2019 23:51
Show Gist options
  • Save billmei/01155814e7c1e78b431a5ee30a727893 to your computer and use it in GitHub Desktop.
Save billmei/01155814e7c1e78b431a5ee30a727893 to your computer and use it in GitHub Desktop.
Provides hotkey support for piazza. Paste into the Chrome Dev Console for use. This fork changes the keycodes to Gmail/Vim defaults.
(function() {
/*
Provides hotkey support for navigating many posts on piazza.
1) e + alt: archives the current post and navigates to the previous post
2) u + alt: marks the current post as unread and navigates to the previous post
3) k + alt: navigates to the more recent post from current
4) j + alt: navigates to the older post
*/
// keycodes: http://keycode.info/
const keyArchive = 69; // alt + e
const keyUnread = 85; // alt + u
const keyGoUp = 75; // alt + k
const keyGoDown = 74; // alt + j
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 == keyArchive || code == keyUnread) && 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 == keyArchive) {
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 == keyGoUp || code == keyGoDown) && alt) { // up + alt, keyGoDown + alt
event.preventDefault();
current = getSelected();
if (current) {
if (code == keyGoUp) {
current.previousElementSibling.click();
}
else {
current.nextElementSibling.click();
}
}
else {
console.log("No question selected");
}
}
else {
// pass
}
});
})();
// Prepend `javascript:` to save this as a bookmarklet.
!function(){const e=function(){return s=document.querySelectorAll("li.selected"),s.length>0?s[0]:null};document.addEventListener("keydown",function(l){const n=l.keyCode,o=l.altKey;if(69!=n&&85!=n||!o)75!=n&&74!=n||!o||(l.preventDefault(),(c=e())?75==n?c.previousElementSibling.click():c.nextElementSibling.click():console.log("No question selected"));else{var t=window.location.search.split("="),i=t.length>1?t[t.length-1]:"?",c=e();if(c){var s=c.previousElementSibling,d=document.getElementById("FeedItemDropdown_"+c.id);if(69==n){var r=d.children[1].children[1];r.click(),r||console.log("Failed to archive post "+i)}else{var a=d.children[1].children[3];a.click(),a||console.log("Failed to mark post "+i+"as unread")}s&&s.click()}else console.log("Did not select a question")}})}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment