Greasy Fork

Greasy Fork is available in English.

RPC切换

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

当前为 2025-07-01 提交的版本,查看 最新版本

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

(function() {
  'use strict';
  // 定义多个RPC配置
  const rpcConfigs = [
    {
      address: 'http://127.0.0.1',
      downloadPath: '/Users/Administrator/Downloads',
      token:'123',
      port: 6800
    },
          {
      address: 'https://ade.com',
      downloadPath: '/root/downloads',
      token:'123',
      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);
    unsafeWindow.base.setValue('setting_rpc_token', config.token); // 添加这一行
  } else {
    // 如果config为空,可以选择重置或保持当前配置
    // 这里示例为重置
    unsafeWindow.base.setValue('setting_rpc_domain', '');
    unsafeWindow.base.setValue('setting_rpc_dir', '');
    unsafeWindow.base.setValue('setting_rpc_port', '');
    unsafeWindow.base.setValue('setting_rpc_token', ''); // 添加这一行
  }
};

  // 创建一个下拉选择框来切换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 newParent = document.querySelector('.list-header');

    // 只有当这个容器出现时(即用户选中文件后),才执行插入操作
    if (newParent) {
      // 检查下拉框是否已经存在于此容器,避免重复插入
      if (!newParent.contains(select)) {
          // 将下拉框插入为该容器的第一个子节点
          newParent.insertBefore(select, newParent.firstChild);
      }

      // 获取当前配置并设置下拉框的值
      const currentRpcAddress = unsafeWindow.base.getValue('setting_rpc_domain');
      const currentDownloadPath = unsafeWindow.base.getValue('setting_rpc_dir');
      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 = '';
      }
    } else {
        // 如果容器消失了(例如取消了文件选择),就从DOM中移除下拉框,避免它残留
        if (select.parentNode) {
            select.parentNode.removeChild(select);
        }
    }
  }

  init();

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