-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-caps.user.js
76 lines (69 loc) · 2.31 KB
/
auto-caps.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// ==UserScript==
// @name auto-caps
// @namespace https://asottile.dev
// @version 0.1
// @author asottile
// @match https://github.com/*
// @grant none
// ==/UserScript==
// TODO:
// - if we backspace immediately after sending a capital, un-capital it
// - (MAYBE) make sure the ^Z works to do the same for uncaps
(function() {
function keydown(e) {
console.log('keydown');
if (
e.target.tagName !== 'TEXTAREA' &&
(e.target.tagName !== 'INPUT' || e.target.type !== 'text')
) {
console.log('skipping: not TEXTAREA/INPUT[type=text]');
return;
} else if (e.ctrlKey || e.altKey || e.shiftKey) {
console.log('skipping: ctrl / alt / shift');
return;
}
let pos = e.target.selectionStart;
let end = e.target.selectionEnd;
if (
e.key.match(/^[a-z]$/) && (
pos === 0 || (
pos >= 1 &&
e.target.value[pos - 1] === '\n'
) || (
pos >= 2 &&
e.target.value[pos - 1] === ' ' &&
e.target.value[pos - 2].match(/[.?!\-]/)
) || (
pos >= 3 &&
e.target.value[pos - 1] === ' ' &&
e.target.value[pos - 2] === ' ' &&
e.target.value[pos - 3].match(/[.?!]/)
)
)
) {
e.target.value = (
e.target.value.slice(0, pos) +
e.key.toUpperCase() +
e.target.value.slice(end)
);
e.target.selectionStart = e.target.selectionEnd = pos + 1;
e.preventDefault();
}
if (
e.target.selectionStart == e.target.selectionEnd &&
e.key === ' ' &&
pos >= 2 &&
e.target.value[pos - 1] === ' ' &&
!e.target.value[pos - 2].match(/[.?! ]/)
) {
e.target.value = (
e.target.value.slice(0, pos - 1) +
'. ' +
e.target.value.slice(pos)
);
e.target.selectionStart = e.target.selectionEnd = pos + 1;
e.preventDefault();
}
}
document.body.addEventListener('keydown', keydown);
})();