簡體   English   中英

用於在GitHub中新發行頁面的標題文本框中按Ctrl + Enter或Enter時創建確認彈出窗口的用戶腳本

[英]Userscript for creating a confirmation popup whenever pressing Ctrl+Enter or Enter in the title textbox of a new issue page in GitHub

(繼續此回答

我一直在嘗試制作一個腳本(使用Greasemonkey),該腳本在每次嘗試執行以下操作時都會顯示確認彈出窗口:

  • 提交新期刊,或
  • 發表新評論。

通過按Ctrl + Enter
如果用戶在彈出窗口中按“ Ok ”,則該腳本允許提交,
但是如果用戶在彈出窗口中按“ Cancel ”,則腳本將停止提交。

在這些情況下,以上答案中的腳本可以正常工作。


我注意到,還有一種其他方式可以提交問題:
重點關注問題標題文本框時,按EnterCtrl + Enter

我也想用腳本進行介紹。

下面是我的代碼。
如果我只是在新標簽_中打開新的問題頁面( https://github.com/darkred/test/issues/new )_(即不通過單頁應用程序工作流程,也稱為History API)_),則按下Ctrl + Enter腳本也可以工作。

我仍然遇到的問題是,如果我通過點擊“ New issue按鈕(即通過History API)導航到新問題頁面,
然后我要么按Ctrl + Enter或只是在標題文本框中輸入 ,然后在彈出的瞬間出現,但提交不會被阻斷

(function () {
    function init() {
        var targArea = document.querySelector('#issue_title'); // New issue title
        function manageKeyEvents(zEvent) {
            if (zEvent.ctrlKey && zEvent.keyCode === 13) {      // and the focused element is the issue title textbox
                if (confirm('Are you sure?') === false) {
                    zEvent.stopPropagation();
                    zEvent.preventDefault();
                // } else {
                    // var btn = document.querySelector('.btn-primary');                        // 'Submit new issue' button                
                    // btn.click();
                }
            }
        }
        if (targArea !== null) {targArea.addEventListener('keydown', manageKeyEvents);}
    }
    init();
    document.addEventListener('pjax:end', init); // for the History API
})();

STR:

  • 打開https://github.com/darkred/test/issues
  • 點擊“ New Issue按鈕(您將通過History API重定向到https://github.com/darkred/test/issues/new
  • (您會注意到現在的重點是問題標題文本框)
    鍵入123作為問題標題,並繼續關注問題標題文本框(將問題正文留空),
  • Ctrl + Enter (或按Enter),
  • 現在注意,確認彈出窗口將立即出現,
    但提交不會被阻止。

我的腳本怎么了?


作為參考,下面是GitHub的鍵盤快捷鍵列表的列表: 屏幕截圖
按下時出現? 在新發行頁面中。

我設法通過對#issue_title元素強制取消關注和重新關注來解決此問題:
當您打開“新問題”頁面時,焦點將放在“問題標題”文本框中。
腳本由於某種原因不會阻止提交。 但是,如果您強制使該元素失去焦點並重新聚焦(使用blur() (= unfocus)和focus() ,則腳本將阻止提交。

這是代碼(總是// @run-at document-end

(function () {
    function init() {
        var targArea = document.querySelector('#issue_title'); // New issue title
        function manageKeyEvents(zEvent) {
            targArea.blur();
            targArea.focus();
            if ((zEvent.ctrlKey && zEvent.keyCode === 13) || zEvent.keyCode === 13) {
                if (confirm('Are you sure?') === false) {
                    zEvent.stopPropagation();
                    zEvent.preventDefault();
                } else {
                    var btn = document.querySelector('.btn-primary');  
                    btn.click();
                }
            }
        }
        if (targArea !== null) {targArea.addEventListener('keydown', manageKeyEvents);}
    }
    init();
    document.addEventListener('pjax:end', init); // for the History API
})();


這是完整的用戶腳本:
GitHub-提交問題和評論之前的確認

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM