Skip to content

Instantly share code, notes, and snippets.

@jmglov
Last active May 3, 2021 12:58
Show Gist options
  • Save jmglov/c0c6b43944028807adcb8d2536b8fbbc to your computer and use it in GitHub Desktop.
Save jmglov/c0c6b43944028807adcb8d2536b8fbbc to your computer and use it in GitHub Desktop.
Userscript to give Gmail a Hey-like UX
// ==UserScript==
// @name Tja
// @version 1
// @grant none
// ==/UserScript==
const elementType = {
bottomBar: 'aeG',
categoryList: 'nH oy8Mbf nn aeN',
chromeDivs: 'nH nn',
readEmail: 'zA yO',
scrollBar: 'aeJ',
searchBar: 'nH oy8Mbf qp',
unreadEmail: 'zA zE'
};
function getElementByClass(className) {
return document.getElementsByClassName(className)[0];
}
function getElementsByLiteralClassName(className) {
return Array.prototype.filter.call(
document.getElementsByClassName(className),
el => el.className == className
);
}
function manipulateElements(className, fn) {
Array.prototype.forEach.call(
document.getElementsByClassName(className),
fn
);
}
function hideElement(el) {
el.style.display = 'none';
}
function showElement(el) {
el.style.removeProperty('display');
}
function manipulator(type, fn) {
return () => {
manipulateElements(type, fn);
};
}
function toggler(type) {
return () => {
const isHidden = getElementByClass(elementType[type]).style['display'] == 'none';
manipulateElements(elementType[type], isHidden ? showElement : hideElement);
};
}
function addScroll() {
document.getElementById(':3').classList.add(elementType.scrollBar);
}
function removeScroll() {
document.getElementById(':3').classList.remove(elementType.scrollBar);
}
function clutterise(fn) {
['bottomBar', 'categoryList', 'searchBar'].forEach(className =>
fn(getElementByClass(elementType[className]))
);
getElementsByLiteralClassName(elementType.chromeDivs).forEach(fn);
}
function declutter() {
clutterise(hideElement);
removeScroll();
}
function reclutter() {
clutterise(showElement);
addScroll();
}
function isCluttered() {
return document.getElementById(':3').classList.contains(elementType.scrollBar);
};
function toggleClutter() {
if (isCluttered()) {
declutter();
} else {
reclutter();
}
}
function updateEventListeners(fn) {
const listeners = {
Comma: toggleClutter,
Period: toggler('readEmail'),
Slash: toggler('searchBar')
};
Object.keys(listeners).forEach(
k => fn('keyup', event => {
if (event.ctrlKey && event.code == k) {
listeners[k]();
}
}));
}
function registerEventListeners() {
updateEventListeners(document.addEventListener);
}
function unregisterEventListeners() {
updateEventListeners(document.removeEventListener);
}
// Do the business!
registerEventListeners();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment