Skip to main content
added 441 characters in body
Source Link
Benjol
  • 1.7k
  • 3
  • 13
  • 19

UPDATE, now converts " to « and » (alternately).

Click HERE to install the user script.

// ==UserScript==
// @name NonBreakSpacesNonBreakSpaces+
// @namespace benjol
// @description Make Ctrl-space add a non-break-space in textareas
// @include http://french.stackexchange.com/questions/*
// @include http://meta.french.stackexchange.com/questions/*
// @include http://chat.stackexchange.com/rooms/1098/*
// ==/UserScript==
function inject(f) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.textContent = "(" + f.toString() + ")()";
  document.body.appendChild(script);
};

//Note that if you copy in Firefox, you'll get the impression that the nbsp isn't there, but it is!
// (https://bugzilla.mozilla.org/show_bug.cgi?id=359303)
inject(function () {
  $(document)var nbsp = String.keydown(functionfromCharCode(event160) {,
    if(event.ctrlKey && event.keyCode ==left 32)= {
"«" + nbsp, //171
     var fieldright = event.target, nbsp =+ String.fromCharCode(160);"»", //187
      next = left;

  function insert(field, val) {
    if(field.selectionStart || field.selectionStart == '0') {
          var cursorAfter = field.selectionStart + nbspval.length;
          var first = field.value.slice(0, field.selectionStart);
          var second = field.value.slice(field.selectionEnd);
          field.value = first + nbspval + second;
          field.setSelectionRange(cursorAfter, cursorAfter);
        }
        else //won't work in IE, sorry!
      field.value += val;
  }

  $(document).keypress(function(event) {
    var field = event.valuetarget;

 += nbsp;  if(event.ctrlKey && event.charCode == 32) {
        insert(field, nbsp);
        event.preventDefault();
    } else if (event.charCode == 34) {
        insert(field, next);
next = (next == left) ? right : left;
        event.preventDefault();
    };
  });
});

Click HERE to install the user script.

// ==UserScript==
// @name NonBreakSpaces
// @namespace benjol
// @description Make Ctrl-space add a non-break-space in textareas
// @include http://french.stackexchange.com/questions/*
// @include http://chat.stackexchange.com/rooms/1098/*
// ==/UserScript==
function inject(f) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.textContent = "(" + f.toString() + ")()";
  document.body.appendChild(script);
};

//Note that if you copy in Firefox, you'll get the impression that the nbsp isn't there, but it is!
// (https://bugzilla.mozilla.org/show_bug.cgi?id=359303)
inject(function () {
  $(document).keydown(function(event) {
    if(event.ctrlKey && event.keyCode == 32) {
        var field = event.target, nbsp = String.fromCharCode(160);
        
        if(field.selectionStart || field.selectionStart == '0') {
          var cursorAfter = field.selectionStart + nbsp.length;
          var first = field.value.slice(0, field.selectionStart);
          var second = field.value.slice(field.selectionEnd);
          field.value = first + nbsp + second;
          field.setSelectionRange(cursorAfter, cursorAfter);
        }
        else //won't work in IE, sorry!
          field.value += nbsp;
        event.preventDefault();
    }
  });
});

UPDATE, now converts " to « and » (alternately).

Click HERE to install the user script.

// ==UserScript==
// @name NonBreakSpaces+
// @namespace benjol
// @description Make Ctrl-space add a non-break-space in textareas
// @include http://french.stackexchange.com/questions/*
// @include http://meta.french.stackexchange.com/questions/*
// @include http://chat.stackexchange.com/rooms/1098/*
// ==/UserScript==
function inject(f) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.textContent = "(" + f.toString() + ")()";
  document.body.appendChild(script);
};

//Note that if you copy in Firefox, you'll get the impression that the nbsp isn't there, but it is!
// (https://bugzilla.mozilla.org/show_bug.cgi?id=359303)
inject(function () {
  var nbsp = String.fromCharCode(160),
      left = "«" + nbsp, //171
      right = nbsp + "»", //187
      next = left;

  function insert(field, val) {
    if(field.selectionStart || field.selectionStart == '0') {
      var cursorAfter = field.selectionStart + val.length;
      var first = field.value.slice(0, field.selectionStart);
      var second = field.value.slice(field.selectionEnd);
      field.value = first + val + second;
      field.setSelectionRange(cursorAfter, cursorAfter);
    }
    else //won't work in IE, sorry!
      field.value += val;
  }

  $(document).keypress(function(event) {
    var field = event.target;

    if(event.ctrlKey && event.charCode == 32) {
        insert(field, nbsp);
        event.preventDefault();
    } else if (event.charCode == 34) {
        insert(field, next);
next = (next == left) ? right : left;
        event.preventDefault();
    };
  });
});
Source Link
Benjol
  • 1.7k
  • 3
  • 13
  • 19

Convert Ctrl-space to non-break-space (for FL&U)

This is a mini-script to convert Ctrl+Space to a non-break-space (0x160, not  ).

You can copy-paste the code below (if you want to change the includes, for example), or check out the gist here:

Click HERE to install the user script.

Click HERE to preview the code.

// ==UserScript==
// @name NonBreakSpaces
// @namespace benjol
// @description Make Ctrl-space add a non-break-space in textareas
// @include http://french.stackexchange.com/questions/*
// @include http://chat.stackexchange.com/rooms/1098/*
// ==/UserScript==
function inject(f) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.textContent = "(" + f.toString() + ")()";
  document.body.appendChild(script);
};

//Note that if you copy in Firefox, you'll get the impression that the nbsp isn't there, but it is!
// (https://bugzilla.mozilla.org/show_bug.cgi?id=359303)
inject(function () {
  $(document).keydown(function(event) {
    if(event.ctrlKey && event.keyCode == 32) {
        var field = event.target, nbsp = String.fromCharCode(160);
        
        if(field.selectionStart || field.selectionStart == '0') {
          var cursorAfter = field.selectionStart + nbsp.length;
          var first = field.value.slice(0, field.selectionStart);
          var second = field.value.slice(field.selectionEnd);
          field.value = first + nbsp + second;
          field.setSelectionRange(cursorAfter, cursorAfter);
        }
        else //won't work in IE, sorry!
          field.value += nbsp;
        event.preventDefault();
    }
  });
});