// ==UserScript==
// @name Seenthis Quotator
// @namespace seenthis
// @description Easy answering with quote
// @include http://seenthis.net/*
// @include https://seenthis.net/*
// @version 4
// @grant none
// ==/UserScript==
// Extrait le nom à afficher à partir de l'élément .auteur
function extractAuthorTitle(elt) {
var name = elt.find('strong').first().text().trim();
var login = elt.find('.login').first().text().trim();
if (isNameUseful(login, name))
return name + ' (' + login + ')';
else
return login;
}
function getSelectedText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text.replace('▻', '').replace('►', '').replace(/^\s*\n/gm, "");
}
function scrollToElement(selector, time, verticalOffset) {
time = typeof(time) != 'undefined' ? time : 1000;
verticalOffset = typeof(verticalOffset) != 'undefined' ? verticalOffset : 0;
element = $(selector);
offset = element.offset();
offsetTop = offset.top + verticalOffset;
$('html, body').animate({
scrollTop: offsetTop
}, time);
}
function addSelectedTextInAnswer() {
var selectedText = getSelectedText();
console.log(selectedText);
if (selectedText.length > 0) {
var selectNode = $(window.getSelection().anchorNode);
var cont = selectNode.parents('.reponse');
if (cont.length > 0) {
var article = cont.find('article');
var textarea = article.parents('li[id^=message]').find('.formulaire_poster_message textarea');
var author = extractAuthorTitle(article);
insertAtTextareaCursor(textarea, author + ' \n❝' + selectedText + '❞\n');
}
}
}
function insertAtTextareaCursor(elt, text) {
var txtarea = elt[0];
var scrollPos = txtarea.scrollTop;
var strPos = 0;
var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
"ff" : (document.selection ? "ie" : false));
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart('character', -txtarea.value.length);
strPos = range.text.length;
} else if (br == "ff") strPos = txtarea.selectionStart;
var front = (txtarea.value).substring(0, strPos);
var back = (txtarea.value).substring(strPos, txtarea.value.length);
txtarea.value = front + text + back;
strPos = strPos + text.length;
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart('character', -txtarea.value.length);
range.moveStart('character', strPos);
range.moveEnd('character', 0);
range.select();
} else if (br == "ff") {
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
}
txtarea.scrollTop = scrollPos;
}
function isNameUseful(login, name) {
var nameRaw = name.toLowerCase();
nameRaw = nameRaw.replace(RegExp('[ @_-]', 'g'), '');
var loginRaw = login.toLowerCase();
loginRaw = loginRaw.replace(RegExp('[ @_-]', 'g'), '');
return nameRaw != loginRaw;
}
function deleteWithLogin(login, things) {
for (var i = 0; i < things.length; i++) {
if (things[i]['login'] == login)
things.splice(i, 1);
}
return things;
}
function deleteDuplicate(things) {
var arr = {};
for (var i = 0; i < things.length; i++)
arr[things[i]['login']] = things[i];
things = new Array();
for (var key in arr)
things.push(arr[key]);
return things;
}
function openCommentInput(elt) {
var textarea = elt.find('.formulaire_poster_message textarea').first();
if (!textarea.is(":visible")) {
elt.find('.bouton_repondre a').click();
}
scrollToElement(textarea, 500, -200);
textarea.focus();
}
// Change le comportement de l'élément @login sous un commentaire
// cliquer sur le login l'insert dans le champ de réponse
$('li[id^=message]').find('.auteur a').click(function() {
var login = extractAuthorTitle($(this).parent()) + ' ';
var selTxt = getSelectedText();
var areaTxt = login;
if (selTxt.length > 0) {
areaTxt += "\n❝" + getSelectedText() + "❞";
}
openCommentInput($(this).parents('li[id^=message]'));
insertAtTextareaCursor($(this).parents('li[id^=message]').find('.formulaire_poster_message textarea'), areaTxt);
return false;
});
// Ajoute un comportement sur Ctrl + Alt + Shift + R
// insert dans le champ de réponse le texte sélectionné ainsi que son auteur
$(document).keydown(function(e) {
if (e.which == 82 && e.altKey && e.ctrlKey) {
var article = $(window.getSelection().anchorNode).parents('li[id^=message]');
if (article.length > 0) {
addSelectedTextInAnswer();
openCommentInput(article);
}
}
});
// Ajoute la liste des logins participant à la discussion sous le textarea
$('#messages textarea').each(function() {
var cont = $('<div class="completeAuthor author"></div>');
cont.css('font-size', '0.7em');
cont.css('margin-top', '5px');
cont.css('float', 'left');
$(this).parent().append(cont);
var authorList = [];
var authors = [];
$(this).parents('li[id^=message]')
.find('.auteur').each(function() {
var name = $(this).find('strong').first().text();
var login = $(this).find('a').first().attr('href').
replace('people/', '');
authorList.push(name);
authors.push({
'login': login,
'name': name
});
});
authors = deleteDuplicate(authors);
authors = deleteWithLogin($('#entete .perso .nom_auteur').text(), authors);
for (var i = 0; i < authors.length; i++) {
var name;
if (isNameUseful(authors[i]['login'], authors[i]['name']))
name = authors[i]['name'] + '(@' + authors[i]['login'] + ')';
else
name = '@' + authors[i]['login'];
var html = '<span><a class="login quotator_login" data-login="' + authors[i]['login'] + '" href="javascript:void(0);">' + name + '</a> ';
if (i < authors.length - 1)
html += ' • ';
html += '</span>';
cont.append($(html));
}
cont.find('a').each(function() {
$(this).css('text-decoration', 'none').css('color', '#333');
});
cont.find('.quotator_login').click(function() {
insertAtTextareaCursor($(this).parents('li[id^=message]').first().find('.formulaire_poster_message textarea').first(), '@' + $(this).attr('data-login') + ' ');
});
});