めぶきちゃんねるの投稿フォームをEnterキーで開き、Escapeキーで閉じます
当前为
// ==UserScript==
// @name mebuki-post-keybind
// @namespace https://mebuki.moe/
// @version 0.1.0
// @description めぶきちゃんねるの投稿フォームをEnterキーで開き、Escapeキーで閉じます
// @author ame-chan
// @match https://mebuki.moe/app/t/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=mebuki.moe
// @license MIT
// @run-at document-idle
// ==/UserScript==
(async () => {
'use strict';
const handleEscape = (e) => {
if (e.key === 'Escape') {
const postFormWindowElm = document.querySelector('.compose-window:has(textarea)');
const postFormWindowCloseElm = postFormWindowElm?.querySelector('.absolute.top-1\\.5.right-1\\.5 > button');
if (postFormWindowCloseElm instanceof HTMLButtonElement) {
e.preventDefault();
e.stopPropagation();
postFormWindowCloseElm.click();
}
}
};
const keydownHandler = (e) => {
const postFormWindowElm = document.querySelector('.compose-window:has(textarea)');
const postFormOpenButtonElm = document.querySelector('main[data-slot="sidebar-inset"] > div.pb-safe > button');
const isFormVisible = postFormWindowElm instanceof HTMLElement;
if (!(postFormOpenButtonElm instanceof HTMLButtonElement)) return;
if (e.key === 'Enter' && !isFormVisible) {
e.preventDefault();
e.stopPropagation();
postFormOpenButtonElm.click();
}
};
document.addEventListener('keydown', keydownHandler, true);
const observer = new MutationObserver(() => {
const postFormWindowElm = document.querySelector('.compose-window:has(textarea)');
if (postFormWindowElm) {
postFormWindowElm.addEventListener('keydown', handleEscape, true);
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
})();