RPC切换

方便地切换RPC主机地址及其他设置

// ==UserScript==
// @name         RPC切换
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  方便地切换RPC主机地址及其他设置
// @author       jiemo
// @match        *://pan.quark.cn/*
// @grant        unsafeWindow
// @license MIT
// ==/UserScript==

(function() {
  'use strict';
  // 定义多个RPC配置
  const rpcConfigs = [
    {
      address: 'https://1.de',
      downloadPath: '/downloads',
      port: 443
    },
    {
      address: 'https://2.de',
      downloadPath: '/root/downloads',
      port: 443
    },
    {
      address: 'https://3.de',
      downloadPath: '/downloads',
      port: 443
    }
  ];

  // 切换RPC配置的函数
  const rpcSwitcher = function(config) {
    if (config) {
      unsafeWindow.base.setValue('setting_rpc_domain', config.address);
      unsafeWindow.base.setValue('setting_rpc_dir', config.downloadPath);
      unsafeWindow.base.setValue('setting_rpc_port', config.port);
    } else {
      // 如果config为空,可以选择重置或保持当前配置
      // 这里示例为重置
      unsafeWindow.base.setValue('setting_rpc_domain', '');
      unsafeWindow.base.setValue('setting_rpc_dir', '');
      unsafeWindow.base.setValue('setting_rpc_port', '');
    }
  };

  // 创建一个下拉选择框来切换RPC配置
  const select = document.createElement('select');
  select.className = 'ant-select-selection ant-select-selection--single';
  select.style.width = '250px';
  select.style.fontSize = '15px';

  // 添加空选项
  const emptyOption = document.createElement('option');
  emptyOption.value = '';
  emptyOption.text = '';
  select.appendChild(emptyOption);

  rpcConfigs.forEach((config) => {
    const option = document.createElement('option');
    option.value = JSON.stringify(config);
    option.text = config.address;
    select.appendChild(option);
  });

  select.onchange = function() {
    if (this.value) {
      const selectedConfig = JSON.parse(this.value);
      rpcSwitcher(selectedConfig);
    } else {
      rpcSwitcher(null); // 处理空选项
    }
  };

  // 将下拉选择框放置在指定的按钮右侧
  function init() {
    const targetButton = document.querySelector('.ant-btn.btn-file.btn-create-folder');
    if (targetButton) {
      if (select.parentNode) {
        select.parentNode.removeChild(select);
      }
      targetButton.parentNode.insertBefore(select, targetButton.nextSibling);

      // 获取当前配置并设置下拉框的值
      const currentRpcAddress = unsafeWindow.base.getValue('setting_rpc_domain');
      const currentDownloadPath = unsafeWindow.base.getValue('setting_download_path');
      const currentRpcPort = unsafeWindow.base.getValue('setting_rpc_port');

      const currentConfig = rpcConfigs.find(config =>
        config.address === currentRpcAddress &&
        config.downloadPath === currentDownloadPath &&
        config.port === currentRpcPort
      );

      if (currentConfig) {
        select.value = JSON.stringify(currentConfig);
      } else {
        // 默认选中空选项
        select.value = '';
      }
    }
  }

  init();

  // 监听网址变化
  window.addEventListener('hashchange', init);
  window.addEventListener('popstate', init);
})();