Advertisement
Guest User

Untitled

a guest
Apr 18th, 2013
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name LORCode Tools
  3. // @description Кнопка цитирования выделенного и панель тегов для LORCode
  4. // @author Алексей Соловьев aka moscwich
  5. // @version 0.21.3
  6. // @license Creative Commons Attribution 3.0 Unported
  7. // @namespace http://www.linux.org.ru/*
  8. // @namespace https://www.linux.org.ru/*
  9. // @include http://www.linux.org.ru/*
  10. // @include https://www.linux.org.ru/*
  11. // ==/UserScript==
  12.  
  13. // Based on MultiCodePanel 2.2 (v. 0.22)
  14. // http://al-moscwich.tk/tag/multicodepanel
  15.  
  16. // if (/https?:\/\/(www\.)?linux.org.ru/.test (u))
  17.  
  18. msg.wrtSel = function (subj, offset, before, after, zset) { //also msg.wrtSel (before, after, offset)
  19.     if (typeof offset == "string")
  20.         var
  21.             after = offset, offset = before,
  22.             before = subj, subj = undefined;
  23.     var
  24.         before = before || "", after = after || "",
  25.         offset = set (offset, before.length), zset = zset || 0;
  26.     var
  27.         startSel = set (a, this.selectionStart), endSel = set (b, this.selectionEnd),
  28.         subj = before + set (subj, this.value.substring (startSel, endSel)) + after;
  29.  
  30.     this.value = this.value.substring (0, startSel) + subj + this.value.substring (endSel);
  31.     this.focus (); this.setSelectionRange (startSel + offset, endSel + offset + zset);
  32.     a = b = undefined;
  33. }
  34.    
  35.  
  36. function removeElements () {
  37.     for (i = 0; i < arguments.length; i++) {
  38.         var p = arguments[i].parentNode;
  39.         if (p) p.removeChild (arguments[i]);
  40.     }
  41. }
  42. function set (p, z) {
  43.     for (i = 0; i < arguments.length && (arguments[i] === undefined); i++) {}
  44.     return arguments[i];
  45. }
  46.  
  47. i = j = undefined;
  48. a = b = undefined;
  49.  
  50. form = document.getElementById ("commentForm") || document.getElementById ("messageForm") || document.getElementById ("editRegForm");
  51. msg = document.getElementById ("msg") || document.getElementById ("form_msg") || document.getElementById ("info");
  52. var u = window.location.href;
  53.  
  54. // Panel
  55. var panel = document.createElement ("div");
  56. panel.id = 'atag';
  57. panel.createBlock =
  58.     function () {
  59.         block = document.createElement ("span");
  60.         for (i = 0; i < arguments.length; i++) {
  61.             link = document.createElement ("a");
  62.             link.textContent = arguments[i][0];
  63.             link.title = arguments[i][1];
  64.             link.exec = arguments[i][2];
  65.             link.onclick = function () {
  66.                 eval (this.exec);
  67.                 return false;
  68.             }
  69.             block.appendChild (link);
  70.         }
  71.         return this.appendChild (block);
  72.     }
  73. panel.createBlock (
  74.     ["[b]", "Полужирный", 'intag ("strong");'],
  75.     ["[i]", "Курсив", 'intag ("em");'],
  76.     ["[s]", "Зачеркнутый", 'intag ("s");'],
  77.     ["[u]", "Подчеркнутый", 'intag ("u");']
  78. );
  79. panel.createBlock (
  80.     ["[quote]", "Цитата", 'intag ("quote"); msg.parentNode.mode.selectedIndex = 0;'],
  81.     ["[code]", "Код", 'intag ("code"); msg.parentNode.mode.selectedIndex = 0;']
  82. );
  83. panel.createBlock (
  84.     ["[url]", "URL", 'url ();'],
  85.     ["[user]", "Участник", 'intag ("user");']
  86. );
  87. panel.createBlock (
  88.     ["[list]", "Список", 'intag ("list"); msg.parentNode.mode.selectedIndex = 0;'],
  89.     ["[*]", "Элемент списка", 'msg.wrtSel ("[*]", ""); msg.parentNode.mode.selectedIndex = 0;']
  90. );
  91. panel.createBlock (
  92.     ["«»", "Кавычки", 'msg.wrtSel ("«", "»");'],
  93.     ["„“", "Кавычки", 'msg.wrtSel ("„", "“");'],
  94.     ["[br]", "Перевод строки", 'msg.wrtSel ("[br]", "");']
  95. );
  96. panel.createBlock (
  97.     [" fix ", "Превратить знаки и обозначения в соответствующие спец. символы", 'fix ();'],
  98.     [" deltags-in ", "Удалить крайнее входящие обрамление тегами", 'deltagsin ();'],
  99.     [" brs ", "Добавить [br] к переводам строк", 'brs (); msg.parentNode.mode.selectedIndex = 0;']
  100. );
  101.  
  102. msg.parentNode.insertBefore (panel, msg);
  103. msg.cols = 100;
  104. msg.rows = 20;
  105.  
  106. // Styles
  107. obj = document.createElement ("style");
  108. obj.innerHTML = '\
  109.     #atag a {\
  110.         padding:2px 3px; margin:2px; cursor: pointer;\
  111.         text-decoration: none; color: #FFF !important;\
  112.         background-color:#004; border: #888 outset 1px;\
  113.     }\
  114.     #atag a:hover {background-color:#008; border-color:#888;}\
  115.     #atag {\
  116.         margin-top: 5px; margin-bottom: 5px;\
  117.         padding: 3px 1px; font-size: 0.9em;\
  118.     }\
  119.     #atag > span {margin-right: 4px;}\
  120.     label[for="msg"] {display: inline-block; margin-top: 5px;}\
  121.     #msg {width: 50em !important;}\
  122.     label[for="title"], label[for="form_mode"] {display: inline-block; margin: 5px 0 3px 0;\
  123.     .msg_body p {margin: 0.3em 0 !important;}\
  124.     .quote > p {margin: 0.5em 0 0.3em 0 !important;}\
  125. ';
  126. document.getElementsByTagName ("head")[0].appendChild (obj);
  127.  
  128. // Remove formating tips
  129. if (u.indexOf ("add.jsp") <= -1 &&
  130.         u.indexOf ("edit") <= -1)
  131.     removeElements (form.getElementsByTagName ('font')[0],
  132.         (i = form.getElementsByTagName ('br'))[5],
  133.         i[6]
  134.     );
  135.  
  136. // Add quote links
  137. var t = new Array ();
  138. var d = document.getElementsByClassName ("title");
  139. for (i = 0; i < d.length; i++)
  140.     if (d[i].parentNode.className == "msg")
  141.         t.push (d[i]);
  142.    
  143. t.createQlink = function () {
  144.     for (i = 0; i < this.length; i++) {
  145.         for (j in arguments) {
  146.             var qlink = document.createElement ("a");
  147.             qlink.onclick = arguments[j][1];
  148.             qlink.href = u;
  149.             qlink.textContent = arguments[j][0];
  150.             this[i].insertBefore (
  151.                 document.createTextNode ("] "),
  152.                 this[i].firstChild
  153.             );
  154.             this[i].insertBefore (
  155.                 document.createTextNode ("["),
  156.                 this[i].insertBefore (qlink, this[i].firstChild)
  157.             );
  158.         }
  159.     }
  160. }
  161. t.createQlink (['цитата', q], ['блок-цитата', qb], ['юзер', user]);
  162.  
  163. // Add \n to <br>
  164. var mbs = document.getElementsByClassName ("msg_body");
  165. for (j in mbs) if (!isNaN (j)) {
  166.     var mps = mbs[j].getElementsByTagName ("p");
  167.     for (i in mps)
  168.         if (!isNaN (i))
  169.             mps[i].innerHTML = mps[i].innerHTML.replace (/<br\/?>(?![\n\r])/g, "<br>\n");
  170. }
  171.  
  172. /* ==========
  173.     Main
  174. ========== */
  175.  
  176. // Auxiliary functions
  177. function addbr (c) {
  178.     return c.replace (/^((?:(?!\[\/?(?:quote|code|list|br)(?:=.*)?\]$)[^\n\r])+)(\r?\n)(?!\n|\[\/?(?:br|quote(?:=.*)?|code(?:=.*)?)\])/gm, "$1[br]$2");
  179. }
  180. function getTextContent (post) {
  181.     var text = "";
  182.     var pTags = post.getElementsByClassName ("msg_body")[0].getElementsByTagName ("p");
  183.     for (i = 0; i < pTags.length; i++)
  184.         if (
  185.             pTags[i].parentNode.className.indexOf ('msg_body') > -1
  186.             || pTags[i].parentNode.getAttribute ('itemprop') == "articleBody"
  187.         ) {
  188.             text += pTags[i].textContent;
  189.             if (i != pTags.length - 1) text += "\n\n";
  190.         }
  191.     return text;
  192. }
  193. function getUserName (post){
  194.     if (i = post.getElementsByClassName ("sign")[0].getElementsByTagName ("a")[0])
  195.         return i.innerHTML;
  196.     else return "anonymous";
  197. }
  198.  
  199. // Functions to run
  200. function intag (tag, arg) {
  201.     var arg = arg || "";
  202.     msg.wrtSel (
  203.         undefined,
  204.         tag.length + arg.length + 2,
  205.         "[" + tag + arg + "]",
  206.         "[/" + tag + "]"
  207.     );
  208. }
  209. function fix () {
  210.     var a = msg.selectionStart, b = msg.selectionEnd;
  211.     var repc = function (c) {
  212.         c = c.replace (/\(c\)/gi, "©");    c = c.replace (/\([rр]\)/gi, "®");
  213.         c = c.replace (/\(f\)/gi, "£");    c = c.replace (/\(e\)/gi, "€");
  214.         c = c.replace (/%\/10/g, "‰");    c = c.replace (/%\/100/g, "‱");
  215.         c = c.replace (/\(V\)/g, "✓");    c = c.replace (/\(V\+\)/g, "✔");
  216.         c = c.replace (/\(x\)/g, "✗");    c = c.replace (/\(x\+\)/g, "✘");
  217.         c = c.replace (/`/g, "&#769;"); c = c.replace (/\(p\)/gi, "§");
  218.         c = c.replace (/(^| )- /g, "$1— ");   c = c.replace (/-->/g, "→");
  219.         c = c.replace (/\(\*\+?\)/g, "★");    c = c.replace (/\(\*-\)/g, "☆");
  220.         c = c.replace (/\([tт][mм]\)/gi, "™");
  221.         return c;
  222.     }
  223.    
  224.     if (a != b) {
  225.         var c = msg.value.substring (a, b);
  226.         var z = repc (c);
  227.         msg.wrtSel (z, 0, "", "", z.length - c.length);
  228.     }
  229.     else
  230.         msg.value = repc (msg.value);
  231. }
  232. function url () {
  233.     a = msg.selectionStart; b = msg.selectionEnd;
  234.     z = msg.value.substring (a, b);
  235.     if (/((ftp|http|https):\/\/)[\.\w- ]{2,}\.[A-Za-z]{2,4}(\/?$|\/.*)/.test(z) || z.length == 0) {
  236.         msg.wrtSel (z, 5,
  237.             "[url]", "[/url]"
  238.         );
  239.     }
  240.     else if (/[\.\w- ]{2,}\.[A-Za-z]{2,4}(\/?$|\/.*)/.test(z)) {
  241.         msg.wrtSel (
  242.             "http://"+z, 5,
  243.             "[url]", "[/url]", 7
  244.         );
  245.     }
  246.     else {
  247.         msg.wrtSel (z, 5,
  248.             "[url=]", "[/url]",
  249.             -z.length
  250.         );
  251.     }
  252. }
  253. function deltagsin () {
  254.     z = msg.value.substring (a = msg.selectionStart, b = msg.selectionEnd);
  255.     c = z.replace (/\[\w+\](.*)\[\/\w+\]/, "$1");
  256.     msg.wrtSel (c, 0, "", "", - z.length + c.length);
  257. }
  258. function brs () {
  259.     var a = msg.selectionStart, b = msg.selectionEnd;
  260.     if (a != b) {
  261.         var c = msg.value.substring (a, b);
  262.         var z = addbr (c);
  263.         msg.wrtSel (z, 0, "", "", z.length - c.length);
  264.     }
  265.     else {
  266.         msg.value = addbr (msg.value);
  267.     }
  268. }
  269. function qb () {
  270.     var seltxt = getSelection ();
  271.     var getQuoteSrc =
  272.         function (sel, post, prnt) {
  273.             return (
  274.                 "[quote" + (
  275.                     prnt.parentNode.parentNode != msg.parentNode.parentNode.parentNode.parentNode.parentNode
  276.                     ? "=" + getUserName (post)
  277.                     : ""
  278.                 ) + "]"
  279.                 + sel.toString ().replace (
  280.                     /(?:>>-----Цитата---->>|^)(.*)<<-----Цитата----<</,
  281.                     function (str, p) {
  282.                         if (p!="") return "[quote]" + p + "[/quote]";
  283.                         else return "";
  284.                     })
  285.                 + "[/quote]");
  286.         }
  287.     if (seltxt != "") {
  288.         var post = seltxt.getRangeAt (0).commonAncestorContainer;
  289.         while (post.className != "msg")
  290.             post = post.parentNode;
  291.         msg.wrtSel (i = addbr (getQuoteSrc (seltxt, post, this)), i.length);
  292.     }
  293.     else {
  294.         var post = this.parentNode.parentNode;
  295.         msg.wrtSel (i = addbr (getQuoteSrc (getTextContent (post), post, this)), i.length);
  296.     }
  297.     msg.parentNode.mode.selectedIndex = 0;
  298.     return false;
  299. }
  300. function q () {
  301.     var seltxt = getSelection ();
  302.     if (seltxt != "") {
  303.         var post = seltxt.getRangeAt (0).commonAncestorContainer;
  304.         while (post.className != "msg")
  305.             post = post.parentNode;
  306.         msg.wrtSel (i = seltxt.toString ().replace (/(\n\r?|^)(?:\n\r?)?/g, "$1> ") + "\r\n", i.length);
  307.     }
  308.     else {
  309.         post = this.parentNode.parentNode;
  310.         msg.wrtSel (i = getTextContent (post).replace (/(\n\r?|^)(?:\n\r?)?/g, "$1> ")  + "\r\n", i.length);
  311.     }
  312.     msg.parentNode.mode.selectedIndex = 1;
  313.     return false;
  314. }
  315. function user () {
  316.     if ((i = getUserName (this.parentNode.parentNode)) != "anonymous")
  317.         msg.wrtSel (i = "[user]" + i + "[/user], ", i.length);
  318.     else msg.wrtSel (i = "[strong]Михаил[/strong], ", i.length);
  319.     return false;
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement