heres the new improved code
tested in IE and firefox
this new one should fix the crazy selection issues (blame the function quickquotesel1 for that : <) and if you're using a browser that supports selectionStart and selectionEnd then it should preserve the caret position
GM version:
http://version.thedt.net/scripts/GM/quickquotetester.user.js
code:
function quickquote(pid){
var sel = getCurrentSelection();
var messageElement = document.getElementsByName('message')[0];
if(!sel || sel == ""){
var postdata = document.getElementsByName('qq' + pid)[0].value;
var username = document.getElementsByName('qqu' + pid)[0].value;
insertAtCurrentSelection("[quote=" + username + "]" + postdata + "[/quote]\r\n", messageElement);
}else{
insertAtCurrentSelection("[quote]" + sel + "[/quote]\n", messageElement);
}
}
//inserts at the current selection and moves carret
function insertAtCurrentSelection(text, element){
if(element.selectionStart !== undefined && element.selectionEnd !== undefined){
var value = element.value;
var startString = value.substring(0, element.selectionStart);
var endString = value.substring(element.selectionEnd, value.length);
var newSelPos = element.selectionEnd + text.length;
element.value = startString + text + endString;
element.focus();
element.setSelectionRange(newSelPos, newSelPos);
}else{
//IE doesnt have per input selection, only document.selection, so we cannot insert at the correct position :(
element.value += text;
element.focus();
}
}
function getCurrentSelection(){
//good browsers
if(window.getSelection){
return window.getSelection().toString();
//IE
}else if(document.selection && document.selection.type == 'Text' && document.selection.createRange){
return document.selection.createRange().text;
}else{
return false;
}
}