30

About

This user script disables the default behavior of the enter key submitting a comment. With this script installed, you must explicitly click the Add Comment button to submit your comment.

Source

If you have Firefox+GreaseMonkey, Opera, or Google Chrome, try this GreaseMonkey script that will disable enter in comments.

// ==UserScript==
// @name           Disable Enter key on comments on Stack Exchange.
// @namespace      http://networkpx.googlecode.com
// @description    Disable Enter key on comments on Stack Exchange.
// @include        http://*.stackexchange.com/*
// @include        http://stackoverflow.com/*
// @include        https://meta.stackoverflow.com/*
// @include        http://serverfault.com/*
// @include        http://meta.serverfault.com/*
// @include        http://superuser.com/*
// @include        http://meta.superuser.com/*
// @include        http://askubuntu.com/*
// @include        http://meta.askubuntu.com/*
// ==/UserScript==

var s = document.createElement("script");
    s.innerHTML = '$(document).delegate(".comments-link, .comment-edit", "click", function(e) { \
  var events = $(this).closest("td").find("textarea[name=comment]").data("events"); \
  var makeShift = { handler: function(e) { e.shiftKey = true; } }; \
  events.keyup.splice(0, 0, makeShift); \
  events.keypress.splice(0, 0, makeShift); \
});';
document.body.appendChild(s);

Installation

Click here to download this script.

See the script info page for step-by-step install instructions

https://stackapps.com/tags/script/info

5
  • 1
    P.S. This works on Firefox with GreaseMonkey - I tested it. Commented Feb 17, 2011 at 19:26
  • 2
    @Nick: Thanks, now it also prevents the Enter key from posting the comment while editing too! Commented Jun 7, 2011 at 18:43
  • please you didn't include stackapps in your script.
    – Mohammad
    Commented Mar 17, 2014 at 18:17
  • 1
    I've a weird issue with this on Chrome 35. When this script is enabled and I click "add comment", nothing happens except going to the top of the page...I don't see why that should happen. Any thoughts? Commented Jun 13, 2014 at 14:33
  • @RodyOldenhuis I can confirm. brasofolio's version seems to work.
    – mzso
    Commented Oct 31, 2016 at 12:58

1 Answer 1

3

I created another version of the script modifying:

  • Shift + Enter submits the comment.
  • Changed @includes for @matches using another domain pattern.
  • Used another script injection technique.
  • Tried to solve instances where the script would produce an error wrapping the block where the error occurs within a try{} catch(){}. Then I discovered that adding a timeout made the script work in pages where it was failing. I'll leave both for now, but probably the timeout suffices.
// ==UserScript==
// @name            Disable Enter key on comments on Stack Exchange.
// @namespace       https://stackapps.com/a/4910
// @version         1.1
// @description     Disable Enter key on comments on Stack Exchange.
// @homepage        https://stackapps.com/q/2061
// @icon            https://i.sstatic.net/6xzO1.png
// @match           *://*.askubuntu.com/questions*
// @match           *://*.mathoverflow.net/questions*
// @match           *://*.serverfault.com/questions*
// @match           *://*.stackapps.com/questions*
// @match           *://*.stackexchange.com/questions*
// @match           *://*.stackoverflow.com/questions*
// @match           *://*.superuser.com/questions*
// ==/UserScript==

function inject() {
    for ( var i = 0; i < arguments.length; ++i ) {
        if ( typeof(arguments[i]) == 'function' ) {
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.textContent = '(' + arguments[i].toString() + ')(jQuery)';
            document.body.appendChild(script);
        }
    }
}

inject(function ($) {
    setTimeout( function() {
        // Run only on individual posts
        if( ( StackExchange.options.routeName.indexOf('Questions/Show') === -1 ) ) 
            return;

        $(document).delegate(".comments-link, .comment-edit", "click", function(e) {        
            var events = $(this).closest("td").find("textarea[name=comment]").data("events");
            var makeShift = { handler: function(e) { 
                if( e.keyCode === 13 && e.shiftKey )
                    e.shiftKey = false; 
                else
                    e.shiftKey = true; 
            } };
            try {
                events.keyup.splice(0, 0, makeShift);
                events.keypress.splice(0, 0, makeShift);
            }
            catch (evt) {
                console.error('Disable Enter key on comments on Stack Exchange failed to load');
            }
        });
    }, 500 );
});

Install link

1
  • Looks this works unreliably. It worked when I tried to comment this answer. Then it didn't with the OP. Edit: And now it failed again here too. Weird Edit2: I was mistaken. It doesn't work for comments and all. (I was typing into the answer field by accident)
    – mzso
    Commented Oct 31, 2016 at 13:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .