Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // ==UserScript==
  2. // @name        Seenthis Quotator
  3. // @namespace   seenthis
  4. // @description Easy answering with quote
  5. // @include     http://seenthis.net/*
  6. // @include     https://seenthis.net/*
  7. // @version     4
  8. // @grant       none
  9. // ==/UserScript==
  10.  
  11.  
  12.  
  13. // Extrait le nom à afficher à partir de l'élément .auteur
  14. function extractAuthorTitle(elt) {
  15.     var name = elt.find('strong').first().text().trim();
  16.     var login = elt.find('.login').first().text().trim();
  17.     if (isNameUseful(login, name))
  18.         return name + ' (' + login + ')';
  19.     else
  20.         return login;
  21. }
  22.  
  23.  
  24.  
  25. function getSelectedText() {
  26.     var text = "";
  27.     if (window.getSelection) {
  28.         text = window.getSelection().toString();
  29.     } else if (document.selection && document.selection.type != "Control") {
  30.         text = document.selection.createRange().text;
  31.     }
  32.     return text.replace('▻', '').replace('►', '').replace(/^\s*\n/gm, "");
  33. }
  34.  
  35.  
  36. function scrollToElement(selector, time, verticalOffset) {
  37.     time = typeof(time) != 'undefined' ? time : 1000;
  38.     verticalOffset = typeof(verticalOffset) != 'undefined' ? verticalOffset : 0;
  39.     element = $(selector);
  40.     offset = element.offset();
  41.     offsetTop = offset.top + verticalOffset;
  42.     $('html, body').animate({
  43.         scrollTop: offsetTop
  44.     }, time);
  45. }
  46.  
  47. function addSelectedTextInAnswer() {
  48.  
  49.     var selectedText = getSelectedText();
  50.     console.log(selectedText);
  51.     if (selectedText.length > 0) {
  52.         var selectNode = $(window.getSelection().anchorNode);
  53.         var cont = selectNode.parents('.reponse');
  54.         if (cont.length > 0) {
  55.             var article = cont.find('article');
  56.             var textarea = article.parents('li[id^=message]').find('.formulaire_poster_message textarea');
  57.  
  58.             var author = extractAuthorTitle(article);
  59.             insertAtTextareaCursor(textarea, author + ' \n❝' + selectedText + '❞\n');
  60.  
  61.         }
  62.  
  63.     }
  64. }
  65.  
  66.  
  67.  
  68. function insertAtTextareaCursor(elt, text) {
  69.  
  70.     var txtarea = elt[0];
  71.     var scrollPos = txtarea.scrollTop;
  72.     var strPos = 0;
  73.     var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
  74.         "ff" : (document.selection ? "ie" : false));
  75.     if (br == "ie") {
  76.         txtarea.focus();
  77.         var range = document.selection.createRange();
  78.         range.moveStart('character', -txtarea.value.length);
  79.         strPos = range.text.length;
  80.     } else if (br == "ff") strPos = txtarea.selectionStart;
  81.  
  82.     var front = (txtarea.value).substring(0, strPos);
  83.     var back = (txtarea.value).substring(strPos, txtarea.value.length);
  84.     txtarea.value = front + text + back;
  85.     strPos = strPos + text.length;
  86.     if (br == "ie") {
  87.         txtarea.focus();
  88.         var range = document.selection.createRange();
  89.         range.moveStart('character', -txtarea.value.length);
  90.         range.moveStart('character', strPos);
  91.         range.moveEnd('character', 0);
  92.         range.select();
  93.     } else if (br == "ff") {
  94.         txtarea.selectionStart = strPos;
  95.         txtarea.selectionEnd = strPos;
  96.         txtarea.focus();
  97.     }
  98.     txtarea.scrollTop = scrollPos;
  99. }
  100.  
  101.  
  102.  
  103. function isNameUseful(login, name) {
  104.     var nameRaw = name.toLowerCase();
  105.     nameRaw = nameRaw.replace(RegExp('[ @_-]', 'g'), '');
  106.     var loginRaw = login.toLowerCase();
  107.     loginRaw = loginRaw.replace(RegExp('[ @_-]', 'g'), '');
  108.     return nameRaw != loginRaw;
  109. }
  110.  
  111. function deleteWithLogin(login, things) {
  112.     for (var i = 0; i < things.length; i++) {
  113.         if (things[i]['login'] == login)
  114.             things.splice(i, 1);
  115.     }
  116.     return things;
  117. }
  118.  
  119. function deleteDuplicate(things) {
  120.     var arr = {};
  121.     for (var i = 0; i < things.length; i++)
  122.         arr[things[i]['login']] = things[i];
  123.  
  124.     things = new Array();
  125.     for (var key in arr)
  126.         things.push(arr[key]);
  127.  
  128.     return things;
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135. function openCommentInput(elt) {
  136.     var textarea = elt.find('.formulaire_poster_message textarea').first();
  137.     if (!textarea.is(":visible")) {
  138.         elt.find('.bouton_repondre a').click();
  139.     }
  140.  
  141.     scrollToElement(textarea, 500, -200);
  142.     textarea.focus();
  143. }
  144.  
  145.  
  146. // Change le comportement de l'élément @login sous un commentaire
  147. // cliquer sur le login l'insert dans le champ de réponse
  148. $('li[id^=message]').find('.auteur a').click(function() {
  149.  
  150.     var login = extractAuthorTitle($(this).parent()) + ' ';
  151.     var selTxt = getSelectedText();
  152.     var areaTxt = login;
  153.     if (selTxt.length > 0) {
  154.         areaTxt += "\n❝" + getSelectedText() + "❞";
  155.     }
  156.     openCommentInput($(this).parents('li[id^=message]'));
  157.     insertAtTextareaCursor($(this).parents('li[id^=message]').find('.formulaire_poster_message textarea'), areaTxt);
  158.     return false;
  159. });
  160.  
  161.  
  162. // Ajoute un comportement sur Ctrl + Alt + Shift + R
  163. // insert dans le champ de réponse le texte sélectionné ainsi que son auteur
  164. $(document).keydown(function(e) {
  165.     if (e.which == 82 && e.altKey && e.ctrlKey) {
  166.         var article = $(window.getSelection().anchorNode).parents('li[id^=message]');
  167.         if (article.length > 0) {
  168.             addSelectedTextInAnswer();
  169.             openCommentInput(article);
  170.         }
  171.     }
  172. });
  173.  
  174.  
  175. // Ajoute la liste des logins participant à la discussion sous le textarea
  176. $('#messages textarea').each(function() {
  177.  
  178.  
  179.     var cont = $('<div class="completeAuthor author"></div>');
  180.     cont.css('font-size', '0.7em');
  181.     cont.css('margin-top', '5px');
  182.     cont.css('float', 'left');
  183.  
  184.     $(this).parent().append(cont);
  185.     var authorList = [];
  186.     var authors = [];
  187.     $(this).parents('li[id^=message]')
  188.         .find('.auteur').each(function() {
  189.             var name = $(this).find('strong').first().text();
  190.             var login = $(this).find('a').first().attr('href').
  191.             replace('people/', '');
  192.             authorList.push(name);
  193.             authors.push({
  194.                 'login': login,
  195.                 'name': name
  196.             });
  197.         });
  198.  
  199.     authors = deleteDuplicate(authors);
  200.  
  201.     authors = deleteWithLogin($('#entete .perso .nom_auteur').text(), authors);
  202.  
  203.  
  204.     for (var i = 0; i < authors.length; i++) {
  205.  
  206.         var name;
  207.         if (isNameUseful(authors[i]['login'], authors[i]['name']))
  208.             name = authors[i]['name'] + '(@' + authors[i]['login'] + ')';
  209.         else
  210.             name = '@' + authors[i]['login'];
  211.  
  212.  
  213.  
  214.         var html = '<span><a class="login quotator_login" data-login="' + authors[i]['login'] + '" href="javascript:void(0);">' + name + '</a> ';
  215.  
  216.         if (i < authors.length - 1)
  217.             html += ' &bullet; ';
  218.  
  219.         html += '</span>';
  220.         cont.append($(html));
  221.  
  222.     }
  223.  
  224.  
  225.     cont.find('a').each(function() {
  226.         $(this).css('text-decoration', 'none').css('color', '#333');
  227.     });
  228.  
  229.     cont.find('.quotator_login').click(function() {
  230.         insertAtTextareaCursor($(this).parents('li[id^=message]').first().find('.formulaire_poster_message textarea').first(), '@' + $(this).attr('data-login') + ' ');
  231.     });
  232.  
  233.  
  234. });