Trying to create a theme component that shows when text is pasted

AI-generated summary

Andrew_Rowe shares an experiment in building a theme component that visually highlights text pasted into the Discourse composer. While initially inspired by the idea that visible pasting might discourage low-effort or bot-generated content, the project revealed technical limitations. ask.discourse advised that implementing robust rich-text editor (ProseMirror) integration requires a plugin, as theme components run in a sandboxed environment restricted to a whitelist of discourse/* modules and cannot access raw npm packages. Consequently, a pure theme component solution is limited.

Despite these constraints, Andrew developed a JavaScript snippet that listens for paste events on the .d-editor-input element. This script intercepts the paste, prevents the default action, and wraps the text in [wrap=pasted-text] BBCode. It includes fallback logic using document.execCommand and setRangeText to ensure text insertion works correctly. The accompanying CSS styles these wrapped blocks with a distinct border, background color, and a “Pasted text” label to make the content obvious to readers.

The post includes the complete JS and CSS code, along with a screenshot of the resulting styled quote block. Andrew notes that the component works on a live site but encountered permission errors in the Theme Creator sandbox. The author clarifies that they do not actually need this feature, as their site receives very little traffic and no bot spam, framing the project as a fun weekend exercise.

The discussion concludes with two open questions for the community: first, whether users would be deterred from pasting text if it were explicitly marked, and second, whether bots (which typically use the API) would bypass this UI-based detection entirely, rendering the feature ineffective against automated spam.

I was intrigued by the idea that if a user pasted text into the composer a theme component could be made to highlight the text and let the reader know the author had pasted in the text so i decided to let ask.discourse take a crack at it

Here is what I learned. ask.discourse came to conclusion that making it work in the rich text editor would require a plugin because:

Theme components run in a sandboxed JS environment that only allows importing from a whitelist of discourse/* modules. Raw npm packages like prosemirror-state and prosemirror-model are not available to theme components. The full Ember build pipeline (which bundles ProseMirror) is only accessible to plugins.

^^^ that was pasted in from ask.discourse

So anyhow, I could not get it to work in the Theme Creator because I kept getting an error

An error occurred: You are not permitted to view the requested resource.

so i tried it on my own site. Here is the js that ask.discourse gave me

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer((api) => {
  document.addEventListener("paste", function (e) {
    const target = e.target;
    if (!target || !target.matches(".d-editor-input")) return;

    const pasted = e.clipboardData?.getData("text/plain");
    if (!pasted) return;

    e.preventDefault();

    const wrapped =
      "\n\n[wrap=pasted-text]\n\n" + pasted + "\n\n[/wrap]\n\n";

    const inserted = document.execCommand("insertText", false, wrapped);

    if (!inserted) {
      const start = target.selectionStart;
      const end = target.selectionEnd;
      target.setRangeText(wrapped, start, end, "end");
      target.dispatchEvent(new Event("input", { bubbles: true }));
    }
  }, true);
});

and here is the css

[data-wrap="pasted-text"] {
  position: relative;
  border: 1px solid var(--primary-low);
  border-left: 4px solid var(--tertiary);
  border-radius: 6px;
  padding: 1.2em 1em 1em;
  margin: 0.75em 0;
  background: var(--primary-very-low);
}

[data-wrap="pasted-text"]::before {
  content: "📋 Pasted text";
  position: absolute;
  top: -0.6em;
  left: 12px;
  background: var(--tertiary);
  color: var(--secondary);
  font-size: 0.7em;
  font-weight: 700;
  padding: 0.15em 0.65em;
  border-radius: 3px;
  letter-spacing: 0.3px;
  text-transform: uppercase;
}

and here is the end results

Note: I don’t actually need this plugin or component!

my site isn’t very busy… I don’t even get bots trying to join :rofl:

just thought it would be a fun exercise on a lazy Sunday afternoon

question for the community:

Do you think users would think twice about pasting text if they knew it was going to be obvious to everyone reading it that the text was pasted? And is this really the problem? when a bot creates a user and posts garbage, do they use the api and thus would bypass all my efforts?