-
Notifications
You must be signed in to change notification settings - Fork 1
/
pf_hotkey.user.js
51 lines (43 loc) · 1.54 KB
/
pf_hotkey.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
// ==UserScript==
// @name ProgrammersForumHotkey
// @namespace http://programmersforum.ru/
// @version 0.3
// @description allows to submit posts with Ctrl+Enter
// @author Alex P
// @include *programmersforum.ru/*
// @grant none
// @downloadURL https://github.com/AlexP11223/ProgForumRuUserscripts/raw/master/pf_hotkey.user.js
// ==/UserScript==
(function () {
'use strict';
function ctrlEnterPressed(keyEcent) {
return (keyEcent.keyCode === 10 || keyEcent.keyCode === 13) && keyEcent.ctrlKey;
}
function isQuickEdit(textarea) {
return textarea.attr('id').indexOf('_QE_') > 0;
}
const textAreaKeyHandler = function (e) {
if (ctrlEnterPressed(e)) {
const textarea = $(this);
if (isQuickEdit(textarea)) {
textarea.closest('table').find('input[id$="_save"]').click();
} else {
textarea.closest('form').submit();
}
textarea.blur();
}
};
$('textarea').keydown(textAreaKeyHandler);
if (typeof MutationObserver !== 'undefined') {
const posts = $('#posts');
if (posts.length) {
new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.addedNodes) {
$(mutation.addedNodes).find('textarea').keydown(textAreaKeyHandler);
}
});
}).observe(posts[0], {childList: true, subtree: true});
}
}
})();