Last active
April 23, 2017 09:16
-
-
Save Aracturat/75ddf5b2c0a01296b882b70eec241db4 to your computer and use it in GitHub Desktop.
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 Deutsche letters | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Change letters to umlaut letters | |
// @author Nikolay Dozmorov | |
// @match http://*/* | |
// @match https://*/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var mapping = { | |
"a": "ä", | |
"o": "ö", | |
"u": "ü", | |
"s": "ß", | |
"A": "Ä", | |
"O": "Ö", | |
"U": "Ü" | |
}; | |
document.body.addEventListener('keyup', function(e) { | |
if ((e.target.tagName == "INPUT" || e.target.tagName == "DIV") && e.altKey) { | |
var newKey = mapping[e.key]; | |
if (newKey) { | |
var off = 0; | |
if (e.target.tagName == "INPUT") { | |
off = e.target.selectionStart; | |
e.target.value = e.target.value.slice(0, off) + newKey + e.target.value.slice(off); | |
e.target.setSelectionRange(off + 1, off + 1); | |
} | |
if (e.target.tagName == "DIV") { | |
var sel = window.getSelection(); | |
var range = sel.getRangeAt(0); | |
off = range.startOffset; | |
e.target.innerHTML = e.target.innerHTML.slice(0, off) + newKey + e.target.innerHTML.slice(off); | |
range.setStart(e.target.childNodes[0], off + 1); | |
range.collapse(true); | |
sel.removeAllRanges(); | |
sel.addRange(range); | |
} | |
e.preventDefault(); | |
} | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment