Skip to content

Instantly share code, notes, and snippets.

@ypresto
Last active April 9, 2022 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypresto/60db24e3e367a49c7ab008e7e9981972 to your computer and use it in GitHub Desktop.
Save ypresto/60db24e3e367a49c7ab008e7e9981972 to your computer and use it in GitHub Desktop.
Keyboard shortcut snippet for MoneyForward. Paste .js to your dev console (quite insecure, check the code before do it).
function setup() {
const checkboxSelector = '.collective-operation-checkbox:not(#item_bulk_select)';
const globalCheckboxSelector = `.js-block-journalize-account ${checkboxSelector}`;
delegate(document, 'keydown', 'body', e => {
if (e.target.matches('input:not([type="checkbox"]), textarea')) {
return;
}
if (e.target.matches('.js-block-journalize-account') || findParent(e.target, '.js-block-journalize-account')) {
// keydown handler treats this
return;
}
if (e.key === "j") {
const firstCheckbox = document.querySelector(globalCheckboxSelector);
firstCheckbox === null || firstCheckbox === void 0 ? void 0 : firstCheckbox.focus();
}
if (e.key === "k") {
const lastCheckbox = document.querySelector(`${globalCheckboxSelector}:last-child`);
lastCheckbox === null || lastCheckbox === void 0 ? void 0 : lastCheckbox.focus();
}
});
delegate(document, 'keydown', globalCheckboxSelector, (e, currentEl) => {
var _a, _b, _c, _d, _e, _f, _g;
if (e.target.matches('input:not([type="checkbox"]), textarea')) {
return;
}
if (e.key === "j") {
const row = findParent(currentEl, '.act-list');
if (row && !row.nextElementSibling) {
(_a = document.querySelector('.js-block-journalize-account .js-show-next-act')) === null || _a === void 0 ? void 0 : _a.click();
return;
}
(_c = (_b = row === null || row === void 0 ? void 0 : row.nextElementSibling) === null || _b === void 0 ? void 0 : _b.querySelector(checkboxSelector)) === null || _c === void 0 ? void 0 : _c.focus();
}
if (e.key === "k") {
const row = findParent(currentEl, '.act-list');
(_e = (_d = row === null || row === void 0 ? void 0 : row.previousElementSibling) === null || _d === void 0 ? void 0 : _d.querySelector(checkboxSelector)) === null || _e === void 0 ? void 0 : _e.focus();
}
if (e.key === 'x') {
const checkbox = currentEl;
// MF looks managing state by onclick, not onchange
checkbox.click();
}
if (e.key === 'e') {
const hasSelection = !!document.querySelector(`${globalCheckboxSelector}:checked`);
if (hasSelection) {
(_f = document.querySelector('.js-block-journalize-account .js-btn-bulk-ignore-act')) === null || _f === void 0 ? void 0 : _f.click();
}
else {
const row = findParent(currentEl, '.act-list');
(_g = row === null || row === void 0 ? void 0 : row.querySelector('.js-journal-ignore')) === null || _g === void 0 ? void 0 : _g.click();
}
}
});
}
function delegate(srcElement, type, selector, callback) {
const handler = (e) => {
let currentEl = e.target;
while (currentEl) {
if (currentEl.matches(selector)) {
callback(e, currentEl);
return;
}
currentEl = currentEl.parentElement;
}
};
srcElement.addEventListener(type, handler);
return handler;
}
function findParent(el, selector) {
let currentEl = el.parentElement;
while (currentEl) {
if (currentEl.matches(selector)) {
return currentEl;
}
currentEl = currentEl.parentElement;
}
return null;
}
setup();
function setup() {
const checkboxSelector = '.collective-operation-checkbox:not(#item_bulk_select)'
const globalCheckboxSelector = `.js-block-journalize-account ${checkboxSelector}`
delegate(document, 'keydown', 'body', e => {
if ((e.target as HTMLElement).matches('input:not([type="checkbox"]), textarea')) {
return
}
if ((e.target as HTMLElement).matches('.js-block-journalize-account') || findParent(e.target as HTMLElement, '.js-block-journalize-account')) {
// keydown handler treats this
return
}
if (e.key === "j") {
const firstCheckbox = document.querySelector<HTMLInputElement>(globalCheckboxSelector)
firstCheckbox?.focus()
}
if (e.key === "k") {
const lastCheckbox = document.querySelector<HTMLInputElement>(`${globalCheckboxSelector}:last-child`)
lastCheckbox?.focus()
}
})
delegate(document, 'keydown', globalCheckboxSelector, (e, currentEl) => {
if ((e.target as HTMLElement).matches('input:not([type="checkbox"]), textarea')) {
return
}
if (e.key === "j") {
const row = findParent(currentEl, '.act-list')
if (row && !row.nextElementSibling) {
document.querySelector<HTMLElement>('.js-block-journalize-account .js-show-next-act')?.click()
return
}
row?.nextElementSibling?.querySelector<HTMLInputElement>(checkboxSelector)?.focus()
}
if (e.key === "k") {
const row = findParent(currentEl, '.act-list')
row?.previousElementSibling?.querySelector<HTMLInputElement>(checkboxSelector)?.focus()
}
if (e.key === 'x') {
const checkbox = currentEl as HTMLInputElement
// MF looks managing state by onclick, not onchange
checkbox.click()
}
if (e.key === 'e') {
const hasSelection = !!document.querySelector(`${globalCheckboxSelector}:checked`)
if (hasSelection) {
document.querySelector<HTMLElement>('.js-block-journalize-account .js-btn-bulk-ignore-act')?.click()
} else {
const row = findParent(currentEl, '.act-list')
row?.querySelector<HTMLElement>('.js-journal-ignore')?.click()
}
}
})
}
function delegate<K extends keyof DocumentEventMap>(srcElement: Document, type: K, selector: string, callback: (event: DocumentEventMap[K], target: HTMLElement, ) => void): Function
function delegate<K extends keyof HTMLElementEventMap>(srcElement: HTMLElement, type: K, selector: string, callback: (event: HTMLElementEventMap[K], target: HTMLElement) => void): Function
function delegate(srcElement: Document | HTMLElement, type: string, selector: string, callback: (event: Event, currentTarget: HTMLElement) => void): Function {
const handler = (e: Event) => {
let currentEl = e.target as HTMLElement | null
while (currentEl) {
if (currentEl.matches(selector)) {
callback(e, currentEl)
return
}
currentEl = currentEl.parentElement
}
}
srcElement.addEventListener(type, handler)
return handler
}
function findParent(el: HTMLElement, selector: string): HTMLElement | null {
let currentEl: HTMLElement | null = el.parentElement
while (currentEl) {
if (currentEl.matches(selector)) {
return currentEl
}
currentEl = currentEl.parentElement
}
return null
}
setup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment