[英]Userscript for creating a confirmation popup whenever submitting an issue or posting a comment via pressing Ctrl+Enter(the built-in hotkey) in GitHub
Test URL: https://github.com/darkred/test/issues/new 测试网址: https : //github.com/darkred/test/issues/new
GitHub allows in the issues area for a public repo: GitHub允许在公共仓库的问题区域:
The above happens to me quite a lot because the build-in hotkey for 'submiting an issue or comment' is Ctrl + Enter : I accidentally press that keyboard shortcut before my issue/comment text is ready. 上面发生了很多,因为“提交问题或评论”的内置热键是Ctrl + Enter :在我的问题/评论文本准备就绪之前,我不小心按下了键盘快捷键。
So, I'm trying to make a script (using Greasemonkey) that would show a confirmation popup whenever I try to: 所以,我试图制作一个脚本(使用Greasemonkey),每当我尝试时,它会显示一个确认弹出窗口:
via pressing Ctrl + Enter : 按Ctrl + Enter :
if user presses Ok in the popup, then the script to allow the submit, 如果用户在弹出窗口中按“ 确定 ”,则允许提交的脚本,
but if the user presses Cancel in the popup, then the script to stop the submit. 但如果用户在弹出窗口中按“ 取消 ”,则脚本停止提交。
I've come across these two approaches:
我遇到过这两种方法:
After the helpful comment by Brock Adams I have the following code: 在Brock Adams的有用评论之后,我有以下代码:
var targArea_1 = document.querySelector('#issue_body'); // New issue textarea
var targArea_2 = document.querySelector('#new_comment_field'); // New comment textarea
function manageKeyEvents (zEvent) {
if (zEvent.ctrlKey && zEvent.keyCode == 13) { // If Ctrl+Enter is pressed
if (confirm('Are you sure?') == false) { // If the user presses Cancel in the popup
zEvent.stopPropagation(); // then it stops propagation of the event
zEvent.preventDefault(); // and cancels/stops the submit submit action bound to Ctrl+Enter
}
}
}
if (targArea_1 !== null) {targArea_1.addEventListener('keydown', manageKeyEvents);}
if (targArea_2 !== null) {targArea_2.addEventListener('keydown', manageKeyEvents);}
Now the popup appears ok whenever I press Ctrl + Enter . 现在,只要按Ctrl + Enter ,弹出窗口就会显示。
The problem is that the issue/comment is not submitted when pressing Ok in the popup (even if I haven't pressed Cancel in the popup before, at all). 问题是在弹出窗口中按“ 确定”时没有提交问题/评论 (即使我之前没有在弹出窗口中按“ 取消 ”)。 How to fix this? 如何解决这个问题?
And, how to re-allow the issue/comment submit after I have pressed Cancel in the popup once? 而且,如何重新允许后,我按在弹出一旦取消问题/评论提交?
In other words: how to re-enable default after preventDefault() ? 换句话说:在preventDefault()之后如何重新启用默认值?
Based on the help by user trespassersW here (I thank him a lot) 基于用户trespassersW帮助这里 (我感谢他很多)
ie that my code was missing an else
branch: 即我的代码缺少一个else
分支:
if (confirm('Are you sure?') == false) {
// ...
} else {
var btn = document.querySelector("#partial-new-comment-form-actions button");
if (btn) btn.click();
}
and that's because the confirm
messagebox clears keyboard events queue. 那是因为confirm
消息框清除了键盘事件队列。
(therefore the click 'Ok'
action must be done by the script). (因此, click 'Ok'
操作必须由脚本完成)。
Here is a full working script: 这是一个完整的工作脚本:
// ==UserScript==
// @nameGitHub Confirm Create and Close issues
// @include https://github.com/*
// @grant none
// ==/UserScript==
(function () { // Self-Invoking function
function init() {
// For submitting issues in issue body textarea via Ctrl+Enter
var targArea1 = document.querySelector('#issue_body'); // New issue textarea
function manageKeyEvents1(zEvent) {
if (zEvent.ctrlKey && zEvent.keyCode === 13) {
if (confirm('Are you sure?') === false) {
zEvent.stopPropagation();
zEvent.preventDefault();
} else {
var btn1 = document.querySelector('.btn-primary');
if (btn1) {btn1.click();}
}
}
}
if (targArea1 !== null) { targArea1.addEventListener('keydown', manageKeyEvents1); }
// ------------------------------------------------------------------------------------------------
// For submitting issues in new comment textarea via Ctrl+Enter
var targArea2 = document.querySelector('#new_comment_field'); // New comment textarea
function manageKeyEvents2(zEvent) {
if (zEvent.ctrlKey && zEvent.keyCode === 13) {
if (confirm('Are you sure?') === false) {
zEvent.stopPropagation();
zEvent.preventDefault();
} else {
var btn2 = document.querySelector('#partial-new-comment-form-actions button');
if (btn2) {btn2.click();}
}
}
}
if (targArea2 !== null) { targArea2.addEventListener('keydown', manageKeyEvents2); }
}
// Page load
init();
// On pjax (because GitHub uses the History API)
document.addEventListener('pjax:end', init);
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.