Skip to content

Instantly share code, notes, and snippets.

@Talleyran
Created October 30, 2012 05:41
Show Gist options
  • Save Talleyran/3978507 to your computer and use it in GitHub Desktop.
Save Talleyran/3978507 to your computer and use it in GitHub Desktop.
Lorquotes UserScript
// ==UserScript==
// @name lorquotes
// @namespace lor
// @include http://www.linux.org.ru/forum/*/*
// @exclude http://www.linux.org.ru/forum/*/
// @include http://www.linux.org.ru/news/*/*
// @exclude http://www.linux.org.ru/news/*/
// @version 1
// ==/UserScript==
let quotesStore = {}
let usersStore = {}
let textArea = document.getElementById('msg')
let commentForm = document.getElementById('commentForm')
let replyButtons = document.body.getElementsByClassName('reply')
let createQuoteButtonClick = function(e){
e.preventDefault();
e.stopPropagation();
if( this.getAttribute('class') == 'quotesShowed' ){
this.setAttribute('class','');
this.menu.style.display = 'none'
} else {
this.setAttribute('class','quotesShowed');
this.menu.style.display = 'block'
}
}
let createClearQuotesButton = function(){
let clearButton = document.createElement( 'a' );
clearButton.href = '#';
clearButton.style.color = '#f00';
clearButton.style.backgroundColor = '#111';
clearButton.innerHTML = '[x_x]';
clearButton.style.textDecoration = 'none';
clearButton.style.lineHeight = '18.2px';
clearButton.style.fontSize = '13px';
clearButton.style.display = 'inline-block';
clearButton.style.position = 'relative';
clearButton.style.marginLeft = '16px';
return clearButton
}
let incQuotes = function(){
this.quotesCount += 1
if( this.quotesCount == 1){
this.style.color = '#111';
this.style.backgroundColor = '#0df';
}
}
let decQuotes = function(){
if( this.quotesCount > 0) this.quotesCount -= 1
if( this.quotesCount == 0){
this.style.color = '#0df';
this.style.backgroundColor = '#111';
}
}
let createQuoteButton = function(menu){
let quoteButton = document.createElement( 'a' );
window.addEventListener('click', function(){
menu.style.display = 'none'
menu.parentNode.setAttribute('class','')
})
quoteButton.quotesCount = 0
quoteButton.menu = menu;
quoteButton.incQuotes = incQuotes
quoteButton.decQuotes = decQuotes
quoteButton.onclick = createQuoteButtonClick;
quoteButton.href = '#';
quoteButton.style.color = '#0df';
quoteButton.style.backgroundColor = '#111';
quoteButton.innerHTML = '[>_>]';
quoteButton.style.textDecoration = 'none';
quoteButton.style.lineHeight = '18.2px';
quoteButton.style.fontSize = '13px';
quoteButton.style.display = 'inline-block';
quoteButton.style.position = 'relative';
quoteButton.style.marginLeft = '16px';
return quoteButton
}
let createQuoteMenu = function(){
let menu = document.createElement( 'div' );
menu.style.lineHeight = '18.2px';
menu.style.fontSize = '13px';
menu.style.position = 'absolute';
menu.style.maxWidth = '200px';
menu.style.overflow = 'hidden';
menu.style.left = '0';
menu.style.whiteSpace = 'nowrap';
menu.style.display = 'none';
return menu
}
let html2lorcode = function(text){
let t = text.replace(/\<br\>/gi,'[br]');
t = t.replace(/[\n\r]+/gm, " ");
t = t.replace(/\<a\shref/gi, '[url');
t = t.replace(/title=".+"/gi, '');
t = t.replace(/\<\/a/gi, '[/url');
t = t.replace(/\>/gi, ']');
return t
}
let sendText = function(){
let currentUser;
let nameLink = ( commentForm.parentNode.parentNode.getAttribute('class') == 'reply' ? commentForm.parentNode.parentNode.parentNode : commentForm.parentNode.parentNode.parentNode).getElementsByClassName('sign')[0].getElementsByTagName('a')[0];
currentUser = nameLink ? nameLink.innerHTML.trim() : 'anonymous';
let allQuotes = [];
let currentQuotes = {}
for(let k in quotesStore){
if( currentQuotes[usersStore[k]] == undefined ) currentQuotes[usersStore[k]] = []
for(let i=0; i<quotesStore[k].length; i++){
let message = quotesStore[k][i];
if(message != undefined && message != null){
let level_text = '>';
for(let j = 0; j < message.level; j++) level_text += '>';
currentQuotes[usersStore[k]].push(level_text + html2lorcode(message.text))
}
}
}
if(currentQuotes[currentUser][0] != undefined) allQuotes.push( currentQuotes[currentUser].join('\r\r') )
for( k in currentQuotes){
if(currentUser != k && currentQuotes[k][0] != undefined){
allQuotes.push( '>[user]' + k + '[/user]' + "\r")
allQuotes[allQuotes.length - 1] += currentQuotes[k].join('\r\r');
}
}
textArea.value = allQuotes.join('\r\r')
if(allQuotes.length > 0) textArea.value += '\r'
}
let onQuoteMenuItemClick = function(e){
e.preventDefault();
e.stopPropagation();
if( this.getAttribute('class') == 'quoteAdded' ){
this.setAttribute('class','');
delete quotesStore[this.postId][this.pnum];
this.style.backgroundColor = '#222';
this.style.color = '#fff';
this.quoteButton.decQuotes()
} else {
this.setAttribute('class','quoteAdded');
quotesStore[this.postId][this.pnum] = {text: this.realText, level: this.level};
this.style.backgroundColor = '#fd0';
this.style.color = '#000'
this.quoteButton.incQuotes()
}
sendText()
}
let createQuoteMenuItem = function(quoteButton,clearQuotesButton,v,postId,pnum,level){
let menuItem = document.createElement( 'a' );
menuItem.quoteButton = quoteButton;
clearQuotesButton.addEventListener('click', function(e){
e.preventDefault();
e.stopPropagation();
menuItem.setAttribute('class','');
delete quotesStore[menuItem.postId][menuItem.pnum];
menuItem.style.backgroundColor = '#222';
menuItem.style.color = '#fff';
menuItem.quoteButton.decQuotes()
sendText()
})
menuItem.onclick = onQuoteMenuItemClick;
menuItem.href = '#';
menuItem.innerHTML = v.replace(/\<br\>/gim, " ");
for(let i=0; i<level; i++) menuItem.insertBefore(quoteSpan(), menuItem.firstChild);
menuItem.realText = v;
menuItem.postId = postId;
menuItem.pnum = pnum;
menuItem.level = level;
menuItem.style.backgroundColor = '#222';
menuItem.style.padding = '3px';
menuItem.style.paddingTop = '5px';
menuItem.style.paddingBottom = '5px';
menuItem.style.color = '#fff';
menuItem.style.textDecoration = 'none';
menuItem.style.display = 'block';
menuItem.style.boxShadow = '0 0 1px 2px #000';
menuItem.style.margin = '2px';
return menuItem
}
let quoteSpan = function(v){
let qs = document.createElement( 'span' );
qs.innerHTML = '&gt;';
qs.style.color = '#0df';
qs.style.textWeight = 'bold';
qs.style.fontSize = '18px';
return qs
}
for ( let i=0; i < replyButtons.length; i++ ) {
let replyButton = replyButtons[i];
replyButton.style.display = 'inline-block';
let quoteMenu = createQuoteMenu();
let quoteButton = createQuoteButton(quoteMenu);
let clearQuotesButton = createClearQuotesButton();
let title;
let message;
let replyParent = replyButton.parentNode;
if(replyParent.parentNode.id == ''){
message = replyParent.parentNode.getElementsByTagName('div')[0];
title = message.parentNode.getElementsByTagName('h1')[0]
} else {
message = replyParent;
title = message.parentNode.getElementsByTagName('div')[0]
}
let usertag = replyButton.parentNode.getElementsByClassName('sign')[0].getElementsByTagName('a')[0];
let username = usertag == undefined ? 'anonymous' : usertag.innerHTML.trim();
let postId = message.parentNode.id || 'post';
let pnum = 0;
quotesStore[postId] = [];
usersStore[postId]=username;
let si = function(el,l){
if(el.nodeName == 'P'){
quoteMenu.appendChild(createQuoteMenuItem(quoteButton, clearQuotesButton, el.innerHTML, postId, pnum, l));
pnum ++
}else if(el.nodeName == 'DIV' && el.className == 'quote'){
for( let k=0; k < el.childNodes.length; k++){
si(el.childNodes[k],l+1)
}
}
}
let j = 0;
while(message.childNodes[j] && message.childNodes[j] != replyButton && j < 20 ){
si(message.childNodes[j],0);
j++
}
title.parentNode.insertBefore(clearQuotesButton, title.nextSibling);
title.parentNode.insertBefore(quoteButton, title.nextSibling);
quoteButton.appendChild(quoteMenu)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment