Update and Fetch Data Script

Update storage with user input and fetch data from API

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

  1. // ==UserScript==
  2. // @name Update and Fetch Data Script
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.6
  5. // @description Update storage with user input and fetch data from API
  6. // @author Your Name
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11. (function() {
  12. 'use strict';
  13.  
  14. // Create update data button
  15. const updateButton = document.createElement('button');
  16. updateButton.textContent = '更新数据';
  17. updateButton.style.position = 'fixed';
  18. updateButton.style.top = '10px';
  19. updateButton.style.right = '10px';
  20. updateButton.style.zIndex = 1000;
  21. updateButton.style.padding = '10px 15px';
  22. updateButton.style.backgroundColor = '#007BFF';
  23. updateButton.style.color = '#FFFFFF';
  24. updateButton.style.border = 'none';
  25. updateButton.style.borderRadius = '5px';
  26. updateButton.style.cursor = 'pointer';
  27. document.body.appendChild(updateButton);
  28.  
  29. // Update data button event handler
  30. updateButton.addEventListener('click', () => {
  31. const url = window.location.origin + "/api/admin/storage/update";
  32. const token = window.localStorage['token'];
  33. const update_data = prompt("请输入更新的数据(JSON 格式):");
  34. if (update_data) {
  35. let parsedData;
  36. try {
  37. parsedData = JSON.parse(update_data);
  38. } catch (e) {
  39. alert("输入的数据不是有效的 JSON 格式。请重新输入。");
  40. return;
  41. }
  42. fetch(url, {
  43. method: "POST",
  44. headers: {
  45. "Content-Type": "application/json",
  46. "authorization": token
  47. },
  48. body: JSON.stringify(parsedData),
  49. })
  50. .then(response => response.json())
  51. .then(data => {
  52. console.log("更新成功:", data);
  53. alert("数据更新成功!");
  54. })
  55. .catch(error => {
  56. console.error("更新错误:", error);
  57. alert("数据更新失败,请查看控制台以获取详情。");
  58. });
  59. } else {
  60. alert("未输入数据,操作被取消。");
  61. }
  62. });
  63.  
  64. // Create fetch data button
  65. const fetchButton = document.createElement('button');
  66. fetchButton.textContent = '获取数据';
  67. fetchButton.style.position = 'fixed';
  68. fetchButton.style.top = '50px'; // Adjust position to avoid overlap
  69. fetchButton.style.right = '10px';
  70. fetchButton.style.zIndex = 1000;
  71. fetchButton.style.padding = '10px 15px';
  72. fetchButton.style.backgroundColor = '#28A745';
  73. fetchButton.style.color = '#FFFFFF';
  74. fetchButton.style.border = 'none';
  75. fetchButton.style.borderRadius = '5px';
  76. fetchButton.style.cursor = 'pointer';
  77. document.body.appendChild(fetchButton);
  78.  
  79. // Fetch data button event handler
  80. fetchButton.addEventListener('click', () => {
  81. const fetchUrl = window.location.origin + "/api/admin/storage/get?id=7";
  82. const token = window.localStorage['token'];
  83. fetch(fetchUrl, {
  84. method: "GET",
  85. headers: {
  86. "authorization": token
  87. }
  88. })
  89. .then(response => response.json())
  90. .then(data => {
  91. console.log("获取成功:", data);
  92. const formattedData = JSON.stringify(data, null, 2);
  93. alert("获取到的数据请从data后面的括号开始复制到记事本进行修改:\n" + formattedData);
  94.  
  95. // Prompt for copying data to clipboard
  96. if (confirm("是否复制获取到的数据到剪贴板?")) {
  97. navigator.clipboard.writeText(formattedData)
  98. .then(() => {
  99. // Optionally, create a notification message
  100. const messageDiv = document.createElement('div');
  101. messageDiv.textContent = "数据已复制到剪贴板!";
  102. messageDiv.style.position = 'fixed';
  103. messageDiv.style.top = '130px'; // Position the message below the fetch button
  104. messageDiv.style.right = '10px';
  105. messageDiv.style.zIndex = 1000;
  106. messageDiv.style.padding = '10px 15px';
  107. messageDiv.style.backgroundColor = '#dc3545'; // Red background
  108. messageDiv.style.color = '#FFFFFF'; // White text
  109. messageDiv.style.borderRadius = '5px';
  110. messageDiv.style.fontSize = '14px';
  111. document.body.appendChild(messageDiv);
  112.  
  113. // Remove the message after 3 seconds
  114. setTimeout(() => {
  115. document.body.removeChild(messageDiv);
  116. }, 3000);
  117. })
  118. .catch(err => {
  119. console.error("复制失败:", err);
  120. alert("数据复制失败,请查看控制台以获取详情。");
  121. });
  122. }
  123. })
  124. .catch(error => {
  125. console.error("获取错误:", error);
  126. alert("数据获取失败,请查看控制台以获取详情。");
  127. });
  128. });
  129. })();