createButton.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // ==UserScript==
  2. // @name 创建按钮,发起带请求头和POST数据的请求
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 使用自定义请求头和POST数据发起请求,并携带当前页面的Cookie
  6. // @author Jack
  7. // @match *://*/*
  8. // @grant GM_xmlhttpRequest
  9. // @connect example.com
  10. // ==/UserScript==
  11. (function () {
  12. "use strict";
  13. // 创建一个按钮
  14. const executeButton = document.createElement("button");
  15. executeButton.textContent = "执行";
  16. executeButton.style.position = "fixed";
  17. executeButton.style.top = "20px";
  18. executeButton.style.left = "50%";
  19. executeButton.style.padding = "10px 20px";
  20. executeButton.style.backgroundColor = "#007bff";
  21. executeButton.style.color = "#fff";
  22. executeButton.style.border = "none";
  23. executeButton.style.borderRadius = "5px";
  24. executeButton.style.cursor = "pointer";
  25. executeButton.style.zIndex = "10000";
  26. // 添加点击事件
  27. executeButton.addEventListener("click", function () {
  28. // 获取当前页面的Cookie
  29. const currentCookie = document.cookie;
  30. console.log(currentCookie);
  31. // 请求的目标URL
  32. const targetUrl = "https://example.com/api"; // 替换为目标API地址
  33. // POST请求的数据
  34. const postData = {
  35. key1: "value1",
  36. key2: "value2",
  37. };
  38. // 存储请求结果的数组
  39. const responses = [];
  40. for (let i = 0; i < 1; i++) {
  41. GM_xmlhttpRequest({
  42. method: "POST", // 请求方法
  43. url: targetUrl, // 请求URL
  44. data: JSON.stringify(postData), // POST请求的数据
  45. headers: {
  46. "Content-Type": "application/json", // 设置请求头
  47. "User-Agent": "Tampermonkey Script", // 自定义User-Agent
  48. Referer: window.location.href, // 当前页面的Referer
  49. },
  50. cookie: currentCookie, // 使用当前页面的Cookie
  51. onload: function (response) {
  52. responses.push(response.responseText); // 将响应内容添加到数组
  53. if (responses.length === 1) {
  54. showResultDialog(responses); // 显示结果提示框
  55. }
  56. },
  57. onerror: function (error) {
  58. console.error("请求失败:", error);
  59. responses.push(`请求失败:${error.status}`); // 将错误信息添加到数组
  60. if (responses.length === 1) {
  61. showResultDialog(responses); // 显示结果提示框
  62. }
  63. },
  64. });
  65. }
  66. });
  67. // 显示结果提示框
  68. function showResultDialog(responses) {
  69. const dialog = document.createElement("div");
  70. dialog.style.position = "fixed";
  71. dialog.style.top = "50%";
  72. dialog.style.left = "50%";
  73. dialog.style.transform = "translate(-50%, -50%)";
  74. dialog.style.padding = "20px";
  75. dialog.style.backgroundColor = "#f9f9f9"; // 浅灰色背景
  76. dialog.style.border = "1px solid #ddd"; // 边框颜色
  77. dialog.style.borderRadius = "10px"; // 更圆润的边角
  78. dialog.style.boxShadow = "0 4px 10px rgba(0, 0, 0, 0.1)"; // 添加阴影
  79. dialog.style.zIndex = "10001";
  80. dialog.style.width = "800px"; // 调整宽度
  81. dialog.style.textAlign = "center"; // 文字居中
  82. dialog.style.maxHeight = "80vh"; // 最大高度不超过视口的 80%
  83. dialog.style.overflowY = "auto"; // 垂直滚动条(如果内容超出高度)
  84. // 添加标题
  85. const title = document.createElement("h3");
  86. title.textContent = "操作完成";
  87. title.style.color = "#333";
  88. title.style.marginBottom = "10px";
  89. dialog.appendChild(title);
  90. // 添加返回结果
  91. const resultText = document.createElement("pre");
  92. resultText.textContent = responses.join("\n\n"); // 每次请求结果之间增加换行
  93. resultText.style.whiteSpace = "pre-wrap"; // 自动换行
  94. resultText.style.overflowX = "auto"; // 横向滚动条
  95. resultText.style.maxHeight = "300px"; // 最大高度
  96. resultText.style.marginBottom = "15px";
  97. resultText.style.padding = "10px";
  98. resultText.style.border = "1px solid #eee";
  99. resultText.style.borderRadius = "5px";
  100. resultText.style.backgroundColor = "#fff";
  101. resultText.style.width = "100%"; // 宽度占满容器
  102. dialog.appendChild(resultText);
  103. // 添加按钮容器
  104. const buttonContainer = document.createElement("div");
  105. buttonContainer.style.display = "flex";
  106. buttonContainer.style.justifyContent = "center";
  107. buttonContainer.style.gap = "10px"; // 按钮间距
  108. buttonContainer.style.marginTop = "15px"; // 与内容的间距
  109. dialog.appendChild(buttonContainer);
  110. // 创建刷新按钮
  111. const refreshButton = document.createElement("button");
  112. refreshButton.textContent = "刷新";
  113. refreshButton.style.padding = "8px 16px";
  114. refreshButton.style.backgroundColor = "#007bff"; // 蓝色背景
  115. refreshButton.style.color = "#fff";
  116. refreshButton.style.border = "none";
  117. refreshButton.style.borderRadius = "5px";
  118. refreshButton.style.cursor = "pointer";
  119. refreshButton.addEventListener("click", function () {
  120. location.reload();
  121. });
  122. buttonContainer.appendChild(refreshButton);
  123. // 创建取消按钮
  124. const cancelButton = document.createElement("button");
  125. cancelButton.textContent = "取消";
  126. cancelButton.style.padding = "8px 16px";
  127. cancelButton.style.backgroundColor = "#ccc"; // 灰色背景
  128. cancelButton.style.color = "#333";
  129. cancelButton.style.border = "none";
  130. cancelButton.style.borderRadius = "5px";
  131. cancelButton.style.cursor = "pointer";
  132. cancelButton.addEventListener("click", function () {
  133. dialog.remove();
  134. });
  135. buttonContainer.appendChild(cancelButton);
  136. // 将对话框添加到页面
  137. document.body.appendChild(dialog);
  138. }
  139. // 将按钮添加到页面
  140. document.body.appendChild(executeButton);
  141. })();