[英]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),该脚本在每次尝试执行以下操作时都会显示确认弹出窗口:
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 . 重点关注问题标题文本框时,按Enter或Ctrl + 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:
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 , 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.