配置宝塔key

Add a button to configure API key for IP:port URLs

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

  1. // ==UserScript==
  2. // @name 配置宝塔key
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Add a button to configure API key for IP:port URLs
  6. // @author You
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  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. const apiUrl = `http://${url.host}/config?action=set_token`;
  31.  
  32. try {
  33. const response = await fetch(apiUrl, {
  34. headers: {
  35. "accept": "*/*",
  36. "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  37. "cache-control": "no-cache",
  38. "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
  39. "pragma": "no-cache",
  40. "proxy-connection": "keep-alive",
  41. "x-http-token": $("#request_token_head").attr('token'),
  42. "x-requested-with": "XMLHttpRequest"
  43. },
  44. referrer: `http://${url.host}/config`,
  45. referrerPolicy: "strict-origin-when-cross-origin",
  46. body: `t_type=3&limit_addr=${url.host}`,
  47. method: "POST",
  48. mode: "cors",
  49. credentials: "include"
  50. });
  51.  
  52. const result = await response.json();
  53. const decodedResult = JSON.parse(JSON.stringify(result).replace(/\\/g, '\\\\').replace(/\u([0-9a-fA-F]{4})/g, '\\u$1'));
  54. alert(`API 响应结果:\n${JSON.stringify(decodedResult, null, 2)}`);
  55. } catch (error) {
  56. alert(`发生错误: ${error}`);
  57. }
  58. });
  59.  
  60. // 将按钮添加到页面
  61. document.body.appendChild(button);
  62. }
  63. })();