Last active
April 11, 2019 15:44
-
-
Save weaming/14b89cce51bea4899106eec1036608da to your computer and use it in GitHub Desktop.
Tampermonkey script to define my personal key mapping using in chrome browser <3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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