Prevent text selection and right-click actions

I’ve already attempted looking for ways to prevent right-clicks and text selection with the mouse. Discourse has that available, however I tried a few other ways. but haven’t yet discovered the ideal approach. I’m looking for some code to use as a result. And I discovered this method.

add this to css

body * {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }

  button, .menu-item {
    pointer-events: auto !important;
  }

and, add javascript to head

<script type="text/discourse-plugin" version="0.8">
  if (settings.prevent_right_click) {
    document.addEventListener('contextmenu', function(e) {
      e.preventDefault(); // ยกเลิกการกระทำเรื่องคลิกขวา

      // ตรวจสอบว่าการแสดงข้อความแจ้งเตือนถูกเปิดหรือไม่
      if (settings.alert_enabled) {
        alert(settings.alert_message);
      } else {
        // ตรวจสอบว่าการป้องกันการเลือกข้อความถูกเปิดหรือไม่
        if (settings.prevent_text_selection) {
          
        }
      }
    });
  }
</script>

I gave it a go on my forum. There have been no anomalies discovered at the user level. However, I’m still unsure if it has any further adverse effects.
To test it out, I made a theme component.
If my method is incorrect or if you know a better one. Kindly inform me.

Why would you want to do this? If you’re trying to protect text from being copied, there are a million other ways that it could be copied so you’re not actually protecting anything, just annoying legitimate users.

5 Likes

Thank you for your reply.
They copy and paste as if it were commonplace on my community forum. Thus, I think that this will make it more difficult for them to duplicate. My community’s writers have urged me to preserve their works. in order for them to post it on the forum. Despite my explanation that it cannot be blocked. At the very least, inform writers that their work is being preserved.
In terms of the annoyances that users can have when using the forums, I think there is still space for improvement. Furthermore, I’m not sure that my ideas are totally accurate. If there is something better, I’m willing to give it a shot.

2 Likes

You could look at more active flagging and moderation.

The situation is only going to get worse with generative AI.

2 Likes

I just few hours ago used AI to copy&paste piece of text when admin was banned right click. It was way easier solution than traditionally ones, with iPad anyway.

So…

2 Likes

You can also just directly copy out of page source if someone was so inclined, or there’s also extensions to forcibly override the disabled text selections (and right click).

2 Likes

Maybe your changes will convince the people who are generating text and that’ll convince them that you are trying to help, but here are two very easy easy ways to get the text that bypass javascript completely.

https://meta.discourse.org/raw/311289/3 and https://meta.discourse.org/t/prevent-text-selection-and-right-click-actions/311289/3.json

3 Likes