Userscript用于通过在GitHub中按Ctrl + Enter(内置热键)提交问题或发表评论时创建确认弹出窗口 [英] Userscript for creating a confirmation popup whenever submitting an issue or posting a comment via pressing Ctrl+Enter(the built-in hotkey) in GitHub
问题描述
测试网址: https://github.com/darkred/test/issues/new
GitHub允许在问题领域进行公开回购: 提交一个只有1个字符的新问题作为标题和非正文,并且
以上这些发生在我身上很多,因为提交问题或评论的内置热键是 Ctrl + Enter :在我的问题出现之前,我不小心按下了该键盘快捷键/注释文本已准备好。
因此,我试图制作一个脚本(使用Greasemonkey),只要我尝试:
$ b
如果用户按下 Ok 中的b $ b
弹出窗口,然后脚本允许提交,
但如果你在弹出窗口中按下 Cancel ,然后脚本停止提交。
我已经遇到这两种方法:
在经过Brock Adams的有用评论后,我有以下代码:
var targArea_1 = document.querySelector('#issue_body'); //新问题textarea
var targArea_2 = document.querySelector('#new_comment_field'); //新评论textarea
函数manageKeyEvents(zEvent){
if(zEvent.ctrlKey&& zEvent.keyCode == 13){//如果Ctrl + Enter被按下
if(confirm('Are you sure?')== false){//如果用户在弹出的
zEvent.stopPropagation()中按下了Cancel, //然后它停止事件的传播
zEvent.preventDefault(); //取消/停止绑定到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()之后重新启用default?
基于用户trespassersW的帮助这里 (我非常感谢他)
即我的代码缺少 else
分支:
if(confirm('Are you sure?')== false){
// ...
} else {
var btn = document.querySelector(#partial-new-comment -form-actions按钮);
if(btn)btn.click();
$ / code>
这是因为确认
messagebox清除键盘事件队列。
(因此点击'Ok'
动作必须由脚本完成)。
这是一个完整的工作脚本:
// == UserScript ==
// @nameGitHub确认创建并关闭问题
// @include https://github.com/*
// @grant none
// == / UserScript ==
(function(){//自调用函数
函数init(){
//用于提交issue issue中的问题textarea通过Ctrl + Enter
var targArea1 = document.querySelector('#issue_body'); //新问题textarea
函数manageKeyEvents1(zEvent){
if(zEvent.ctrlKey&& zEvent.keyCode === 13){
if(confirm('Are you sure?')=== false){
zEvent.stopPropagation();
zEvent.preventDef ault();
} else {
var btn1 = document.querySelector('。btn-primary');
if(btn1){btn1.click();}
}
}
}
if(targArea1!== null){targArea1.addEventListener('keydown ',manageKeyEvents1); }
// --------------------------------------- -------------------------------------------------- -------
//用于在新评论textarea中通过Ctrl + Enter提交问题
var targArea2 = document.querySelector('#new_comment_field'); //新评论textarea
函数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); }
}
//页面加载
init();
//在pjax上(因为GitHub使用History API)
document.addEventListener('pjax:end',init);
})();
Test URL: https://github.com/darkred/test/issues/new
GitHub allows in the issues area for a public repo:
- submitting a new issue with just 1 character as title and no body, and
- posting a comment with just 1 character .
The above happens to me quite a lot because the build-in hotkey for 'submiting an issue or comment' is Ctrl + Enter: I accidentally press that keyboard shortcut before my issue/comment text is ready.
So, I'm trying to make a script (using Greasemonkey) that would show a confirmation popup whenever I try to:
- submit a new issue, or
- post a comment
via pressing Ctrl + Enter:
if user presses Ok in the popup, then the script to allow the submit,
but if the user presses Cancel in the popup, then the script to stop the submit.
I've come across these two approaches:
After the helpful comment by Brock Adams I have the following code:
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);}
Now the popup appears ok whenever I press Ctrl + Enter.
The problem is that the issue/comment is not submitted when pressing Ok in the popup (even if I haven't pressed Cancel in the popup before, at all). How to fix this?
And, how to re-allow the issue/comment submit after I have pressed Cancel in the popup once?
In other words: how to re-enable default after preventDefault() ?
Based on the help by user trespassersW here (I thank him a lot)
i.e. that my code was missing an else
branch:
if (confirm('Are you sure?') == false) {
// ...
} else {
var btn = document.querySelector("#partial-new-comment-form-actions button");
if (btn) btn.click();
}
and that's because the confirm
messagebox clears keyboard events queue.
(therefore the click 'Ok'
action must be done by the script).
Here is a full working script:
// ==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);
})();
这篇关于Userscript用于通过在GitHub中按Ctrl + Enter(内置热键)提交问题或发表评论时创建确认弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!