Advertisement
Guest User

Untitled

a guest
Apr 18th, 2013
74
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. msg.wrtSel = function (subj, offset, before, after, zset) { //also msg.wrtSel (before, after, offset)
  178.     if (typeof offset == "string")
  179.         var
  180.             after = offset, offset = before,
  181.             before = subj, subj = undefined;
  182.     var
  183.         before = before || "", after = after || "",
  184.         offset = set (offset, before.length), zset = zset || 0;
  185.     var
  186.         startSel = set (a, this.selectionStart), endSel = set (b, this.selectionEnd),
  187.         subj = before + set (subj, this.value.substring (startSel, endSel)) + after;
  188.  
  189.     this.value = this.value.substring (0, startSel) + subj + this.value.substring (endSel);
  190.     this.focus (); this.setSelectionRange (startSel + offset, endSel + offset + zset);
  191.     a = b = undefined;
  192. }
  193. function addbr (c) {
  194.     return c.replace (/^((?:(?!\[\/?(?:quote|code|list|br)(?:=.*)?\]$)[^\n\r])+)(\r?\n)(?!\n|\[\/?(?:br|quote(?:=.*)?|code(?:=.*)?)\])/gm, "$1[br]$2");
  195. }
  196. function getTextContent (post) {
  197.     var text = "";
  198.     var pTags = post.getElementsByClassName ("msg_body")[0].getElementsByTagName ("p");
  199.     for (i = 0; i < pTags.length; i++)
  200.         if (
  201.             pTags[i].parentNode.className.indexOf ('msg_body') > -1
  202.             || pTags[i].parentNode.getAttribute ('itemprop') == "articleBody"
  203.         ) {
  204.             text += pTags[i].textContent;
  205.             if (i != pTags.length - 1) text += "\n\n";
  206.         }
  207.     return text;
  208. }
  209. function getUserName (post){
  210.     if (i = post.getElementsByClassName ("sign")[0].getElementsByTagName ("a")[0])
  211.         return i.innerHTML;
  212.     else return "anonymous";
  213. }
  214.  
  215. // Functions to run
  216. function intag (tag, arg) {
  217.     var arg = arg || "";
  218.     msg.wrtSel (
  219.         undefined,
  220.         tag.length + arg.length + 2,
  221.         "[" + tag + arg + "]",
  222.         "[/" + tag + "]"
  223.     );
  224. }
  225. function fix () {
  226.     var a = msg.selectionStart, b = msg.selectionEnd;
  227.     var repc = function (c) {
  228.         c = c.replace (/\(c\)/gi, "©");    c = c.replace (/\([rр]\)/gi, "®");
  229.         c = c.replace (/\(f\)/gi, "£");    c = c.replace (/\(e\)/gi, "€");
  230.         c = c.replace (/%\/10/g, "‰");    c = c.replace (/%\/100/g, "‱");
  231.         c = c.replace (/\(V\)/g, "✓");    c = c.replace (/\(V\+\)/g, "✔");
  232.         c = c.replace (/\(x\)/g, "✗");    c = c.replace (/\(x\+\)/g, "✘");
  233.         c = c.replace (/`/g, "&#769;"); c = c.replace (/\(p\)/gi, "§");
  234.         c = c.replace (/(^| )- /g, "$1— ");   c = c.replace (/-->/g, "→");
  235.         c = c.replace (/\(\*\+?\)/g, "★");    c = c.replace (/\(\*-\)/g, "☆");
  236.         c = c.replace (/\([tт][mм]\)/gi, "™");
  237.         return c;
  238.     }
  239.    
  240.     if (a != b) {
  241.         var c = msg.value.substring (a, b);
  242.         var z = repc (c);
  243.         msg.wrtSel (z, 0, "", "", z.length - c.length);
  244.     }
  245.     else
  246.         msg.value = repc (msg.value);
  247. }
  248. function url () {
  249.     a = msg.selectionStart; b = msg.selectionEnd;
  250.     z = msg.value.substring (a, b);
  251.     if (/((ftp|http|https):\/\/)[\.\w- ]{2,}\.[A-Za-z]{2,4}(\/?$|\/.*)/.test(z) || z.length == 0) {
  252.         msg.wrtSel (z, 5,
  253.             "[url]", "[/url]"
  254.         );
  255.     }
  256.     else if (/[\.\w- ]{2,}\.[A-Za-z]{2,4}(\/?$|\/.*)/.test(z)) {
  257.         msg.wrtSel (
  258.             "http://"+z, 5,
  259.             "[url]", "[/url]", 7
  260.         );
  261.     }
  262.     else {
  263.         msg.wrtSel (z, 5,
  264.             "[url=]", "[/url]",
  265.             -z.length
  266.         );
  267.     }
  268. }
  269. function deltagsin () {
  270.     z = msg.value.substring (a = msg.selectionStart, b = msg.selectionEnd);
  271.     c = z.replace (/\[\w+\](.*)\[\/\w+\]/, "$1");
  272.     msg.wrtSel (c, 0, "", "", - z.length + c.length);
  273. }
  274. function brs () {
  275.     var a = msg.selectionStart, b = msg.selectionEnd;
  276.     if (a != b) {
  277.         var c = msg.value.substring (a, b);
  278.         var z = addbr (c);
  279.         msg.wrtSel (z, 0, "", "", z.length - c.length);
  280.     }
  281.     else {
  282.         msg.value = addbr (msg.value);
  283.     }
  284. }
  285. function qb () {
  286.     var seltxt = getSelection ();
  287.     var getQuoteSrc =
  288.         function (sel, post, prnt) {
  289.             return (
  290.                 "[quote" + (
  291.                     prnt.parentNode.parentNode != msg.parentNode.parentNode.parentNode.parentNode.parentNode
  292.                     ? "=" + getUserName (post)
  293.                     : ""
  294.                 ) + "]"
  295.                 + sel.toString ().replace (
  296.                     /(?:>>-----Цитата---->>|^)(.*)<<-----Цитата----<</,
  297.                     function (str, p) {
  298.                         if (p!="") return "[quote]" + p + "[/quote]";
  299.                         else return "";
  300.                     })
  301.                 + "[/quote]");
  302.         }
  303.     if (seltxt != "") {
  304.         var post = seltxt.getRangeAt (0).commonAncestorContainer;
  305.         while (post.className != "msg")
  306.             post = post.parentNode;
  307.         msg.wrtSel (i = addbr (getQuoteSrc (seltxt, post, this)), i.length);
  308.     }
  309.     else {
  310.         var post = this.parentNode.parentNode;
  311.         msg.wrtSel (i = addbr (getQuoteSrc (getTextContent (post), post, this)), i.length);
  312.     }
  313.     msg.parentNode.mode.selectedIndex = 0;
  314.     return false;
  315. }
  316. function q () {
  317.     var seltxt = getSelection ();
  318.     if (seltxt != "") {
  319.         var post = seltxt.getRangeAt (0).commonAncestorContainer;
  320.         while (post.className != "msg")
  321.             post = post.parentNode;
  322.         msg.wrtSel (i = seltxt.toString ().replace (/(\n\r?|^)(?:\n\r?)?/g, "$1> ") + "\r\n", i.length);
  323.     }
  324.     else {
  325.         post = this.parentNode.parentNode;
  326.         msg.wrtSel (i = getTextContent (post).replace (/(\n\r?|^)(?:\n\r?)?/g, "$1> ")  + "\r\n", i.length);
  327.     }
  328.     msg.parentNode.mode.selectedIndex = 1;
  329.     return false;
  330. }
  331. function user () {
  332.     if ((i = getUserName (this.parentNode.parentNode)) != "anonymous")
  333.         msg.wrtSel (i = "[user]" + i + "[/user], ", i.length);
  334.     else msg.wrtSel (i = "[strong]Михаил[/strong], ", i.length);
  335.     return false;
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement