// ==UserScript== // @name 创建按钮,发起带请求头和POST数据的请求 // @namespace http://tampermonkey.net/ // @version 0.1 // @description 使用自定义请求头和POST数据发起请求,并携带当前页面的Cookie // @author Jack // @match *://*/* // @grant GM_xmlhttpRequest // @connect example.com // ==/UserScript== (function () { "use strict"; // 创建一个按钮 const executeButton = document.createElement("button"); executeButton.textContent = "执行"; executeButton.style.position = "fixed"; executeButton.style.top = "20px"; executeButton.style.left = "50%"; executeButton.style.padding = "10px 20px"; executeButton.style.backgroundColor = "#007bff"; executeButton.style.color = "#fff"; executeButton.style.border = "none"; executeButton.style.borderRadius = "5px"; executeButton.style.cursor = "pointer"; executeButton.style.zIndex = "10000"; // 添加点击事件 executeButton.addEventListener("click", function () { // 获取当前页面的Cookie const currentCookie = document.cookie; console.log(currentCookie); // 请求的目标URL const targetUrl = "https://example.com/api"; // 替换为目标API地址 // POST请求的数据 const postData = { key1: "value1", key2: "value2", }; // 存储请求结果的数组 const responses = []; for (let i = 0; i < 1; i++) { GM_xmlhttpRequest({ method: "POST", // 请求方法 url: targetUrl, // 请求URL data: JSON.stringify(postData), // POST请求的数据 headers: { "Content-Type": "application/json", // 设置请求头 "User-Agent": "Tampermonkey Script", // 自定义User-Agent Referer: window.location.href, // 当前页面的Referer }, cookie: currentCookie, // 使用当前页面的Cookie onload: function (response) { responses.push(response.responseText); // 将响应内容添加到数组 if (responses.length === 1) { showResultDialog(responses); // 显示结果提示框 } }, onerror: function (error) { console.error("请求失败:", error); responses.push(`请求失败:${error.status}`); // 将错误信息添加到数组 if (responses.length === 1) { showResultDialog(responses); // 显示结果提示框 } }, }); } }); // 显示结果提示框 function showResultDialog(responses) { const dialog = document.createElement("div"); dialog.style.position = "fixed"; dialog.style.top = "50%"; dialog.style.left = "50%"; dialog.style.transform = "translate(-50%, -50%)"; dialog.style.padding = "20px"; dialog.style.backgroundColor = "#f9f9f9"; // 浅灰色背景 dialog.style.border = "1px solid #ddd"; // 边框颜色 dialog.style.borderRadius = "10px"; // 更圆润的边角 dialog.style.boxShadow = "0 4px 10px rgba(0, 0, 0, 0.1)"; // 添加阴影 dialog.style.zIndex = "10001"; dialog.style.width = "800px"; // 调整宽度 dialog.style.textAlign = "center"; // 文字居中 dialog.style.maxHeight = "80vh"; // 最大高度不超过视口的 80% dialog.style.overflowY = "auto"; // 垂直滚动条(如果内容超出高度) // 添加标题 const title = document.createElement("h3"); title.textContent = "操作完成"; title.style.color = "#333"; title.style.marginBottom = "10px"; dialog.appendChild(title); // 添加返回结果 const resultText = document.createElement("pre"); resultText.textContent = responses.join("\n\n"); // 每次请求结果之间增加换行 resultText.style.whiteSpace = "pre-wrap"; // 自动换行 resultText.style.overflowX = "auto"; // 横向滚动条 resultText.style.maxHeight = "300px"; // 最大高度 resultText.style.marginBottom = "15px"; resultText.style.padding = "10px"; resultText.style.border = "1px solid #eee"; resultText.style.borderRadius = "5px"; resultText.style.backgroundColor = "#fff"; resultText.style.width = "100%"; // 宽度占满容器 dialog.appendChild(resultText); // 添加按钮容器 const buttonContainer = document.createElement("div"); buttonContainer.style.display = "flex"; buttonContainer.style.justifyContent = "center"; buttonContainer.style.gap = "10px"; // 按钮间距 buttonContainer.style.marginTop = "15px"; // 与内容的间距 dialog.appendChild(buttonContainer); // 创建刷新按钮 const refreshButton = document.createElement("button"); refreshButton.textContent = "刷新"; refreshButton.style.padding = "8px 16px"; refreshButton.style.backgroundColor = "#007bff"; // 蓝色背景 refreshButton.style.color = "#fff"; refreshButton.style.border = "none"; refreshButton.style.borderRadius = "5px"; refreshButton.style.cursor = "pointer"; refreshButton.addEventListener("click", function () { location.reload(); }); buttonContainer.appendChild(refreshButton); // 创建取消按钮 const cancelButton = document.createElement("button"); cancelButton.textContent = "取消"; cancelButton.style.padding = "8px 16px"; cancelButton.style.backgroundColor = "#ccc"; // 灰色背景 cancelButton.style.color = "#333"; cancelButton.style.border = "none"; cancelButton.style.borderRadius = "5px"; cancelButton.style.cursor = "pointer"; cancelButton.addEventListener("click", function () { dialog.remove(); }); buttonContainer.appendChild(cancelButton); // 将对话框添加到页面 document.body.appendChild(dialog); } // 将按钮添加到页面 document.body.appendChild(executeButton); })();