Add API Key Config Button

配置宝塔key

  1. // ==UserScript==
  2. // @name Add API Key Config Button
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description 配置宝塔key
  6. // @author You
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // 检查当前页面是否为 IP:端口 格式
  15. const url = new URL(window.location.href);
  16. const isIpPort = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$/.test(url.host);
  17.  
  18. if (isIpPort) {
  19. // 创建按钮
  20. const button = document.createElement('button');
  21. button.textContent = '配置 API Key';
  22. button.style.position = 'fixed';
  23. button.style.zIndex = '9999';
  24. button.style.top = '10px';
  25. button.style.right = '10px';
  26.  
  27. // 添加点击事件处理程序
  28. button.addEventListener('click', async () => {
  29. var apiUrl = `http://${url.host}/config?action=set_token`;
  30. var requestBody = `t_type=3&limit_addr=${document.domain}`;
  31.  
  32. var response = await sendRequest(apiUrl, requestBody);
  33. var decodedResult = decodeUnicode(response);
  34. alert(`API 响应结果:\n${JSON.stringify(decodedResult, null, 2)}`);
  35.  
  36. apiUrl = `http://${url.host}/config?action=set_token`;
  37. requestBody = `t_type=2`;
  38.  
  39. response = await sendRequest(apiUrl, requestBody);
  40. decodedResult = decodeUnicode(response);
  41. alert(`API 响应结果:\n${JSON.stringify(decodedResult, null, 2)}`);
  42.  
  43. });
  44.  
  45. // 将按钮添加到页面
  46. document.body.appendChild(button);
  47.  
  48.  
  49. const getKeyButton = document.createElement('button');
  50. getKeyButton.textContent = '获取 API Key';
  51. getKeyButton.style.position = 'fixed';
  52. getKeyButton.style.zIndex = '9999';
  53. getKeyButton.style.top = '50px';
  54. getKeyButton.style.right = '10px';
  55.  
  56. // 添加点击事件处理程序
  57. getKeyButton.addEventListener('click', async () => {
  58. var apiUrl = `http://${url.host}/config?action=get_token`;
  59. var requestBody = `t_type=3&limit_addr=${document.domain}`;
  60.  
  61. var response = await sendRequest(apiUrl, requestBody);
  62. var decodedResult = decodeUnicode(response);
  63. prompt('复制到shell执行可免输key',`echo -e "${decodedResult.token}\nhttp://${url.host}" >> /home/bt_config.txt`);
  64.  
  65. });
  66.  
  67. // 将按钮添加到页面
  68. document.body.appendChild(getKeyButton);
  69. }
  70.  
  71. // 发送 API 请求并返回响应数据
  72. async function sendRequest(url, body) {
  73. var request_token = window.vite_public_request_token;
  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,
  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. })();