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

AI-generated summary

The discussion explores the feasibility and utility of a Discourse theme component that highlights pasted text. Andrew_Rowe shares an experiment where he attempted to create a component that wraps pasted content in a [wrap=pasted-text] block to visually flag it for readers. However, he discovered that rich text editor (ProseMirror) integration requires a full plugin because theme components are sandboxed and lack access to necessary npm packages. He provided the JavaScript and CSS code for the component, which successfully marks pasted text on his test site. Andrew_Rowe questions whether this would effectively deter users from pasting or if it addresses the real problem, which he suspects is bot activity via the API.

Community feedback was largely skeptical of the solution’s effectiveness. Moin argued that pasting is a standard editing behavior (e.g., moving paragraphs, using external editors) and noted that users could easily delete the wrapper markup, rendering the warning useless or intrusive for legitimate users. Andrew_Rowe acknowledged a critical flaw raised by another user (Rajat_99): bots often post directly via the Discourse API, bypassing any client-side paste listeners.

The conversation shifted to API security and bot detection. nickdb mentioned restricting API access for new users as a common mitigation strategy. Andrew_Rowe and merefield discussed how bots operate; merefield clarified that while user_api_key_allowed_groups controls API key access, sophisticated agents can now use “computer use” automation to simulate a normal user interface, effectively spoofing human behavior without needing an API key. awesomerobot noted that Discourse’s frontend is essentially an API client, so distinguishing between direct API calls and UI automation is complex.

Despite these hurdles, Andrew_Rowe proposed a nuanced approach inspired by Discourse’s review queue, which flags suspiciously fast typing. He suggested a hybrid system where pasted text is only flagged if it exceeds a certain length (e.g., 200 characters) or if the user doesn’t provide an explanation for the paste. The goal is to target “wall of text” AI responses rather than penalize standard editing, while acknowledging that bots often ignore deletions, unlike humans.

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.

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

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