This seems to work. Requires greasemonkey for Firefox but should work in IE if you have one of the GreaseMonkey equivalents.
It creates a fourth button next to the Reply, Reply With Quote links.
![Quoting posts-qftext-png](https://cboard.cprogramming.com/attachments/general-discussions/10765d1312239412-quoting-posts-qftext-png?s=fb3befcc0e272225adcf1d5dd8adbb90)
Highlight some text and click any of the buttons and it should magically appear in the quote box.
I'm by no means a javascripter so it could probably be better implemented. If anybody wants to improve it, knock yourself out :-)
Code:
// ==UserScript==
// @name Cboard quote reply-er
// @description Quote highlighted text on cboard
// @include http://cboard.cprogramming.com/*
// @run-at document-end
// ==/UserScript==
window.addEventListener("load", function(e) {
addLinks();
}, false);
//
// From http://robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
//
function getElementsByClassName(oElm, strTagName, strClassName)
{
var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\\-");
var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
var oElement;
for(var i=0; i<arrElements.length; ++i)
{
oElement = arrElements[i];
if(oRegExp.test(oElement.className))
{
arrReturnElements.push(oElement);
}
}
return (arrReturnElements);
}
function addButtonListener(name){
var button = document.getElementById(name);
if(button.addEventListener)
{
button.addEventListener('click', QuoteHighlightedText, true);
}
}
function addLinks(){
var postControls = getElementsByClassName(document, "span", "postcontrols");
var numPostControls = postControls.length;
for(var i = 0; i < numPostControls; ++i)
{
var thisId = 'highlightQuoter' + i;
postControls[i].innerHTML = postControls[i].innerHTML + ' <input type="button" value="Quote Highlighted Text" id="' + thisId + '" onclick="QuoteHighlightedText(); return false;" />';
addButtonListener(thisId);
}
}
function GetHighlightTextAndLocation()
{
var highlightedText;
var highlightLocation;
if (window.getSelection)
{
// all browsers, except IE before version 9
var highlightedTextRange = window.getSelection();
highlightedText = highlightedTextRange.toString();
highlightLocation = highlightedTextRange.anchorNode;
// firefox seems to put text into their own child nodes
// we need the blockquote element here, so get it, if this is the case
if(highlightLocation.parentNode.tagName == "BLOCKQUOTE")
{
highlightLocation = highlightLocation.parentNode;
}
}
else
{
var highlightedTextRange = document.selection.createRange();
highlightedText = highlightedTextRange.text;
highlightLocation = highlightedTextRange.parentElement();
}
return {text: highlightedText, loc: highlightLocation};
}
function GetPostId(selectionLoc)
{
var postMessageDiv = selectionLoc.parentNode;
var idString = postMessageDiv.id;
var underScoreIndex = idString.lastIndexOf("_");
return idString.substring(underScoreIndex + 1);
}
function GetParent(node, numLevels)
{
// firefox
if(node.parentNode)
{
while(numLevels)
{
node = node.parentNode;
--numLevels;
}
}
// IE
else if(node.parentElement)
{
while(numLevels)
{
node = node.parentElement;
--numLevels;
}
}
return node;
}
function NavigateFirstChildren(node, numLevels)
{
if(node.firstElementChild)
{
while(numLevels)
{
node = node.firstElementChild;
--numLevels;
}
}
else
{
while(numLevels)
{
node = node.firstChild;
--numLevels;
}
}
return node;
}
function GetPreviousSibling(node, num)
{
if(node.previousElementSibling)
{
while(num)
{
node = node.previousElementSibling;
--num;
}
}
else
{
while(num)
{
node = node.previousSibling;
--num;
}
}
return node;
}
function GetPosterNameAndId(selectionLoc)
{
var postBodyDiv = selectionLoc;
var isInQuote = ((selectionLoc.className == "message") && (selectionLoc.tagName == "DIV"));
if(isInQuote)
{
// get to the postcontext blockQuote
postBodyDiv = GetParent(selectionLoc, 4);
}
// now get to the actual postbody div
var postId = GetPostId(postBodyDiv);
postBodyDiv = GetParent(postBodyDiv, 4);
// get to the userInfo div
var userInfoDiv = GetPreviousSibling(postBodyDiv, 1);
// now go down to the user name anchor
var userNameAnchor = NavigateFirstChildren(userInfoDiv, 4);
var userName;
if(userNameAnchor.textContent)
{
userName = userNameAnchor.textContent;
}
else
{
userName = userNameAnchor.innerText;
}
return {userName: userName, id: postId};
}
function QuoteHighlightedText()
{
var highlight = GetHighlightTextAndLocation();
var postDetails = GetPosterNameAndId(highlight.loc);
// the "[quote" is split up because
// it caused a quote box in the middle of the code
var quoteText = "[qu" + "ote=" + postDetails.userName + ";" + postDetails.id + "]" + highlight.text + "[/quote]\n";
var postBox = document.getElementsByTagName("textarea")[1];
if(postBox)
{
postBox.value = quoteText;
postBox.focus();
}
else alert("Can't find the Reply box, are you logged in?");
}