mebuki-post-keybind

めぶきちゃんねるの投稿フォームをEnterキーで開き、Escapeキーで閉じます

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         mebuki-post-keybind
// @namespace    https://mebuki.moe/
// @version      0.1.4
// @description  めぶきちゃんねるの投稿フォームをEnterキーで開き、Escapeキーで閉じます
// @author       ame-chan
// @match        https://mebuki.moe/app*
// @license      MIT
// @run-at       document-idle
// @require      https://update.greasyfork.org/scripts/552225/1688013/mebuki-page-state.js
// ==/UserScript==
(async () => {
  'use strict';
  if (typeof window.USER_SCRIPT_MEBUKI_STATE === 'undefined') {
    return;
  }
  const { subscribe } = window.USER_SCRIPT_MEBUKI_STATE;
  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 setKeydownEvent = () => {
    const postFormWindowElm = document.querySelector('.compose-window:has(textarea)');
    if (postFormWindowElm) {
      postFormWindowElm.removeEventListener('keydown', handleEscape, true);
      postFormWindowElm.addEventListener('keydown', handleEscape, true);
    }
  };
  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();
      setTimeout(() => {
        setKeydownEvent();
      }, 300);
    }
  };
  subscribe((state) => {
    if (state.isThreadPage) {
      document.addEventListener('keydown', keydownHandler, true);
    } else {
      document.removeEventListener('keydown', keydownHandler, true);
    }
  });
})();