Add API Key Config Button

配置宝塔key

当前为 2024-03-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Add API Key Config Button
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description 配置宝塔key
  6. // @author You
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10. // @license MIT
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 检查当前页面是否为 IP:端口 格式
  16. const url = new URL(window.location.href);
  17. const isIpPort = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$/.test(url.host);
  18.  
  19. if (isIpPort) {
  20. // 创建按钮
  21. const button = document.createElement('button');
  22. button.textContent = '配置 API Key';
  23. button.style.position = 'fixed';
  24. button.style.zIndex = '9999';
  25. button.style.top = '10px';
  26. button.style.right = '10px';
  27.  
  28. // 添加点击事件处理程序
  29. button.addEventListener('click', async () => {
  30. var apiUrl = `http://${url.host}/config?action=set_token`;
  31. var requestBody = `t_type=3&limit_addr=${document.domain}`;
  32.  
  33. var response = await sendRequest(apiUrl, requestBody);
  34. var decodedResult = decodeUnicode(response);
  35. alert(`API 响应结果:\n${JSON.stringify(decodedResult, null, 2)}`);
  36.  
  37. apiUrl = `http://${url.host}/config?action=set_token`;
  38. requestBody = `t_type=2`;
  39.  
  40. response = await sendRequest(apiUrl, requestBody);
  41. decodedResult = decodeUnicode(response);
  42. alert(`API 响应结果:\n${JSON.stringify(decodedResult, null, 2)}`);
  43.  
  44. });
  45.  
  46. // 将按钮添加到页面
  47. document.body.appendChild(button);
  48.  
  49.  
  50. const getKeyButton = document.createElement('button');
  51. getKeyButton.textContent = '获取 API Key';
  52. getKeyButton.style.position = 'fixed';
  53. getKeyButton.style.zIndex = '9999';
  54. getKeyButton.style.top = '50px';
  55. getKeyButton.style.right = '10px';
  56.  
  57. // 添加点击事件处理程序
  58. getKeyButton.addEventListener('click', async () => {
  59. var apiUrl = `http://${url.host}/config?action=get_token`;
  60. var requestBody = `t_type=3&limit_addr=${document.domain}`;
  61.  
  62. var response = await sendRequest(apiUrl, requestBody);
  63. var decodedResult = decodeUnicode(response);
  64. prompt('复制到shell执行可免输key',`echo -e "${decodedResult.token}\nhttp://${url.host}" >> /home/bt_config.txt`);
  65.  
  66. });
  67.  
  68. // 将按钮添加到页面
  69. document.body.appendChild(getKeyButton);
  70. }
  71.  
  72. // 发送 API 请求并返回响应数据
  73. async function sendRequest(url, body) {
  74. const response = await fetch(url, {
  75. headers: {
  76. "accept": "*/*",
  77. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  78. "cache-control": "no-cache",
  79. "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
  80. "pragma": "no-cache",
  81. "proxy-connection": "keep-alive",
  82. "x-http-token": $("#request_token_head").attr('token'),
  83. "x-requested-with": "XMLHttpRequest"
  84. },
  85. referrer: `http://${url.host}/config`,
  86. referrerPolicy: "strict-origin-when-cross-origin",
  87. body,
  88. method: "POST",
  89. mode: "cors",
  90. credentials: "include"
  91. });
  92. return await response.json();
  93. }
  94.  
  95. // 解码 Unicode 编码的中文字符
  96. function decodeUnicode(data) {
  97. return JSON.parse(JSON.stringify(data).replace(/\\/g, '\\\\').replace(/\u([0-9a-fA-F]{4})/g, '\\u$1'));
  98. }
  99. })();