简体   繁体   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

(In continuance of this answer ) (继续此回答

I've been trying to make a script (using Greasemonkey) that would show a confirmation popup whenever I attempt to: 我一直在尝试制作一个脚本(使用Greasemonkey),该脚本在每次尝试执行以下操作时都会显示确认弹出窗口:

  • submit a new issue, or 提交新期刊,或
  • post a new comment. 发表新评论。

via pressing Ctrl + Enter : 通过按Ctrl + Enter
if user presses Ok in the popup, then the script to allow the submit, 如果用户在弹出窗口中按“ Ok ”,则该脚本允许提交,
but if the user presses Cancel in the popup, then the script to stop the submit. 但是如果用户在弹出窗口中按“ Cancel ”,则脚本将停止提交。

The script in the answer above works ok in these cases. 在这些情况下,以上答案中的脚本可以正常工作。


I've noticed that, there's an additional way to submit an issue: 我注意到,还有一种其他方式可以提交问题:
press Enter or Ctrl+Enter while having focus on the issue title textbox . 重点关注问题标题文本框时,按EnterCtrl + Enter

I'd like to cover this with the script, too. 我也想用脚本进行介绍。

Below is my code. 下面是我的代码。
if I just open the new issue page ( https://github.com/darkred/test/issues/new ) in a new tab _(ie not via single-page application workflow, aka the History API)_), then the script also works when pressing Ctrl+Enter . 如果我只是在新标签_中打开新的问题页面( https://github.com/darkred/test/issues/new )_(即不通过单页应用程序工作流程,也称为History API)_),则按下Ctrl + Enter脚本也可以工作。

The problem that I still have are that if I navigate to the new issue page via following the New issue button (ie via the History API) , 我仍然遇到的问题是,如果我通过点击“ New issue按钮(即通过History API)导航到新问题页面,
and then I either press Ctrl+Enter or just Enter in the title textbox, then the popup appears momentarily but the submit is not blocked . 然后我要么按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: STR:

  • open https://github.com/darkred/test/issues , 打开https://github.com/darkred/test/issues
  • click the New Issue button (you'll get redirected via the History API to https://github.com/darkred/test/issues/new , 点击“ New Issue按钮(您将通过History API重定向到https://github.com/darkred/test/issues/new
  • (You'll notice the focus now is on the issue title textbox) (您会注意到现在的重点是问题标题文本框)
    type 123 as issue title and keep the focus on the issue title textbox (leave the issue body empty) , 键入123作为问题标题,并继续关注问题标题文本框(将问题正文留空),
  • press Ctrl+Enter (or just Enter), Ctrl + Enter (或按Enter),
  • notice now that the confirmation popup will appear momentarily, 现在注意,确认弹出窗口将立即出现,
    but the submit won't be blocked. 但提交不会被阻止。

What's wrong with my script? 我的脚本怎么了?


For reference here is a list of the GitHub's keyboard shortcuts list: screenshot , 作为参考,下面是GitHub的键盘快捷键列表的列表: 屏幕截图
that appears when you press ? 按下时出现? in the new issue page. 在新发行页面中。

I managed to solve this by forcing an unfocus+re-focus on the #issue_title element : 我设法通过对#issue_title元素强制取消关注和重新关注来解决此问题:
when you open the New issue page, the focus is on the issue title textbox. 当您打开“新问题”页面时,焦点将放在“问题标题”文本框中。
The script for some reason wouldn't block the submit. 脚本由于某种原因不会阻止提交。 But, if you force an unfocus and re-focus of that element (using blur() (=unfocus) and focus() ) , then the script blocks the submit. 但是,如果您强制使该元素失去焦点并重新聚焦(使用blur() (= unfocus)和focus() ,则脚本将阻止提交。

Here is the code (always // @run-at document-end ) 这是代码(总是// @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
})();


And here is the complete userscript: 这是完整的用户脚本:
GitHub - Confirmations before submitting issues and comments GitHub-提交问题和评论之前的确认

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM