# Prevent text selection and right-click actions

**URL:** <https://meta.discourse.org/t/prevent-text-selection-and-right-click-actions/311289>\
**Category:** Development\
**Created:** [June 9, 2024, 10:56am UTC](https://meta.discourse.org/t/prevent-text-selection-and-right-click-actions/311289 "2024-06-09T10:56:13Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![kokoloman001](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kokoloman001/32/389438_2.png) [@kokoloman001](https://meta.discourse.org/u/kokoloman001)\
**Post date:** [June 9, 2024, 10:56am UTC](https://meta.discourse.org/t/prevent-text-selection-and-right-click-actions/311289/1 "2024-06-09T10:56:13Z")

</div>

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**

```plaintext
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**

```plaintext
<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.  
[https://github.com/MHAT-DEV/Discourse-Prevent-Protects/](https://github.com/MHAT-DEV/Discourse-Prevent-Protects/)

 ![Screenshot 2024-06-09 174611](https://global.discourse-cdn.com/meta/original/4X/9/8/0/9807d4a86d0fd14264a863be982e0d7fb6a8935e.png)

---

_[View the full topic](https://meta.discourse.org/t/prevent-text-selection-and-right-click-actions/311289)._
