1

テスト URL: https://github.com/darkred/test/issues/new

GitHub では、公開リポジトリの問題領域で次のことが許可されています。

  • タイトルが 1 文字だけで本文がない新しい号を提出する。
  • たった1文字でコメントを投稿する。

「課題またはコメントを送信する」ための組み込みのホットキーがCtrl + Enter: 課題/コメント テキストの準備が整う前に、そのキーボード ショートカットを誤って押してしまったため、上記のようなことがよく起こります。


したがって、次のことを試みるたびに確認ポップアップを表示するスクリプトを (Greasemonkey を使用して) 作成しようとしています。

  • 新しい問題を提出する、または
  • コメントを投稿

押すことによってCtrl + Enter:
ユーザーがOkポップアップを押すと、スクリプトは送信を許可し
ますが、ユーザーがCancelポップアップを押すと、スクリプトは送信を停止します。


私はこれらの2つのアプローチに出くわしまし
た.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);}

これで、 を押すたびにポップアップが正常に表示されますCtrl + Enter
問題は、ポップアップを押したときに問題/コメントが送信されないことOkです(以前にポップアップを押したことがない場合でもCancel)。これを修正する方法は?また、ポップアップを一度
押した後、問題/コメントの送信を再度許可するにはどうすればよいですか? つまり、 preventDefault() の後にデフォルトを再度有効にする方法は?Cancel

4

1 に答える 1

0

ここで ユーザー trespassersW の助けに基づいて(私は彼に感謝します)
つまり、私のコードには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);

})();
于 2016-08-02T13:11:40.137 に答える