SimpleBalancer

Balances API key and URL pairs usage

이 스크립트는 직접 설치하는 용도가 아닙니다. 다른 스크립트에서 메타 지시문 // @require https://update.greasyfork.org/scripts/528703/1732956/SimpleBalancer.js을(를) 사용하여 포함하는 라이브러리입니다.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         SimpleBalancer
// @namespace    http://tampermonkey.net/
// @version      0.2.0
// @description  Balances API key and URL pairs usage
// @author       RoCry
// @grant        GM_setValue
// @grant        GM_getValue
// @license MIT
// ==/UserScript==

class SimpleBalancer {
  constructor(storageKey = 'simpleBalancerUsage') {
    this.storageKey = storageKey;
    this.storage = this.createStorage();
    this.pairUsage = this.loadUsageData();
  }

  createStorage() {
    if (typeof GM_getValue === 'function' && typeof GM_setValue === 'function') {
      return {
        get: (key, fallback) => GM_getValue(key, fallback),
        set: (key, value) => GM_setValue(key, value)
      };
    }

    if (typeof localStorage !== 'undefined') {
      return {
        get: (key, fallback) => {
          const raw = localStorage.getItem(key);
          if (raw === null) return fallback;
          return JSON.parse(raw);
        },
        set: (key, value) => {
          localStorage.setItem(key, JSON.stringify(value));
        }
      };
    }

    throw new Error('No storage available for SimpleBalancer');
  }

  loadUsageData() {
    const data = this.storage.get(this.storageKey, {});
    if (!data || typeof data !== 'object') {
      throw new Error('Invalid balancer data in storage');
    }
    return data;
  }

  saveUsageData() {
    this.storage.set(this.storageKey, this.pairUsage);
  }

  parseItems(items) {
    if (typeof items !== 'string') {
      throw new Error('Expected comma-separated string');
    }

    const parsed = items.split(',').map(item => item.trim()).filter(Boolean);
    if (parsed.length === 0) {
      throw new Error('Keys and URLs cannot be empty');
    }
    return parsed;
  }

  createPairKey(key, url) {
    return `${key}|||${url}`;
  }

  parsePairKey(pairKey) {
    const [key, url] = pairKey.split('|||');
    return [key, url];
  }

  choosePair(keys, urls) {
    const keyList = this.parseItems(keys);
    const urlList = this.parseItems(urls);

    let pairs = [];

    if (urlList.length === 1) {
      pairs = keyList.map(key => [key, urlList[0]]);
    } else if (keyList.length === 1) {
      pairs = urlList.map(url => [keyList[0], url]);
    } else {
      if (keyList.length !== urlList.length) {
        throw new Error('When using multiple keys and URLs, their counts must match');
      }
      pairs = keyList.map((key, index) => [key, urlList[index]]);
    }

    const pairKeys = pairs.map(([key, url]) => {
      const pairKey = this.createPairKey(key, url);
      if (!(pairKey in this.pairUsage)) {
        this.pairUsage[pairKey] = 0;
      }
      return pairKey;
    });

    const minUsage = Math.min(...pairKeys.map(key => this.pairUsage[key]));
    const leastUsedPairKeys = pairKeys.filter(key => this.pairUsage[key] === minUsage);
    const randomIndex = Math.floor(Math.random() * leastUsedPairKeys.length);
    const chosenPairKey = leastUsedPairKeys[randomIndex];

    this.pairUsage[chosenPairKey] += 1;
    this.saveUsageData();

    return this.parsePairKey(chosenPairKey);
  }

  resetUsage() {
    this.pairUsage = {};
    this.saveUsageData();
  }

  getUsageStats() {
    const stats = {};
    for (const pairKey in this.pairUsage) {
      const [key, url] = this.parsePairKey(pairKey);
      stats[`${key}, ${url}`] = this.pairUsage[pairKey];
    }
    return stats;
  }
}

if (typeof module !== 'undefined') {
  module.exports = SimpleBalancer;
} else {
  window.SimpleBalancer = SimpleBalancer;
}