[英]Userscript for creating a confirmation popup whenever pressing Ctrl+Enter or Enter in the title textbox of a new issue page in GitHub
[英]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允許在公共倉庫的問題區域:
上面發生了很多,因為“提交問題或評論”的內置熱鍵是Ctrl + Enter :在我的問題/評論文本准備就緒之前,我不小心按下了鍵盤快捷鍵。
所以,我試圖制作一個腳本(使用Greasemonkey),每當我嘗試時,它會顯示一個確認彈出窗口:
按Ctrl + Enter :
如果用戶在彈出窗口中按“ 確定 ”,則允許提交的腳本,
但如果用戶在彈出窗口中按“ 取消 ”,則腳本停止提交。
我遇到過這兩種方法:
在Brock Adams的有用評論之后,我有以下代碼:
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);}
現在,只要按Ctrl + Enter ,彈出窗口就會顯示。
問題是在彈出窗口中按“ 確定”時沒有提交問題/評論 (即使我之前沒有在彈出窗口中按“ 取消 ”)。 如何解決這個問題?
而且,如何重新允許后,我按在彈出一旦取消問題/評論提交?
換句話說:在preventDefault()之后如何重新啟用默認值?
基於用戶trespassersW幫助這里 (我感謝他很多)
即我的代碼缺少一個else
分支:
if (confirm('Are you sure?') == false) {
// ...
} else {
var btn = document.querySelector("#partial-new-comment-form-actions button");
if (btn) btn.click();
}
那是因為confirm
消息框清除了鍵盤事件隊列。
(因此, click 'Ok'
操作必須由腳本完成)。
這是一個完整的工作腳本:
// ==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);
})();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.