用户脚本,用于在提交问题或通过在 GitHub 中按 Ctrl+Enter(内置热键)发布评论时创建确认弹出窗口

Userscript for creating a confirmation popup whenever submitting an issue or posting a comment via pressing Ctrl+Enter(the built-in hotkey) in GitHub

本文关键字:评论 布评论 脚本 创建 窗口 确认 问题 提交 GitHub 内置 用户      更新时间:2023-09-26

Test URL: https://github.com/darkred/test/issues/new

GitHub 允许在公共存储库的问题区域中:

  • 提交仅 1 个字符作为标题且没有正文的新问题,以及
  • 发表仅包含 1 个字符的评论。

以上情况经常发生在我身上,因为用于"提交问题或评论"的内置热键是 Ctrl + Enter:在我的问题/评论文本准备就绪之前,我不小心按下了该键盘快捷键。


因此,我正在尝试制作一个脚本(使用 Greasemonkey),每当我尝试以下操作时都会显示一个确认弹出窗口:

  • 提交新问题,或
  • 发表评论

通过按 Ctrl + 输入
如果用户在弹出窗口中按 Ok,则脚本允许提交,
但是如果用户在弹出窗口中按取消,则脚本将停止提交。


我遇到了这两种方法:
在布洛克·亚当斯的有用评论之后,我有以下代码:

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);}

现在,每当我按 Ctrl + Enter 时,弹出窗口都会正常出现。
问题是在弹出窗口中按确定时未提交问题/评论(即使我之前根本没有在弹出窗口中按取消)。 如何解决这个问题?
而且,如何在弹出窗口中按一次取消后重新允许问题/评论提交?
换句话说:如何在 preventDefault() 之后重新启用默认值?

基于用户入侵者的帮助W在这里(我非常感谢他)
即我的代码缺少一个else分支:

if (confirm('Are you sure?') == false) {
    // ...
} else {
    var btn = document.querySelector("#partial-new-comment-form-actions button");
    if (btn) btn.click();
}

这是因为confirm消息框会清除键盘事件队列。
(因此,click 'Ok'操作必须由脚本完成)。

这是一个完整的工作脚本:

// ==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);
})();