// ==UserScript==
// @name QuoteSelection
// @namespace kkapsner.de
// @description Creates a quote of the selected text.
// @include http://forum.jswelt.de/*
// @version 1
// ==/UserScript==
function getParentByTagName(node, tagName){
tagName = tagName.toUpperCase();
do {
node = node.parentNode;
} while(node && node.tagName !== tagName);
return node;
}
document.addEventListener("keydown", function(ev){;
if (String.fromCharCode(ev.keyCode) === "Q" && ev.ctrlKey && !ev.altKey && ev.target.nodeName !== "textarea"){
var textarea = document.querySelector("#cke_contents_vB_Editor_QR_editor textarea");
if (textarea){
ev.preventDefault();
var selection = window.getSelection();
var text = selection.toString();
if (text){
var postBase = getParentByTagName(selection.anchorNode, "li");
var user = postBase.querySelector(".userinfo .username strong, .userinfo_noavatar .username strong").textContent;
var postNumber = postBase.querySelector(".posthead a[id^=postcount]").id.replace(/\D/g, "");
var selectionStart = textarea.selectionStart;
var insertion = "[quote=" + user + ";" + postNumber + "]" + text + "[/quote]\n";
textarea.value = textarea.value.substring(0, textarea.selectionStart) +
insertion +
textarea.value.substring(textarea.selectionEnd);
textarea.selectionStart = textarea.selectionEnd = selectionStart. insertion.length;
}
}
}
}, false);