Take the 2-minute tour ×
Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. It's 100% free, no registration required.

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        http://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

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

share|improve this question
    
P.S. This works on Firefox with GreaseMonkey - I tested it. –  Nathan Osman Feb 17 '11 at 19:26
1  
@Nick: Thanks, now it also prevents the Enter key from posting the comment while editing too! –  Tsuyoshi Ito Jun 7 '11 at 18:43
    
please you didn't include stackapps in your script. –  Mhmd Mar 17 at 18:17
    
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? –  Rody Oldenhuis Jun 13 at 14:33

1 Answer 1

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       http://stackapps.com/a/4910
// @version         1.1
// @description     Disable Enter key on comments on Stack Exchange.
// @homepage        http://stackapps.com/q/2061
// @icon            http://i.stack.imgur.com/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

share|improve this answer

You must log in to answer this question.

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