Skip to content

Instantly share code, notes, and snippets.

@weaming weaming/MyKeyMap.js
Last active Apr 11, 2019

Embed
What would you like to do?
Tampermonkey script to define my personal key mapping using in chrome browser <3
// ==UserScript==
// @name MyKepMap
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author weaming
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
"use strict";
window.print = console.log.bind(console);
let leader = ",";
let maxLen = 3;
let KS = {
empty: () => {
window.keyStack = [];
},
push: v => {
if (window.keyStack === undefined) {
window.keyStack = [];
}
window.keyStack.push(v);
},
len: () => {
if (window.keyStack) {
return window.keyStack.length;
}
return 0;
},
value: () => {
if (window.keyStack) {
return window.keyStack;
}
return [];
}
};
document.addEventListener("keydown", e => {
print(e);
if (e.key === "Escape") {
KS.empty();
return;
}
if (KS.len() >= maxLen) {
KS.empty();
}
if (KS.len() === 0) {
if (e.key === leader) {
KS.empty();
KS.push(e.key);
return;
}
} else {
if (KS.value()[0] === leader && e.key.length === 1) {
KS.push(e.key);
}
}
let ks = KS.value()
.slice(1)
.join("");
if (ks.length > 0) {
print(ks);
e.preventDefault();
}
if (ks === ".") {
KS.empty();
// goto root
location.href = location.origin;
return;
}
if (ks === ",") {
KS.empty();
// goto upper
location.search = "";
let newPath = location.pathname
.split("/")
.filter(x => x.length > 0)
.slice(0, -1)
.join("/");
location.pathname = newPath;
return;
}
if (ks === "a") {
KS.empty();
// goto raw file of github source code
location.href = location.href
.replace("github.com", "raw.githubusercontent.com")
.replace("/blob/", "/");
return;
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.