Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ─────────── Logging ───────────
- console.log("[auto-chatgpt] content.js loaded on", location.href);
- // ─────────── PROMPTS ───────────
- const PROMPT_1 = "Your first prompt goes here.";
- const PROMPT_2 = "Your second prompt goes here.";
- // ───────── helper: waitFor ─────────
- function waitFor(selector, timeout = 30000) {
- return new Promise((resolve, reject) => {
- const start = Date.now();
- const iv = setInterval(() => {
- const el = document.querySelector(selector);
- if (el) {
- clearInterval(iv);
- console.log("[auto-chatgpt] Found", selector);
- return resolve(el);
- }
- if (Date.now() - start > timeout) {
- clearInterval(iv);
- return reject(new Error(`Timeout waiting for selector: ${selector}`));
- }
- }, 300);
- });
- }
- // ───────── helper: waitForId ─────────
- function waitForId(id, timeout = 30000) {
- return new Promise((resolve, reject) => {
- const start = Date.now();
- const iv = setInterval(() => {
- const el = document.getElementById(id);
- if (el) {
- clearInterval(iv);
- console.log("[auto-chatgpt] Found #"+id);
- return resolve(el);
- }
- if (Date.now() - start > timeout) {
- clearInterval(iv);
- return reject(new Error(`Timeout waiting for #${id}`));
- }
- }, 300);
- });
- }
- // ───────── sendMessage ─────────
- async function sendMessage(text) {
- console.log("[auto-chatgpt] Sending prompt:", text);
- // 1) Wait for the ProseMirror editor
- const editor = await waitFor("div[id='prompt-textarea']");
- console.log("[auto-chatgpt] Found editor");
- // 2) Focus and move the caret to the end
- editor.focus();
- const sel = window.getSelection();
- const range = document.createRange();
- range.selectNodeContents(editor);
- range.collapse(false);
- sel.removeAllRanges();
- sel.addRange(range);
- // 3) Insert the text as if typed
- document.execCommand("insertText", false, text);
- // 4) Wait for the submit button by ID, then click it
- const submitBtn = await waitForId("composer-submit-button");
- submitBtn.click();
- console.log("[auto-chatgpt] Clicked submit button");
- }
- // ───────── wait for response ─────────
- function waitForResponseComplete(timeout = 60000) {
- return new Promise((resolve, reject) => {
- const container = document.querySelector("div.flex.flex-col.items-center");
- if (!container) return reject(new Error("Chat container not found"));
- const obs = new MutationObserver(() => {
- // spinner is an svg with animate-spin
- if (!container.querySelector("svg.animate-spin")) {
- obs.disconnect();
- console.log("[auto-chatgpt] Response complete");
- resolve();
- }
- });
- obs.observe(container, { childList: true, subtree: true });
- setTimeout(() => {
- obs.disconnect();
- reject(new Error("Timed out waiting for response to finish"));
- }, timeout);
- });
- }
- // ───────── run the two prompts ─────────
- async function runPrompts() {
- try {
- console.log("[auto-chatgpt] runPrompts() starting");
- // this implicitly waits for the right input before sending
- await sendMessage(PROMPT_1);
- await waitForResponseComplete();
- await sendMessage(PROMPT_2);
- console.log("[auto-chatgpt] Done");
- } catch (err) {
- console.error("[auto-chatgpt] Error in runPrompts:", err);
- }
- }
- // ───────── trigger on load & SPA nav ─────────
- function shouldRun() {
- // fire on any chat page
- return (
- location.host.includes("chatgpt.com") ||
- location.host.includes("openai.com")
- );
- }
- function hookSPA() {
- const origPush = history.pushState;
- history.pushState = function () {
- origPush.apply(this, arguments);
- window.dispatchEvent(new Event("locationchange"));
- };
- window.addEventListener("popstate", () =>
- window.dispatchEvent(new Event("locationchange"))
- );
- window.addEventListener("locationchange", () => {
- console.log("[auto-chatgpt] locationchange →", location.href);
- if (shouldRun()) runPrompts();
- });
- }
- window.addEventListener("load", () => {
- console.log("[auto-chatgpt] Window load:", location.href);
- hookSPA();
- if (shouldRun()) {
- runPrompts();
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment