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

AI-generated summary

Andrew_Rowe shares an experiment creating a theme component that visually highlights pasted text in the composer, aiming to discourage low-quality AI-generated “wall of text” posts. He discovered that while a plugin is required for deep ProseMirror integration, a basic JavaScript workaround can detect paste events in the standard editor, wrapping the content in a [wrap=pasted-text] block with distinct CSS styling.

The community debated the feature’s utility. Moin argued that pasting is a normal user behavior (e.g., moving paragraphs, using translators) and highlighting it would only irritate legitimate users without stopping determined actors. Rajat_99 noted that client-side detection is easily bypassed by typing, using external editors, or using the API, recommending reliance on built-in spam tools instead.

A key limitation highlighted is that bots often use the API or automated tools. Andrew_Rowe initially worried that API access was a show-stopper, but nickdb suggested restricting API access for new users as a better strategy. merefield clarified that while API access requires specific group permissions, advanced AI agents can now simulate normal user behavior by automating browser inputs, bypassing paste listeners entirely.

Andrew_Rowe concluded that Discourse’s existing review queue, which flags suspiciously fast typing, is already an effective first line of defense. He envisioned his feature not as a hard block, but as a transparency tool: allowing a small amount of pasted text before flagging it, giving authors a chance to explain, and warning readers about potential bot content. He believes distinguishing between human effort and automated garbage is the real goal, rather than strictly preventing pasting.

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?

What is the problem with pasting a link instead of typing it?
Would you disable the “copy quote” option on your forum, because it only makes sense if pasting is allowed?

  • I don’t think every piece of pasted content needs to be marked as such.

What prevents me from deleting the wrapper the component adds after I paste the text?

  • If I am able to delete it, why would it stop me?

Even if it wasn’t possible to remove it, I would still use copy and paste to change the order of my paragraphs, paste a screenshot, the result of what I wrote with the help of a translator, or simply what I wrote on another forum because I dislike the redesigned composer.

Would the fact that those are marked as pasted change how you read them?

I don’t think pasting is a problem in general. A component which would highlight that one of the sentences above had been pasted wouldn’t stop me from doing it. I hope the fact that I moved a sentence by copying and pasting wouldn’t change how you read it. In case it’s possible, I would have removed it, because it only interrupts the reader by separating sentences that still belong together.

@Andrew_Rowe Interesting experiment! However, this approach has limitations:

  • The Theme Component only detects clipboard paste events in the traditional Markdown composer.
  • Users can bypass it by typing, using another editor, or submitting through the API.
  • Bots can create posts directly through the Discourse API without triggering the paste listener.
  • Highlighting pasted text might discourage some users, but it won’t prevent spam.

My recommendation: Use Discourse’s built-in trust levels, rate limits, spam detection and moderation tools. Treat pasted-text highlighting as a visual feature, not a security measure.

A plugin would be needed for deeper server-side integration, but even that cannot reliably distinguish pasted text from manually typed text.

that one is obviously a show stopper, that is where the garbage usually originates I fear

@Andrew_Rowe Exactly. In that case, API restrictions, rate limits and server-side spam detection are the better solutions. A theme component won’t catch API-generated spam.

I simply disabled API access for new users, as that is where almost all the issues are. In the current environment, API access should be trust-based and leaving it wide open is an unnecessary backdoor.

can they? I thought making a post in a topic or starting a new topic using the api required a key. I’m guessing that meta doesn’t hand out api keys to brand new users.

I’m just trying to understand. So the spam posts here on meta are bots or agents that have been trained to spoof the new user sign up process?

EDIT:

OK maybe I answered my own question. This topic from 2021 seems to indicate that if a user is registered and is allowed to post in a given category they are allowed to use the api to do so.

So to take this one step forward, a bot is trained to sign up and create a user then use the api to post? That doesn’t really seem to match what I see getting picked up by the spam bot.

I’m guess if you can train a bot to create a new user, it wouldn’t be to difficult to train it to use the normal manner to post. Just curious as to how this works

Not quite.

They need to be in a group included in user_api_key_allowed_groups (which @nickdb alluded to)

However, regardless of that control, so long as the user is authenticated, an agent can automate via computer use and spoof as a normal user.

AI Computer Use is so advanced and fast now it is, I imagine, fairly hard to detect or defeat.

so not using the api, just automating the normal posting method?

Correct, yes indeed, simulating a normal user posting.

Discourse is one big API, all an API key really does is provide an alternative method of authentication separate from username and password

I figured that was the case based on what I see in the review queue. The review queue has a little note that tells us if the user typed really fast. Kris made it so we can see how long, most the time it is a few tenths of a second. This is kind of the first line of defense beyond the spam bot.

Brand new user. Typed to fast and smelly content → bounced

My idea all along with my little experiment was that a user would get say a 200 characters allowed to be pasted before triggering the ‘pasted text’ box. And also the user would be given the chance to provide an explanation such as ‘I typed this in another editor’

The idea is to ding the ‘wall of text’ posts typical of AI responses.

That to me is the injustice, some machine spits out three of four paragraphs of useless garbage and the user is half way thru it before it becomes apparent they are wasting their time.

If the spam bot can tell a post was typed to quickly to likely be a human then a box could be put around questionable post content warning the user ‘this is likely a bot’. If the author objects, fine say something and it will be removed.

Bots don’t seem to mind their posts getting deleted. Humans usually ask what happen