ChatGPT-Question-Automation

Read a file and input its contents into an input field

  1. // ==UserScript==
  2. // @name ChatGPT-Question-Automation
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.15
  5. // @description Read a file and input its contents into an input field
  6. // @author You
  7. // @match https://chat.openai.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. ("use strict");
  14. const panel = document.createElement("div");
  15. panel.style.position = "fixed";
  16. panel.style.top = "0";
  17. panel.style.right = "0";
  18. panel.style.backgroundColor = "white";
  19. panel.style.padding = "10px";
  20. panel.style.border = "1px solid black";
  21. panel.style.width = "100px"; // 控制面板的宽度
  22. panel.style.fontFamily = "'Arial', sans-serif"; // 设定字体
  23. panel.style.backgroundColor = "#f2f2f2"; // 更改背景颜色为浅灰色
  24. panel.style.fontFamily = "'Arial', sans-serif"; // 设定字体
  25. panel.style.borderRadius = "10px"; // 添加圆角
  26. document.body.appendChild(panel);
  27.  
  28. // 创建一个新的悬浮层
  29. const hoverPanel = document.createElement("div");
  30. hoverPanel.style.position = "fixed";
  31. hoverPanel.style.top = "0";
  32. hoverPanel.style.right = "110px"; // 需要手动调整位置以避免和主面板重叠
  33. hoverPanel.style.backgroundColor = "white";
  34. hoverPanel.style.padding = "10px";
  35. hoverPanel.style.border = "1px solid black";
  36. hoverPanel.style.width = "200px"; // 控制悬浮层的宽度
  37. hoverPanel.style.fontFamily = "'Arial', sans-serif"; // 设定字体
  38. hoverPanel.style.opacity = "0"; // 初始设置为透明
  39. hoverPanel.style.backgroundColor = "#f2f2f2"; // 更改背景颜色为浅灰色
  40. hoverPanel.style.borderRadius = "10px"; // 添加圆角
  41. hoverPanel.style.transition = "opacity 0.3s ease"; // 设置渐变动画
  42. document.body.appendChild(hoverPanel);
  43.  
  44. // 创建一个显示悬浮层的函数
  45. const showHoverPanel = () => {
  46. clearTimeout(timeoutId); // 如果已经设置了一个隐藏悬浮层的定时器,先清除它
  47. hoverPanel.style.opacity = "1"; // 完全不透明
  48. hoverPanel.style.pointerEvents = "auto"; // 恢复鼠标事件
  49. };
  50.  
  51. // 创建一个隐藏悬浮层的函数
  52. const hideHoverPanel = () => {
  53. timeoutId = setTimeout(() => {
  54. hoverPanel.style.opacity = "0"; // 完全透明
  55. hoverPanel.style.pointerEvents = "none"; // 当不可见时,禁止鼠标事件
  56. }, 200); // 延迟200毫秒后隐藏悬浮层
  57. };
  58.  
  59. // 当鼠标悬浮在面板上时,显示悬浮层
  60. panel.addEventListener("mouseover", showHoverPanel);
  61.  
  62. // 当鼠标离开面板时,隐藏悬浮层
  63. panel.addEventListener("mouseout", hideHoverPanel);
  64.  
  65. // 当鼠标悬浮在悬浮层上时,保持悬浮层显示
  66. hoverPanel.addEventListener("mouseover", showHoverPanel);
  67.  
  68. // 当鼠标离开悬浮层时,隐藏悬浮层
  69. hoverPanel.addEventListener("mouseout", hideHoverPanel);
  70.  
  71. // 在面板上添加文件输入元素
  72. const fileInput = document.createElement("input");
  73. fileInput.type = "file";
  74. fileInput.style.marginTop = "10px"; // 增加间距
  75. fileInput.style.width = "100%"; // 控制输入框的宽度
  76. panel.appendChild(fileInput);
  77.  
  78. // 显示文件名的标签
  79. const fileNameLabel = document.createElement("span");
  80. fileNameLabel.style.display = "block"; // 使得每个元素占用一行
  81. fileNameLabel.style.marginTop = "10px"; // 增加间距
  82. fileNameLabel.style.overflow = "hidden"; // 超出部分隐藏
  83. fileNameLabel.style.textOverflow = "ellipsis"; // 使用省略号表示超出部分
  84. panel.appendChild(fileNameLabel);
  85.  
  86. // 在面板上添加输入元素以获取通用提示
  87. const promptLabel = document.createElement("span");
  88. promptLabel.textContent = "Prompt: ";
  89. promptLabel.style.display = "block"; // 使得每个元素占用一行
  90. hoverPanel.appendChild(promptLabel);
  91. const promptInput = document.createElement("input");
  92. promptInput.type = "text";
  93. promptInput.style.marginTop = "10px"; // 增加间距
  94. promptInput.style.width = "100%"; // 控制输入框的宽度
  95. hoverPanel.appendChild(promptInput);
  96.  
  97. // 创建一个新的 div 元素用来容纳标签和复选框
  98. const restDiv = document.createElement("div");
  99. restDiv.style.display = "flex";
  100. restDiv.style.alignItems = "center"; // 使元素在垂直方向上居中对齐
  101. restDiv.style.justifyContent = "space-between"; // 使元素在水平方向上均匀分布
  102. restDiv.style.width = "100%"; // 控制 div 的宽度
  103. hoverPanel.appendChild(restDiv);
  104.  
  105. // 在面板上添加是否每25次发送后休息的复选框
  106. const restLabel = document.createElement("label");
  107. restLabel.textContent = "sleep after every 25 sends";
  108. restDiv.appendChild(restLabel);
  109.  
  110. const restCheckbox = document.createElement("input");
  111. restCheckbox.type = "checkbox";
  112. restDiv.appendChild(restCheckbox);
  113.  
  114. // 每次信息发送后,暂停时间输入
  115. const delayInput = document.createElement("input");
  116. delayInput.type = "number";
  117. delayInput.style.marginTop = "10px"; // 增加间距
  118. delayInput.style.width = "100%"; // 控制输入框的宽度
  119. delayInput.placeholder = "sleeptime(s)";
  120. hoverPanel.appendChild(delayInput);
  121.  
  122. // 在面板上添加确认按钮
  123. const confirmButton = document.createElement("button");
  124. confirmButton.textContent = "确认";
  125. confirmButton.style.marginTop = "10px"; // 增加间距
  126. confirmButton.style.width = "100%"; // 控制按钮的宽度
  127. confirmButton.style.backgroundColor = "#4CAF50"; // 更改按钮颜色为绿色
  128. confirmButton.style.color = "white"; // 更改按钮文字颜色为白色
  129. confirmButton.style.border = "none"; // 移除按钮边框
  130. confirmButton.style.cursor = "pointer"; // 当鼠标悬停在按钮上时,改变光标样式为手型
  131. confirmButton.style.borderRadius = "5px"; // 添加圆角
  132. panel.appendChild(confirmButton);
  133.  
  134. confirmButton.addEventListener("mouseover", function () {
  135. confirmButton.style.backgroundColor = "#45a049"; // 当鼠标悬停在按钮上时,改变按钮颜色
  136. });
  137.  
  138. confirmButton.addEventListener("mouseout", function () {
  139. confirmButton.style.backgroundColor = "#4CAF50"; // 当鼠标移开按钮时,恢复按钮颜色
  140. });
  141.  
  142. // 在面板上添加任务完成进度的标签
  143. const progressBar = document.createElement("progress");
  144. progressBar.style.width = "100%"; // 控制进度条的宽度
  145. progressBar.style.marginTop = "10px"; // 增加间距
  146. progressBar.max = 1; // 最大值设为1(代表100%)
  147. panel.appendChild(progressBar);
  148.  
  149. // 在面板上添加一个用于显示提示的 div 元素
  150. const alertDiv = document.createElement("div");
  151. alertDiv.style.position = "fixed";
  152. alertDiv.style.top = "10px";
  153. alertDiv.style.right = "10px";
  154. alertDiv.style.padding = "10px";
  155. alertDiv.style.backgroundColor = "red";
  156. alertDiv.style.color = "white";
  157. alertDiv.style.display = "none"; // 默认隐藏
  158. alertDiv.style.backgroundColor = "#f44336"; // 更改警告框背景颜色为红色
  159. alertDiv.style.color = "white"; // 更改警告框文字颜色为白色
  160. alertDiv.style.borderRadius = "5px"; // 添加圆角
  161. document.body.appendChild(alertDiv);
  162.  
  163. // 创建一个函数用于显示提示
  164. function showAlert(message) {
  165. alertDiv.textContent = message;
  166. alertDiv.style.display = "block"; // 显示提示
  167. setTimeout(function () {
  168. alertDiv.style.display = "none"; // 3 秒后隐藏提示
  169. }, 3000);
  170. }
  171.  
  172. // 当用户点击确认按钮时,开始执行操作
  173. confirmButton.addEventListener("click", function () {
  174. let fileContent;
  175.  
  176. // 如果 localStorage 中存在文件
  177. if (localStorage.getItem("savedFile")) {
  178. // 从 localStorage 获取文件内容
  179. fileContent = localStorage.getItem("savedFile");
  180. } else {
  181. // 从文件输入框获取文件
  182. const file = fileInput.files[0];
  183. if (!file) {
  184. showAlert("请先选择文件");
  185. return;
  186. }
  187. const reader = new FileReader();
  188. reader.onload = function (e) {
  189. fileContent = e.target.result;
  190. handleFileContent(fileContent);
  191. };
  192. reader.readAsText(file);
  193. return;
  194. }
  195.  
  196. handleFileContent(fileContent);
  197. });
  198.  
  199. function handleFileContent(fileContent) {
  200. // 用 fileContent 进行后续操作...
  201. const jsonData = JSON.parse(fileContent);
  202.  
  203. if (Array.isArray(jsonData)) {
  204. let messagesSent = 0;
  205. let messageCount = 0;
  206.  
  207. // 在 handleFileContent 中,获取通用提示
  208. const prompt = promptInput.value || localStorage.getItem("prompt");
  209. localStorage.setItem("prompt", prompt);
  210. progressBar.max = jsonData.length; // 设置进度条的最大值
  211.  
  212. function sendMessage() {
  213. if (messageCount >= jsonData.length) {
  214. // taskProgressLabel.textContent = "Finish!"; // 更新任务进度标签的内容
  215. progressBar.value = jsonData.length; // 将进度条设为满值
  216. return;
  217. }
  218.  
  219. const inputField = document.querySelector("#prompt-textarea");
  220. if (!inputField) {
  221. return;
  222. }
  223. const item = jsonData[messageCount++];
  224. //添加通用提示到消息的开头
  225. inputField.value = prompt + item.title;
  226. // inputField.value = item.title;
  227.  
  228. var inputEvent = new Event("input", { bubbles: true });
  229. inputField.dispatchEvent(inputEvent);
  230.  
  231. setTimeout(function () {
  232. var submit_button = document.querySelector(
  233. "#__next > div.overflow-hidden.w-full.h-full.relative.flex.z-0 > div > div > main > div.absolute.bottom-0.left-0.w-full.border-t.md\\:border-t-0.dark\\:border-white\\/20.md\\:border-transparent.md\\:dark\\:border-transparent.md\\:bg-vert-light-gradient.bg-white.dark\\:bg-gray-800.md\\:\\!bg-transparent.dark\\:md\\:bg-vert-dark-gradient.pt-2 > form > div > div.flex.flex-col.w-full.py-\\[10px\\].flex-grow.md\\:py-4.md\\:pl-4.relative.border.border-black\\/10.bg-white.dark\\:border-gray-900\\/50.dark\\:text-white.dark\\:bg-gray-700.rounded-xl.shadow-xs.dark\\:shadow-xs > button"
  234. );
  235. submit_button.click();
  236. }, 1000);
  237.  
  238. var sleep_time = 60000 || delayInput.value * 1000;
  239. messagesSent++;
  240.  
  241. // taskProgressLabel.textContent = `progress rate : ${messagesSent} / ${jsonData.length} `; // 更新任务进度标签的内容
  242. progressBar.value = messagesSent; // 更新进度条的值
  243.  
  244. if (restCheckbox.checked && messagesSent >= 25) {
  245. // 如果用户选中了复选框,并且已经发送了25个消息,等待三小时再发送下一个
  246. setTimeout(sendMessage, 3 * 60 * 60 * 1000 - 25 * sleep_time);
  247. messagesSent = 0;
  248. } else {
  249. setTimeout(sendMessage, sleep_time);
  250. }
  251. }
  252.  
  253. sendMessage();
  254. }
  255. }
  256. // 当文件改变时,更新标签中的文件名
  257. fileInput.addEventListener("change", function () {
  258. if (this.files && this.files.length) {
  259. fileNameLabel.textContent = this.files[0].name;
  260. // 将文件内容存储到 localStorage
  261. var reader = new FileReader();
  262. reader.onload = function (event) {
  263. localStorage.setItem("savedFile", event.target.result);
  264. localStorage.setItem("savedFileName", fileInput.files[0].name); // 存储文件名
  265. };
  266. reader.readAsText(this.files[0]);
  267. }
  268. });
  269.  
  270. // 当页面加载时,尝试从 localStorage 恢复文件名
  271. window.addEventListener("load", function () {
  272. var savedFileName = localStorage.getItem("savedFileName");
  273. if (savedFileName) {
  274. fileNameLabel.textContent = savedFileName; // 更新标签中的文件名
  275. }
  276. var prompt = localStorage.getItem("prompt");
  277. if (prompt) {
  278. promptInput.value = prompt;
  279. }
  280. });
  281. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址