Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name LOR TreeView
- // @description Make tree view for LOR threads (with jQuery)
- // @exclude http://*linux.org.ru/forum/*/
- // @exclude http://*linux.org.ru/news/*/
- // @exclude http://*linux.org.ru/gallery/*/
- // @exclude http://*linux.org.ru/polls/*/
- // @include http://*linux.org.ru/forum/*/*
- // @include http://*linux.org.ru/news/*/*
- // @include http://*linux.org.ru/gallery/*/*
- // @include http://*linux.org.ru/polls/*/*
- // @exclude https://*linux.org.ru/forum/*/
- // @exclude https://*linux.org.ru/news/*/
- // @exclude https://*linux.org.ru/gallery/*/
- // @exclude https://*linux.org.ru/polls/*/
- // @include https://*linux.org.ru/forum/*/*
- // @include https://*linux.org.ru/news/*/*
- // @include https://*linux.org.ru/gallery/*/*
- // @include https://*linux.org.ru/polls/*/*
- // ==/UserScript==
- //
- // License: GPL
- // Author: sdio ( http://www.linux.org.ru/whois.jsp?nick=sdio )
- // Version: 6.7
- var LINK2IMG = 0;
- var STARTTREE = 0;
- var THEME;
- // set the default theme in case autodetect fail
- THEME = 'tango';
- //THEME = 'white'
- //THEME = 'black';
- //----------------------------------------------------------------
- var COLOR = new Array;
- COLOR['white'] = 'black';
- COLOR['white2'] = 'black';
- COLOR['black'] = 'white';
- COLOR['tango'] = 'white';
- COLOR['swamp'] = 'black';
- // Length of the BACKGROUNDS array may be any, so change it as you want
- var BACKGROUNDS = new Array;
- BACKGROUNDS['white'] = ['#ccf', '#ffc', '#cfc', '#fcc', '#cff', '#fcf', '#ccc'];
- BACKGROUNDS['white2'] = ['#ccf', '#ffc', '#cfc', '#fcc', '#cff', '#fcf', '#ccc'];
- 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'];
- BACKGROUNDS['tango'] = ['#452830','#284542','#283145','#452845'];
- BACKGROUNDS['swamp'] = ['#C0C3B1', '#B0B8A1', '#A0A895','#909580'];
- // Indentation per reply level
- const INDENT = '20px';
- const DAYS = 3;
- const COOKIENAME = "TreeView";
- var options = {
- // post-submit callback
- cache: false
- };
- var jq;
- var st;
- if (typeof(GM_log) == 'function') {
- // For FF, Mozilla (with greasemonkey sandbox)
- jq = unsafeWindow.$;
- st = unsafeWindow.setTimeout;
- unsafeWindow.jump = function(link) {
- return 1;
- }
- unsafeWindow.image_onload = function(elem){
- var width = elem.naturalWidth;
- if (width < 320) {
- elem.style.width = '';
- }
- }
- unsafeWindow.onbeforeunload = null;
- } else {
- // For Epiphany, Opera
- jq = $;
- st = setTimeout;
- function jump(link) {
- return 1;
- }
- function image_onload(elem){
- var width = elem.naturalWidth;
- if (width < 320) {
- elem.style.width = '';
- }
- }
- window.onbeforeunload = null;
- }
- var msgs = -1;
- //var thread_id = document.location.href.replace(/^.*\/forum\/[-a-z]*\/(\d+).*$/, "$1");
- var thread_id = jq('div.messages div.msg div.title a').get(0).pathname.split('/').pop();
- var cnt = getCounter(thread_id, 0);
- var newid = new Array;
- jq('link').each(function(){
- var found = this.href.match(/\/([^/]*)\/combined\.css/);
- if (found) {
- THEME = found[0].split('/')[1];
- if (THEME == 'tango') {
- var subtheme = readCookie("css");
- if (subtheme == 'swamp') {
- THEME = 'swamp';
- }
- }
- }
- });
- // -----
- jq.fn.sort = function() {
- return this.pushStack([].sort.apply(this, arguments), []);
- };
- // -----
- function createCookie(name,value,days) {
- var expires;
- if (days) {
- var date = new Date();
- date.setTime(date.getTime()+(days*24*60*60*1000));
- expires = "; expires="+date.toGMTString();
- } else {
- expires = "";
- }
- document.cookie = name+"="+value+expires+"; path=/";
- }
- function readCookie(name) {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0; i<ca.length; i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') c = c.substring(1,c.length);
- if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
- }
- return null;
- }
- function jq_get(url, callback) {
- var data = {};
- var param = url.split('?')[1];
- var url = url.split('?')[0];
- if (param) {
- var params = param.split('&');
- for (var i=0; i<params.length; i++) {
- var key = params[i].split('=')[0];
- var val = params[i].split('=')[1];
- switch (key) {
- case 'msgid':
- data[key] = val;
- break;
- case 'page':
- data[key] = val;
- break;
- case 'filter':
- data[key] = val;
- break;
- }
- }
- }
- jq.ajaxSetup({cache: false});
- jq.get(url, data, callback);
- }
- //get from Cookie or GM stored thread's message counter
- function getCounter(msg_id, update) {
- var count = null;
- var str = readCookie(COOKIENAME);
- if (str) {
- var flag = 0;
- var d = new Date();
- var nowtime = d.getTime();
- var newarr = new Array;
- var arr = str.split("_");
- var j;
- var k = 0;
- newarr[k++] = "";
- for (var i=1; i<arr.length; i+=3){
- if (arr[i] == msg_id) {
- // found
- j = i+1;
- count = arr[j];
- } //if
- j = i+2;
- if (arr[j] > nowtime) {
- j=i;
- newarr[k++] = arr[j++];
- newarr[k++] = arr[j++];
- newarr[k++] = arr[j++];
- } else {
- flag = 1; // will purge expired records
- } //if
- } //for
- if (flag) {
- str = newarr.join("_");
- createCookie(COOKIENAME, str, DAYS);
- }
- } //if(str)
- if (!count) {
- if (update) {
- count = jq("div.comment div.msg").length;
- } else {
- count = 0;
- }
- }
- return count;
- }
- function setCounter(msg_id, value) {
- var d = new Date();
- var expireat = d.getTime();
- expireat += DAYS * 24 * 60 * 60 * 1000;
- var flag = 0;
- var str = readCookie(COOKIENAME);
- if (str) {
- var arr = str.split("_");
- for (var i=1; i<arr.length; i+=3){
- if (arr[i] == msg_id) {
- // found
- arr[++i] = value;
- arr[++i] = expireat;
- flag = 1;
- break;
- }
- }
- } else {
- str = "";
- }
- if (flag) {
- // updated
- str = arr.join("_");
- } else {
- // new one
- str = str + "_" + msg_id + "_" + value + "_" + expireat;
- }
- createCookie(COOKIENAME, str, DAYS);
- }
- //
- // callback function on UPDATE action
- //
- function link2image(index) {
- if (this.href.match(/\.(jpe?g|png|gif)$/i)) {
- this.innerHTML = '<hr><img onload="image_onload(this)" src="' + this.href + '" alt="' + this.href + '" />';
- var img = this.getElementsByTagName('img');
- img[0].style.width = '320px';
- }
- }
- // ---------------------------------------------------------------------
- function doindent(index) {
- this.setAttribute("treelevel", "0"); // initial indent level
- // this.style.paddingBottom = "1px"; // style
- // this.style.marginBottom = "4px"; // style
- var root;
- // store new id in array
- if ((msgs+index+1) >= cnt) {
- newid.push(this.id);
- }
- root = jq("div.comment").get(0);
- // remove subject
- // jq("h2", this).html('<hr>');
- // do all links colored black
- if (THEME != 'tango' || THEME != 'swamp') {
- jq("a", this).css('cssText', 'color: ' + COLOR[THEME] + ' !important');
- }
- jq(".sign", this).css('cssText', 'text-align: left');
- if (LINK2IMG) {
- jq("div.msg_body a", this).each(link2image);
- }
- // is a message answer to other (non root) message?
- var a = jq("div.title", this).find('a[href*="#comment-"]').get(0);
- if (a) {
- // #Id of reply message <DIV>
- var idr = a.hash.split('#')[1];
- if (idr) {
- // "parent" message
- var idr_msg = document.getElementById(idr);
- if (idr_msg) {
- // child's indent level
- var idr_level = idr_msg.getAttribute("treelevel");
- idr_level++;
- // indent child's message <DIV>
- // this.style.marginLeft = INDENT + ' !important';
- // save child's indent level
- this.setAttribute("treelevel", idr_level);
- // move child to parent
- idr_msg.appendChild(this);
- // choose color accordingly to indent level
- var bgcolor = BACKGROUNDS[THEME][idr_level % BACKGROUNDS[THEME].length] + ' !important';
- // set background color to .title and .body
- // jq('*', this).css("background-color", bgcolor);
- 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");
- //jq('div#' + this.id).css("background-color", bgcolor);
- //jq('div#' + this.id).css("padding-bottom", "0.5em");
- } else {
- if (msgs > -1) {
- root.appendChild(this);
- }
- }
- } else {
- if (msgs > -1) {
- root.appendChild(this);
- }
- }
- } else {
- if (msgs > -1) {
- root.appendChild(this);
- }
- }
- }
- // ---------------------------------------------------------------------
- // do Indent
- function makeTree() {
- msgs = -1;
- jq(".updatepage,._new_").remove();
- jq("div.msg").each(doindent);
- idx = newid.shift(); // remove topic
- addNavigateLinks(newid);
- newid.length = 0;
- // set message counter
- msgs = jq('div.comment div.msg').length;
- setCounter(thread_id, msgs);
- }
- var butText;
- if (STARTTREE) {
- makeTree();
- butText = 'Plain layout';
- } else {
- butText = 'Tree layout';
- }
- // make plain/tree layout
- jq('div.messages table.nav tr:eq(0) td:eq(0)').prepend('<input id="blayout" type="button" value="' + butText + '"/>').click(
- function() {
- var but = jq(this).find('#blayout');
- if (but.val() == 'Plain layout') {
- but.val('Tree layout');
- // do plain
- jq('div.msg').sort(function(a, b) {
- return jq(a).find("div.title a:eq(0)").text() - jq(b).find("div.title a:eq(0)").text();
- }).prependTo("div.comment");
- jq('div.comment div.msg, div.comment div.title').css('cssText', '');
- } else {
- but.val('Plain layout');
- // do tree
- makeTree();
- }
- return false
- });
- //var h1subj = jq('div.msg_body h1').get(0);
- //h1subj.innerHTML = '<br><u>' + h1subj.innerHTML + '</u><br><br>';
- var urlhash = document.location.hash;
- if (urlhash) {
- urlhash = urlhash.split('#')[1];
- document.getElementById(urlhash).scrollIntoView();
- }
- // ---------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment