futaba-post-keybind

ふたばちゃんねるの投稿フォーム周りにキーバインドを仕込みます

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         futaba-post-keybind
// @namespace    http://2chan.net/
// @version      0.2.0
// @description  ふたばちゃんねるの投稿フォーム周りにキーバインドを仕込みます
// @author       ame-chan
// @match        http://*.2chan.net/b/res/*
// @match        https://*.2chan.net/b/res/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=2chan.net
// @license      MIT
// @run-at       document-idle
// @require      https://cdn.jsdelivr.net/npm/[email protected]/src/ua-parser.min.js
// ==/UserScript==
(async () => {
  'use strict';
  const GLOBAL = {
    /** 「Enterキーで入力フォーム表示/非表示制御」が既にセットされているか */ isEnableEnterKeyEvent: false,
    /** 入力エリアにフォーカスがあるか */ isTextareaFocus: false,
  };
  const wait = (timer = 1000) => new Promise((resolve) => setTimeout(() => resolve(true), timer));
  const isMac = () => {
    const parser = typeof window.UAParser === 'function' ? new window.UAParser() : false;
    if (parser) {
      const os = (parser.getOS().name || '').toLowerCase();
      if (os.includes('mac')) {
        return true;
      }
    }
    return false;
  };
  const setEnterKeyEvent = () => {
    const formElm = document.querySelector('#postform') || document.querySelector('#fm');
    const textareaElm = document.querySelector('#ftxa');
    const reloadElm = document.querySelector('#fvw_loading') || document.querySelector('#contres > a:first-of-type');
    const submitElm = document.querySelector('#ftbl input[type="submit"]');
    if (!formElm || !textareaElm || !reloadElm || !submitElm) {
      return;
    }
    const hasMac = isMac();
    const scrollPos = () => {
      const hrPos = document.querySelector('.thre ~ hr')?.offsetTop ?? 0;
      const windowH = window.innerHeight;
      const result = hrPos - windowH;
      return result >= 0 ? result : 0;
    };
    const keydownHandler = async (e) => {
      const writeElm = document.querySelector('#write');
      const formOpacityValue = formElm.style.opacity;
      const isFormHidden = formOpacityValue === '0' || formOpacityValue === '';
      const isFormVisible = formOpacityValue === '1';
      const isCtrlKey = hasMac ? e.metaKey : e.ctrlKey;
      const textAreaValue = [...(textareaElm?.value || '')];
      if (e.key === 'Escape' && isFormVisible && writeElm) {
        writeElm.click();
      } else if (e.key === 'Enter' && textareaElm) {
        if (isFormHidden && writeElm) {
          writeElm.click();
          await wait(300);
          textareaElm.value = '';
        } else if (isCtrlKey && textAreaValue.length && GLOBAL.isTextareaFocus) {
          submitElm.click();
          textareaElm.value = '';
          await wait(1000);
          window.scrollTo({
            top: scrollPos(),
            left: 0,
            behavior: 'smooth',
          });
        }
      }
    };
    document.addEventListener('keydown', keydownHandler);
    textareaElm.addEventListener('focus', () => {
      GLOBAL.isTextareaFocus = true;
    });
    textareaElm.addEventListener('blur', () => {
      GLOBAL.isTextareaFocus = false;
    });
  };
  const changeSubmitText = () => {
    const submitElm = document.querySelector('#ftbl input[type="submit"]');
    if (submitElm) {
      const key = isMac() ? 'Cmd' : 'Ctrl';
      submitElm.value = `返信(${key}+Enter)`;
    }
  };
  const initialize = () => {
    if (GLOBAL.isEnableEnterKeyEvent === false) {
      GLOBAL.isEnableEnterKeyEvent = true;
      setEnterKeyEvent();
      changeSubmitText();
    }
  };
  initialize();
  window.addEventListener('load', () => initialize());
})();