![](/img/trans.png)
[英]Userscript for creating a confirmation popup whenever submitting an issue or posting a comment via pressing Ctrl+Enter(the built-in hotkey) in GitHub
[英]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
”,则脚本将停止提交。
在这些情况下,以上答案中的脚本可以正常工作。
我注意到,还有一种其他方式可以提交问题:
重点关注问题标题文本框时,按Enter或Ctrl + 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:
New Issue
按钮(您将通过History API重定向到https://github.com/darkred/test/issues/new , 我的脚本怎么了?
作为参考,下面是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.