Update storage with user input and fetch data from API
目前為
// ==UserScript==
// @name alist速率修改小工具
// @namespace http://tampermonkey.net/
// @version 0.7
// @description Update storage with user input and fetch data from API
// @author Your Name
// @match *://*/*
// @grant none
// @license MIT
// ==/UserScript==
(function() {
'use strict';
// Create update data button
const updateButton = document.createElement('button');
updateButton.textContent = '更新数据';
updateButton.style.position = 'fixed';
updateButton.style.top = '10px';
updateButton.style.right = '10px';
updateButton.style.zIndex = 1000;
updateButton.style.padding = '10px 15px';
updateButton.style.backgroundColor = '#007BFF';
updateButton.style.color = '#FFFFFF';
updateButton.style.border = 'none';
updateButton.style.borderRadius = '5px';
updateButton.style.cursor = 'pointer';
document.body.appendChild(updateButton);
// Update data button event handler
updateButton.addEventListener('click', () => {
const url = window.location.origin + "/api/admin/storage/update";
const token = window.localStorage['token'];
const update_data = prompt("请输入更新的数据(JSON 格式):");
if (update_data) {
let parsedData;
try {
parsedData = JSON.parse(update_data);
} catch (e) {
alert("输入的数据不是有效的 JSON 格式。请重新输入。");
return;
}
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"authorization": token
},
body: JSON.stringify(parsedData),
})
.then(response => response.json())
.then(data => {
console.log("更新成功:", data);
alert("数据更新成功!");
})
.catch(error => {
console.error("更新错误:", error);
alert("数据更新失败,请查看控制台以获取详情。");
});
} else {
alert("未输入数据,操作被取消。");
}
});
// Create fetch data button
const fetchButton = document.createElement('button');
fetchButton.textContent = '获取数据';
fetchButton.style.position = 'fixed';
fetchButton.style.top = '50px'; // Adjust position to avoid overlap
fetchButton.style.right = '10px';
fetchButton.style.zIndex = 1000;
fetchButton.style.padding = '10px 15px';
fetchButton.style.backgroundColor = '#28A745';
fetchButton.style.color = '#FFFFFF';
fetchButton.style.border = 'none';
fetchButton.style.borderRadius = '5px';
fetchButton.style.cursor = 'pointer';
document.body.appendChild(fetchButton);
// Fetch data button event handler
fetchButton.addEventListener('click', () => {
const fetchUrl = window.location.origin + "/api/admin/storage/get?id=7";
const token = window.localStorage['token'];
fetch(fetchUrl, {
method: "GET",
headers: {
"authorization": token
}
})
.then(response => response.json())
.then(data => {
console.log("获取成功:", data);
const formattedData = JSON.stringify(data, null, 2);
alert("获取到的数据请从data后面的括号开始复制到记事本进行修改:\n" + formattedData);
// Prompt for copying data to clipboard
if (confirm("是否复制获取到的数据到剪贴板?")) {
navigator.clipboard.writeText(formattedData)
.then(() => {
// Optionally, create a notification message
const messageDiv = document.createElement('div');
messageDiv.textContent = "数据已复制到剪贴板!";
messageDiv.style.position = 'fixed';
messageDiv.style.top = '130px'; // Position the message below the fetch button
messageDiv.style.right = '10px';
messageDiv.style.zIndex = 1000;
messageDiv.style.padding = '10px 15px';
messageDiv.style.backgroundColor = '#dc3545'; // Red background
messageDiv.style.color = '#FFFFFF'; // White text
messageDiv.style.borderRadius = '5px';
messageDiv.style.fontSize = '14px';
document.body.appendChild(messageDiv);
// Remove the message after 3 seconds
setTimeout(() => {
document.body.removeChild(messageDiv);
}, 3000);
})
.catch(err => {
console.error("复制失败:", err);
alert("数据复制失败,请查看控制台以获取详情。");
});
}
})
.catch(error => {
console.error("获取错误:", error);
alert("数据获取失败,请查看控制台以获取详情。");
});
});
})();