Guest User

LOR TreeView

a guest
Jan 7th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          LOR TreeView
  3. // @description   Make tree view for LOR threads (with jQuery)
  4. // @exclude http://*linux.org.ru/forum/*/
  5. // @exclude http://*linux.org.ru/news/*/
  6. // @exclude http://*linux.org.ru/gallery/*/
  7. // @exclude http://*linux.org.ru/polls/*/
  8. // @include http://*linux.org.ru/forum/*/*
  9. // @include http://*linux.org.ru/news/*/*
  10. // @include http://*linux.org.ru/gallery/*/*
  11. // @include http://*linux.org.ru/polls/*/*
  12.  
  13. // @exclude https://*linux.org.ru/forum/*/
  14. // @exclude https://*linux.org.ru/news/*/
  15. // @exclude https://*linux.org.ru/gallery/*/
  16. // @exclude https://*linux.org.ru/polls/*/
  17. // @include https://*linux.org.ru/forum/*/*
  18. // @include https://*linux.org.ru/news/*/*
  19. // @include https://*linux.org.ru/gallery/*/*
  20. // @include https://*linux.org.ru/polls/*/*
  21. // ==/UserScript==
  22. //
  23. // License: GPL
  24. // Author:  sdio ( http://www.linux.org.ru/whois.jsp?nick=sdio )
  25. // Version: 6.7
  26.  
  27.  
  28. var LINK2IMG = 0;
  29. var STARTTREE = 0;
  30.  
  31. var THEME;
  32. // set the default theme in case autodetect fail
  33. THEME = 'tango';
  34. //THEME = 'white'
  35. //THEME = 'black';
  36.  
  37.  
  38.  
  39. //----------------------------------------------------------------
  40. var COLOR = new Array;
  41. COLOR['white']  = 'black';
  42. COLOR['white2'] = 'black';
  43. COLOR['black']  = 'white';
  44. COLOR['tango']  = 'white';
  45. COLOR['swamp']  = 'black';
  46.  
  47. // Length of the BACKGROUNDS array may be any, so change it as you want
  48. var BACKGROUNDS = new Array;
  49. BACKGROUNDS['white']  = ['#ccf', '#ffc', '#cfc', '#fcc', '#cff', '#fcf', '#ccc'];
  50. BACKGROUNDS['white2'] = ['#ccf', '#ffc', '#cfc', '#fcc', '#cff', '#fcf', '#ccc'];
  51. BACKGROUNDS['black']  = ['#004', '#000048', '#000052', '#000056', '#000060', '#000064', '#000068', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072', '#000072'];
  52. BACKGROUNDS['tango']  = ['#452830','#284542','#283145','#452845'];
  53. BACKGROUNDS['swamp']  = ['#C0C3B1', '#B0B8A1', '#A0A895','#909580'];
  54.  
  55. // Indentation per reply level
  56. const INDENT = '20px';
  57.  
  58.  
  59. const DAYS = 3;
  60. const COOKIENAME = "TreeView";
  61.  
  62.  
  63. var options = {
  64.     // post-submit callback
  65.     cache: false
  66. };
  67.  
  68.  
  69.  
  70.  
  71. var jq;
  72. var st;
  73.  
  74. if (typeof(GM_log) == 'function') {
  75.     // For FF, Mozilla (with greasemonkey sandbox)
  76.     jq = unsafeWindow.$;
  77.     st = unsafeWindow.setTimeout;
  78.  
  79.     unsafeWindow.jump = function(link) {
  80.         return 1;
  81.     }
  82.  
  83.     unsafeWindow.image_onload = function(elem){
  84.         var width = elem.naturalWidth;
  85.         if (width < 320) {
  86.             elem.style.width = '';
  87.         }
  88.     }
  89.         unsafeWindow.onbeforeunload = null;
  90.  
  91.  
  92. } else {
  93.     // For Epiphany, Opera
  94.     jq = $;
  95.     st = setTimeout;
  96.  
  97.     function jump(link) {
  98.         return 1;
  99.     }
  100.     function image_onload(elem){
  101.         var width = elem.naturalWidth;
  102.         if (width < 320) {
  103.             elem.style.width = '';
  104.         }
  105.     }
  106.         window.onbeforeunload = null;
  107. }
  108.  
  109. var msgs      = -1;
  110. //var thread_id = document.location.href.replace(/^.*\/forum\/[-a-z]*\/(\d+).*$/, "$1");
  111. var thread_id = jq('div.messages div.msg div.title a').get(0).pathname.split('/').pop();
  112. var cnt       = getCounter(thread_id, 0);
  113. var newid     = new Array;
  114.  
  115.  
  116. jq('link').each(function(){
  117.     var found = this.href.match(/\/([^/]*)\/combined\.css/);
  118.     if (found) {
  119.         THEME = found[0].split('/')[1];
  120.         if (THEME == 'tango') {
  121.             var subtheme = readCookie("css");
  122.             if (subtheme == 'swamp') {
  123.                 THEME = 'swamp';
  124.             }
  125.         }
  126.     }
  127. });
  128.  
  129. // -----
  130. jq.fn.sort = function() {
  131.   return this.pushStack([].sort.apply(this, arguments), []);
  132. };
  133. // -----
  134.  
  135. function createCookie(name,value,days) {
  136.     var expires;
  137.     if (days) {
  138.         var date = new Date();
  139.         date.setTime(date.getTime()+(days*24*60*60*1000));
  140.         expires = "; expires="+date.toGMTString();
  141.     } else {
  142.         expires = "";
  143.     }
  144.     document.cookie = name+"="+value+expires+"; path=/";
  145. }
  146.  
  147. function readCookie(name) {
  148.        var nameEQ = name + "=";
  149.        var ca = document.cookie.split(';');
  150.        for(var i=0; i<ca.length; i++) {
  151.                var c = ca[i];
  152.                while (c.charAt(0)==' ') c = c.substring(1,c.length);
  153.                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  154.        }
  155.        return null;
  156. }
  157.  
  158. function jq_get(url, callback) {
  159.     var data = {};
  160.     var param = url.split('?')[1];
  161.     var url = url.split('?')[0];
  162.     if (param) {
  163.         var params = param.split('&');
  164.         for (var i=0; i<params.length; i++) {
  165.             var key = params[i].split('=')[0];
  166.             var val = params[i].split('=')[1];
  167.             switch (key) {
  168.             case 'msgid':
  169.               data[key] = val;
  170.               break;
  171.             case 'page':
  172.               data[key] = val;
  173.               break;
  174.             case 'filter':
  175.               data[key] = val;
  176.               break;
  177.             }
  178.         }
  179.     }
  180.     jq.ajaxSetup({cache: false});
  181.     jq.get(url, data, callback);
  182. }
  183.  
  184. //get from Cookie or GM stored thread's message counter
  185. function getCounter(msg_id, update) {
  186.     var count = null;
  187.     var str = readCookie(COOKIENAME);
  188.     if (str) {
  189.         var flag = 0;
  190.         var d = new Date();
  191.         var nowtime = d.getTime();
  192.         var newarr = new Array;
  193.         var arr = str.split("_");
  194.         var j;
  195.         var k = 0;
  196.         newarr[k++] = "";
  197.         for (var i=1; i<arr.length; i+=3){
  198.             if (arr[i] == msg_id) {
  199.                 // found
  200.                 j = i+1;
  201.                 count = arr[j];
  202.             } //if
  203.             j = i+2;
  204.             if (arr[j] > nowtime) {
  205.                 j=i;
  206.                 newarr[k++] = arr[j++];
  207.                 newarr[k++] = arr[j++];
  208.                 newarr[k++] = arr[j++];
  209.             } else {
  210.                 flag = 1; // will purge expired records
  211.             } //if
  212.  
  213.         } //for
  214.         if (flag) {
  215.             str = newarr.join("_");
  216.             createCookie(COOKIENAME, str, DAYS);
  217.         }
  218.     } //if(str)
  219.     if (!count) {
  220.         if (update) {
  221.             count = jq("div.comment div.msg").length;
  222.         } else {
  223.             count = 0;
  224.         }
  225.     }
  226.     return count;
  227. }
  228.  
  229. function setCounter(msg_id, value) {
  230.     var d = new Date();
  231.     var expireat = d.getTime();
  232.     expireat += DAYS * 24 * 60 * 60 * 1000;
  233.  
  234.     var flag = 0;
  235.     var str = readCookie(COOKIENAME);
  236.     if (str) {
  237.         var arr = str.split("_");
  238.         for (var i=1; i<arr.length; i+=3){
  239.             if (arr[i] == msg_id) {
  240.                 // found
  241.                 arr[++i] = value;
  242.                 arr[++i] = expireat;
  243.                 flag = 1;
  244.                 break;
  245.             }  
  246.         }
  247.     } else {
  248.         str = "";
  249.     }
  250.  
  251.     if (flag) {
  252.         // updated
  253.         str = arr.join("_");
  254.     } else {    
  255.         // new one
  256.         str = str + "_" + msg_id + "_" + value + "_" + expireat;
  257.     }
  258.     createCookie(COOKIENAME, str, DAYS);
  259. }
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272. //
  273. // callback function on UPDATE action
  274. //
  275.  
  276.  
  277. function link2image(index) {
  278.     if (this.href.match(/\.(jpe?g|png|gif)$/i)) {
  279.         this.innerHTML = '<hr><img onload="image_onload(this)" src="' + this.href + '" alt="' + this.href + '" />';
  280.         var img = this.getElementsByTagName('img');
  281.         img[0].style.width = '320px';
  282.     }
  283. }
  284.  
  285. // ---------------------------------------------------------------------
  286. function doindent(index) {
  287.     this.setAttribute("treelevel", "0");  // initial indent level
  288. //    this.style.paddingBottom = "1px"; // style
  289. //    this.style.marginBottom  = "4px"; // style
  290.     var root;
  291.  
  292.     // store new id in array
  293.     if ((msgs+index+1) >= cnt) {
  294.         newid.push(this.id);
  295.     }
  296.  
  297.     root = jq("div.comment").get(0);
  298.  
  299.     // remove subject
  300. //  jq("h2", this).html('<hr>');
  301.  
  302.     // do all links colored black
  303.     if (THEME != 'tango' || THEME != 'swamp') {
  304.         jq("a", this).css('cssText', 'color: ' + COLOR[THEME] + ' !important');
  305.     }
  306.     jq(".sign", this).css('cssText', 'text-align: left');
  307.  
  308.     if (LINK2IMG) {
  309.         jq("div.msg_body a", this).each(link2image);
  310.     }
  311.    
  312.     // is a message answer to other (non root) message?
  313.     var a = jq("div.title", this).find('a[href*="#comment-"]').get(0);
  314.     if (a) {
  315.         // #Id of reply message <DIV>
  316.         var idr = a.hash.split('#')[1];
  317.         if (idr) {
  318.             // "parent" message
  319.             var idr_msg   = document.getElementById(idr);
  320.             if (idr_msg) {
  321.                 // child's indent level
  322.                 var idr_level = idr_msg.getAttribute("treelevel");
  323.                 idr_level++;
  324.  
  325.                 // indent child's message <DIV>
  326. //                this.style.marginLeft = INDENT + ' !important';
  327.  
  328.                 // save child's indent level
  329.                 this.setAttribute("treelevel", idr_level);
  330.  
  331.                 // move child to parent
  332.                 idr_msg.appendChild(this);
  333.                 // choose color accordingly to indent level
  334.                 var bgcolor = BACKGROUNDS[THEME][idr_level % BACKGROUNDS[THEME].length] + ' !important';
  335.                 // set background color to .title and .body
  336. //              jq('*', this).css("background-color", bgcolor);
  337.                 jq('div#' + this.id + ', div#' + this.id +' div.msg').css('cssText', "margin-right: -1px; margin-bottom: 15px; margin-top: 15px; margin-left: " + INDENT + "; background-color: " + bgcolor + "; border-top: solid 13px black; border-bottom: solid 1px black; border-left: solid 1px black");
  338.                 //jq('div#' + this.id).css("background-color", bgcolor);
  339.                 //jq('div#' + this.id).css("padding-bottom", "0.5em");
  340.  
  341.             } else {
  342.                 if (msgs > -1) {
  343.                     root.appendChild(this);
  344.                 }
  345.             }
  346.         } else {
  347.             if (msgs > -1) {
  348.                 root.appendChild(this);
  349.             }
  350.         }
  351.     } else {
  352.         if (msgs > -1) {
  353.             root.appendChild(this);
  354.         }
  355.     }
  356. }
  357. // ---------------------------------------------------------------------
  358.  
  359. // do Indent
  360. function makeTree() {
  361.     msgs = -1;
  362.     jq(".updatepage,._new_").remove();
  363.  
  364.     jq("div.msg").each(doindent);
  365.     idx = newid.shift(); // remove topic
  366.  
  367.     addNavigateLinks(newid);
  368.     newid.length = 0;
  369.     // set message counter
  370.     msgs = jq('div.comment div.msg').length;
  371.     setCounter(thread_id, msgs);
  372.     }
  373.  
  374. var butText;
  375. if (STARTTREE) {
  376.     makeTree();
  377.     butText = 'Plain layout';
  378. } else {
  379.     butText = 'Tree layout';
  380. }
  381.  
  382.  
  383.  
  384. // make  plain/tree layout
  385.  
  386. jq('div.messages table.nav tr:eq(0) td:eq(0)').prepend('<input id="blayout" type="button" value="' + butText + '"/>').click(
  387.   function() {
  388.        var but = jq(this).find('#blayout');
  389.        if (but.val() == 'Plain layout') {
  390.                but.val('Tree layout');
  391.                // do plain
  392.                jq('div.msg').sort(function(a, b) {
  393.                return jq(a).find("div.title a:eq(0)").text() - jq(b).find("div.title a:eq(0)").text();
  394.                }).prependTo("div.comment");
  395.                jq('div.comment div.msg, div.comment div.title').css('cssText', '');
  396.        } else {
  397.                but.val('Plain layout');
  398.                // do tree
  399.                makeTree();
  400.        }
  401.        return false
  402. });
  403.  
  404. //var h1subj = jq('div.msg_body h1').get(0);
  405. //h1subj.innerHTML = '<br><u>' + h1subj.innerHTML + '</u><br><br>';
  406.  
  407. var urlhash = document.location.hash;
  408. if (urlhash) {
  409.     urlhash = urlhash.split('#')[1];
  410.     document.getElementById(urlhash).scrollIntoView();
  411. }
  412.  
  413.  
  414. // ---------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment