Discourse AI Write

Advanced AI Write, powered by Gemini. Write your discourse posts with ease. Quickly ask questions.

Fra 22.04.2025. Se den seneste versjonen.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Discourse AI Write
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Advanced AI Write, powered by Gemini. Write your discourse posts with ease. Quickly ask questions.
// @author       ethandacat
// @match        https://x-camp.discourse.group/*
// @icon         https://avatars.githubusercontent.com/u/78333227?s=200&v=4
// @grant        none
// @license      MIT
// ==/UserScript==

async function askGemini(prompt) {
  try {
    const res = await fetch('https://ai-write-nine.vercel.app/proxy/', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    prompt,
    session_id: "discourse" // or make this dynamic if you want per-editor-session chats
  })
});

    if (!res.ok) return `(Gemini error ${res.status})`;
    const data = await res.json();
    const text = data?.candidates?.[0]?.content?.parts?.[0]?.text;
    return text ?? "(no response)";
  } catch (err) {
    return `(Fetch error: ${err.message})`;
  }
}

function addButton() {
  const bar = document.querySelector(".d-editor-button-bar");
  if (!bar || document.querySelector(".ai-write-button")) return;

  const button = document.createElement("button");
  button.className = "btn no-text btn-icon quote ai-write-button";
  button.tabIndex = 0;
  button.title = "AI Write";
  button.innerHTML = `
    <svg class="fa d-icon d-icon-microchip-ai svg-icon svg-string" xmlns="http://www.w3.org/2000/svg">
      <use href="#robot"></use>
    </svg>
  `;

  button.addEventListener("click", () => {
    const pophtml = `
    <div class="modal-container">
      <div class="modal d-modal poll-ui-builder" data-keyboard="false" aria-modal="true" role="dialog" aria-labelledby="discourse-modal-title">
        <div class="d-modal__container">
          <div class="d-modal__header">
            <div class="d-modal__title">
              <h1 id="discourse-modal-title" class="d-modal__title-text">Discourse AI Write</h1>
            </div>
            <button class="btn no-text btn-icon btn-transparent modal-close d-ai-close" title="close" type="button">
              <svg class="fa d-icon d-icon-xmark svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"><use href="#xmark"></use></svg>
              <span aria-hidden="true">​</span>
            </button>
          </div>
          <div class="d-modal__body" tabindex="-1">
            <div class="poll-options">
              <p>What should Gemini revise or generate?</p>
              <div class="input-group poll-option-value">
                <input type="text" autofocus class="d-ai-inp">
              </div>
              <p>Thank you for using Discourse AI Write by Ethan!</p>
            </div>
          </div>
          <div class="d-modal__footer">
            <button class="btn btn-icon-text btn-primary d-ai-gen" type="button">
              <span class="d-button-label">Generate!</span>
            </button>
            <button class="btn btn-text btn-flat d-ai-close2" type="button">
              <span class="d-button-label">cancel</span>
            </button>
          </div>
        </div>
      </div>
      <div class="d-modal__backdrop"></div>
    </div>
    `;

    const pop = document.createElement("div");
    pop.innerHTML = pophtml;
    document.querySelector(".discourse-root").appendChild(pop);

    document.querySelector(".d-ai-close").onclick = () => document.querySelector(".modal-container")?.remove();
    document.querySelector(".d-ai-close2").onclick = () => document.querySelector(".modal-container")?.remove();

    const input = document.querySelector(".d-ai-inp");
    const genBtn = document.querySelector(".d-ai-gen");

    genBtn.onclick = async () => {
      const inptxt = input.value;
      const modal = document.querySelector(".modal-container");

      genBtn.disabled = true;
      genBtn.innerHTML = "<span class='d-button-label'>Loading...</span>";

      const textarea = document.querySelector("textarea.d-editor-input");
      if (textarea) {
        const result = await askGemini(inptxt);
        textarea.value = result;
        textarea.dispatchEvent(new Event("input", { bubbles: true }));
      }
        document.querySelector(".modal-container").remove();
        document.querySelector(".modal-container").remove();
        document.querySelector(".modal-container").remove();
    };

    input.addEventListener("keydown", e => {
      if (e.key === "Enter") genBtn.click();
      else if (e.key === "Escape") document.querySelector(".modal-container")?.remove();
    });
  });

  bar.appendChild(button);
  console.log("AI Write button added");
}

function reliableWaitForEditor(callback) {
  let lastEditor = null;
  const check = () => {
    const editor = document.querySelector(".d-editor-button-bar");
    if (editor && editor !== lastEditor) {
      lastEditor = editor;
      callback();
    }
  };
  setInterval(check, 300);
  document.addEventListener("visibilitychange", check);
  window.addEventListener("hashchange", check);
  window.addEventListener("focus", check);
}

reliableWaitForEditor(addButton);