mebuki-page-state

めぶきちゃんねるでカタログ、またはスレッドにいる時にstateをアップデートしsubscriberに送信します

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/552225/1688437/mebuki-page-state.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         mebuki-page-state
// @namespace    https://mebuki.moe/
// @version      0.1.6
// @description  めぶきちゃんねるでカタログ、またはスレッドにいる時にstateをアップデートしsubscriberに送信します
// @author       ame-chan
// @match        https://mebuki.moe/app*
// @license      MIT
// @run-at       document-idle
// ==/UserScript==
(function () {
  'use strict';
  class StateManager {
    listeners;
    state;
    /**
     * 初期状態を指定してインスタンスを作成する
     * @param {T} initialState 管理する状態の初期値
     */ constructor(initialState) {
      this.listeners = [];
      this.state = {
        ...initialState,
      };
    }
    /**
     * 現在の状態を取得する (Readonly)
     * @returns {Readonly<T>} 現在の状態オブジェクト
     */ getState() {
      return {
        ...this.state,
      };
    }
    /**
     * 状態を部分的に更新し、購読者に通知する
     * @param {Partial<T>} newState 更新したい状態のキーと値を持つオブジェクト
     * @returns {void}
     */ updateState(newState) {
      const oldState = this.state;
      this.state = {
        ...this.state,
        ...newState,
      };
      if (this.state !== oldState) {
        this.notifyListeners();
      }
    }
    /**
     * 状態の変更を購読する
     * @param {function(state): void} listener 状態変更時に呼び出すコールバック
     * @returns {function(): void} 購読解除用の関数
     */ subscribe(listener) {
      this.listeners.push(listener);
      return () => {
        this.listeners = this.listeners.filter((l) => l !== listener);
      };
    }
    /**
     * 購読者に現在の状態を通知する
     * @private
     * @returns {void}
     */ notifyListeners() {
      const currentState = this.getState();
      this.listeners.forEach((listener) => listener(currentState));
    }
  }
  const initialState = {
    isCatalogPage: false,
    isThreadPage: false,
    threadMessageCount: 0,
  };
  const stateManager = new StateManager(initialState);
  const updateState = () => {
    const threadMessageElms = document.querySelectorAll('.thread-messages > [id^="message-"]');
    const threadMessageCount = threadMessageElms.length;
    const isCatalogPage = /^\/app\/?$/.test(location.pathname);
    const currentState = stateManager.getState();
    if (threadMessageCount > 0) {
      if (
        !currentState.isThreadPage ||
        currentState.isCatalogPage ||
        currentState.threadMessageCount !== threadMessageCount
      ) {
        stateManager.updateState({
          isThreadPage: true,
          isCatalogPage: false,
          threadMessageCount,
        });
      }
    } else if (isCatalogPage) {
      if (currentState.isThreadPage || !currentState.isCatalogPage) {
        stateManager.updateState({
          isThreadPage: false,
          isCatalogPage: true,
          threadMessageCount: 0,
        });
      }
    } else {
      if (currentState.isThreadPage || currentState.isCatalogPage) {
        stateManager.updateState({
          isThreadPage: false,
          isCatalogPage: false,
          threadMessageCount: 0,
        });
      }
    }
  };
  const observer = new MutationObserver(updateState);
  updateState();
  observer.observe(document.body, {
    childList: true,
    subtree: true,
  });
  window.USER_SCRIPT_MEBUKI_STATE = {
    stateManager,
    subscribe: (listener) => stateManager.subscribe(listener),
    getState: () => stateManager.getState(),
  };
})();