NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name HabitRPG Shortcut for New Todo
// @namespace http://ghoulmind.com/
// @version 0.2
// @description Press N to open the Mew Todo Modal.
// @author greatghoul
// @match https://habitica.com/*
// @grant none
// @license MIT
// ==/UserScript==
(function () {
'use strict';
var timer = null;
function noModalOpened () {
return document.querySelectorAll('.modal.show').length === 0;
}
function noInputFocused () {
var activeElement = document.activeElement;
return !(activeElement && /(INPUT|TEXTAREA)/.test(activeElement.tagName));
}
function focusTodoTitle () {
timer = null;
var title = document.querySelector('#task-modal .title-input');
title.focus();
}
function triggerClick(selector) {
var elem = document.querySelector(selector);
var clickEvent = new MouseEvent('click', { 'view': window, 'bubbles': true, 'cancelable': true });
elem.dispatchEvent(clickEvent);
}
function keyOperator (event) {
if (event.code === 'KeyN' && noModalOpened() && noInputFocused()) {
triggerClick('#create-task-btn');
setTimeout(function() {
triggerClick('.create-task-btn .icon-todo');
setTimeout(focusTodoTitle, 300);
}, 300);
}
}
window.addEventListener('keydown', keyOperator, false);
})();