Skip to content

Commit

Permalink
Add warning when posting code to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
int-y1 committed Jun 5, 2024
1 parent 5946470 commit c3d0cab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion templates/comments/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ <h3>{{ _('New comment') }}</h3>
{{ _('You must solve at least one problem before your voice can be heard.') }}
</div>
{% else %}
<form class="comment-submit-form" action="" method="post">
<form class="comment-submit-form{% if comment_warn_code %} comment-warn-code{% endif %}" action="" method="post">
{% csrf_token %}
{% if comment_form.errors %}
<div id="form-errors">
Expand Down
10 changes: 10 additions & 0 deletions templates/comments/media-js.html
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@
$comment.find('.comment-body').show();
$comment.find('.bad-comment-body').hide();
};

var code_regex = [/input\(/, /#include/, /void\s+main/, /fn\s+main/, /func\s+main/];
$(document).on('click', 'form.comment-warn-code .button[type=submit]', function (e) {
var text = $(this).parents('form').find('#id_body').val();
if (code_regex.some(function (regex) {return regex.test(text);})) {
if (!confirm({{ _('Looks like you\'re trying to post some source code!\n\nThe comment section is not for posting source code.\nIf you want to submit your solution, please use the "Submit solution" button.\n\nAre you sure you want to post this?')|htmltojs }})) {
e.preventDefault();
}
}
});
});
</script>
{% endcompress %}
4 changes: 3 additions & 1 deletion templates/problem/problem.html
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ <h2><i class="fa fa-question-circle"></i> {{ _('Clarifications') }}</h2>
{% endif %}
</div>
{% elif enable_comments %}
{% include "comments/list.html" %}
{% with comment_warn_code=true %}
{% include "comments/list.html" %}
{% endwith %}
{% endif %}
{% endblock %}

Expand Down

0 comments on commit c3d0cab

Please sign in to comment.