diff options
| author | Teddy Wing | 2019-05-01 17:32:03 +0200 |
|---|---|---|
| committer | Teddy Wing | 2019-05-01 17:35:32 +0200 |
| commit | fc04446765471311ac8818fa8bc411b5ed384791 (patch) | |
| tree | 16a4e8e7a3ac07bcd28055be27dc4563f7cfb96c | |
| parent | bd0829555bcaee8f0e32d54c5785651965a5b1e4 (diff) | |
| download | muttagen-fc04446765471311ac8818fa8bc411b5ed384791.tar.bz2 | |
Add shortcut to toggle the left sidebar
Map sidebar toggle to `\m`.
Initialise the code after a timeout to account for Gmail's initial
loading screen.
Needed to use an `as HTMLElement` cast in order to get the code to
compile:
https://github.com/Microsoft/TypeScript/issues/16920
src/sidebar.ts:10:5 - error TS2740: Type 'Element' is missing the
following properties from type 'HTMLElement': accessKey,
accessKeyLabel, autocapitalize, dir, and 110 more.
| -rw-r--r-- | src/index.ts | 6 | ||||
| -rw-r--r-- | src/sidebar.ts | 35 |
2 files changed, 40 insertions, 1 deletions
diff --git a/src/index.ts b/src/index.ts index 538d0c4..8ab0071 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,4 +6,8 @@ // ==/UserScript== import './style'; -import './sidebar'; +import sidebar from './sidebar'; + +window.setTimeout(function() { + sidebar(); +}, 3000); diff --git a/src/sidebar.ts b/src/sidebar.ts index 4e106e1..05e5d22 100644 --- a/src/sidebar.ts +++ b/src/sidebar.ts @@ -1 +1,36 @@ import { SIDEBAR } from './gmail_css_class'; + +export default function() { + var key_codes: { [index: string]: number } = { + SLASH: 220, + M: 77 + } + + var key_buffer: number[] = []; + + var sidebar: HTMLElement = document.getElementsByClassName(SIDEBAR)[0] as HTMLElement; + + document.addEventListener('keydown', function(e) { + if (e.keyCode === key_codes.SLASH) { + key_buffer.push(e.keyCode); + } + if (e.keyCode === key_codes.M) { + key_buffer.push(e.keyCode); + } + }); + + document.addEventListener('keyup', function(e) { + if (key_buffer.length >= 2) { + if (key_buffer[0] === key_codes.SLASH + && key_buffer[1] === key_codes.M) { + if (sidebar.offsetParent === null) { + sidebar.style.display = 'block'; + } + else { + sidebar.style.display = 'none'; + } + } + key_buffer = []; + } + }); +}) |
