mebuki-page-state

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/552225/1688437/mebuki-page-state.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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(),
  };
})();