shubhamgoyal

content.js

Jun 23rd, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. // ─────────── Logging ───────────
  2. console.log("[auto-chatgpt] content.js loaded on", location.href);
  3.  
  4. // ─────────── PROMPTS ───────────
  5. const PROMPT_1 = "Your first prompt goes here.";
  6. const PROMPT_2 = "Your second prompt goes here.";
  7.  
  8. // ───────── helper: waitFor ─────────
  9. function waitFor(selector, timeout = 30000) {
  10. return new Promise((resolve, reject) => {
  11. const start = Date.now();
  12. const iv = setInterval(() => {
  13. const el = document.querySelector(selector);
  14. if (el) {
  15. clearInterval(iv);
  16. console.log("[auto-chatgpt] Found", selector);
  17. return resolve(el);
  18. }
  19. if (Date.now() - start > timeout) {
  20. clearInterval(iv);
  21. return reject(new Error(`Timeout waiting for selector: ${selector}`));
  22. }
  23. }, 300);
  24. });
  25. }
  26.  
  27. // ───────── helper: waitForId ─────────
  28. function waitForId(id, timeout = 30000) {
  29. return new Promise((resolve, reject) => {
  30. const start = Date.now();
  31. const iv = setInterval(() => {
  32. const el = document.getElementById(id);
  33. if (el) {
  34. clearInterval(iv);
  35. console.log("[auto-chatgpt] Found #"+id);
  36. return resolve(el);
  37. }
  38. if (Date.now() - start > timeout) {
  39. clearInterval(iv);
  40. return reject(new Error(`Timeout waiting for #${id}`));
  41. }
  42. }, 300);
  43. });
  44. }
  45.  
  46. // ───────── sendMessage ─────────
  47. async function sendMessage(text) {
  48. console.log("[auto-chatgpt] Sending prompt:", text);
  49.  
  50. // 1) Wait for the ProseMirror editor
  51. const editor = await waitFor("div[id='prompt-textarea']");
  52. console.log("[auto-chatgpt] Found editor");
  53.  
  54. // 2) Focus and move the caret to the end
  55. editor.focus();
  56. const sel = window.getSelection();
  57. const range = document.createRange();
  58. range.selectNodeContents(editor);
  59. range.collapse(false);
  60. sel.removeAllRanges();
  61. sel.addRange(range);
  62.  
  63. // 3) Insert the text as if typed
  64. document.execCommand("insertText", false, text);
  65.  
  66. // 4) Wait for the submit button by ID, then click it
  67. const submitBtn = await waitForId("composer-submit-button");
  68. submitBtn.click();
  69. console.log("[auto-chatgpt] Clicked submit button");
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. // ───────── wait for response ─────────
  79. function waitForResponseComplete(timeout = 60000) {
  80. return new Promise((resolve, reject) => {
  81. const container = document.querySelector("div.flex.flex-col.items-center");
  82. if (!container) return reject(new Error("Chat container not found"));
  83.  
  84. const obs = new MutationObserver(() => {
  85. // spinner is an svg with animate-spin
  86. if (!container.querySelector("svg.animate-spin")) {
  87. obs.disconnect();
  88. console.log("[auto-chatgpt] Response complete");
  89. resolve();
  90. }
  91. });
  92. obs.observe(container, { childList: true, subtree: true });
  93.  
  94. setTimeout(() => {
  95. obs.disconnect();
  96. reject(new Error("Timed out waiting for response to finish"));
  97. }, timeout);
  98. });
  99. }
  100.  
  101. // ───────── run the two prompts ─────────
  102. async function runPrompts() {
  103. try {
  104. console.log("[auto-chatgpt] runPrompts() starting");
  105. // this implicitly waits for the right input before sending
  106. await sendMessage(PROMPT_1);
  107. await waitForResponseComplete();
  108. await sendMessage(PROMPT_2);
  109. console.log("[auto-chatgpt] Done");
  110. } catch (err) {
  111. console.error("[auto-chatgpt] Error in runPrompts:", err);
  112. }
  113. }
  114.  
  115.  
  116. // ───────── trigger on load & SPA nav ─────────
  117. function shouldRun() {
  118. // fire on any chat page
  119. return (
  120. location.host.includes("chatgpt.com") ||
  121. location.host.includes("openai.com")
  122. );
  123. }
  124.  
  125. function hookSPA() {
  126. const origPush = history.pushState;
  127. history.pushState = function () {
  128. origPush.apply(this, arguments);
  129. window.dispatchEvent(new Event("locationchange"));
  130. };
  131. window.addEventListener("popstate", () =>
  132. window.dispatchEvent(new Event("locationchange"))
  133. );
  134. window.addEventListener("locationchange", () => {
  135. console.log("[auto-chatgpt] locationchange →", location.href);
  136. if (shouldRun()) runPrompts();
  137. });
  138. }
  139.  
  140. window.addEventListener("load", () => {
  141. console.log("[auto-chatgpt] Window load:", location.href);
  142. hookSPA();
  143. if (shouldRun()) {
  144. runPrompts();
  145. }
  146. });
  147.  
Advertisement
Add Comment
Please, Sign In to add comment