M4veric17 / DJIForumImprove

// @require http://code.jquery.com/jquery-latest.js
// ==UserScript==
// @name         DJIForumImprove
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Improve DJI Forum by turning all last-post-date into links plus giving the abilty to highlight any thread 
// @author       M4veric17
// @license    GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @match        https://forum.dji.com/*
// @grant        none
// ==/UserScript==

var $ = window.jQuery;

$(document).ready(function() {

var a,b,spanTxt,len,myRegexp,match,threadID=[],x=false,curTitleb,h_thread_t,curThreadID,res,curCookie;

function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "") + expires + "; path=/";
}

function getCookie(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 eraseCookie(name) {
    document.cookie = name+'=; Max-Age=-99999999;';
}


var arr = document.getElementsByClassName("h_posttime");
var pageURL = window.location.href

var postDateClick = function(e) {
    if (e.ctrlKey) {
        if (confirm('Are you sure you want to delete all your highlights ?')) {
            eraseCookie('DJIMarkedTids')
            window.location.reload(true);
        }
    } else {
        if (pageURL.search("mod=viewthread")!==-1 || pageURL.match(/thread-(\d*)-\d-1\.html/gm)) {
            var idx=$(arr).index(this)
            if (idx==0) {
               h_thread_t = document.getElementsByClassName("thread_subject xst")[0]
               if (h_thread_t.style.backgroundColor!="lemonchiffon") {
                   h_thread_t.style.backgroundColor="lemonchiffon";
               } else {
                    h_thread_t.style.backgroundColor="";
               }
            }
        } else {
            h_thread_t = this.parentElement.parentElement.firstElementChild
            if (h_thread_t.style.backgroundColor!="lemonchiffon") {
                h_thread_t.style.backgroundColor="lemonchiffon";
            } else {
                h_thread_t.style.backgroundColor="";
            }
        }


        if (pageURL.search("mod=viewthread")!==-1 || pageURL.match(/thread-(\d*)-\d-1\.html/gm)) {
            if (idx==0) {
               curThreadID = pageURL;
                if (curThreadID.match(/-\d-1\.html/g)) {
                    myRegexp = /thread-(\d*)-\d-1\.html/gm;
                    match = myRegexp.exec(curThreadID);
                    curThreadID = match[1];
                } else {
                    myRegexp = /tid=(\d*)\&/gm;
                    match = myRegexp.exec(curThreadID);
                    curThreadID = match[1];
                }
            }
        } else {
            curThreadID = h_thread_t.innerHTML;
            if (curThreadID.match(/-\d-1\.html/gm)) {
                myRegexp = /thread-(\d*)-\d-1\.html/gm;
                match = myRegexp.exec(curThreadID);
                curThreadID = match[1];
            } else {
                myRegexp = /tid=(\d*)\&amp;extra/gm;
                match = myRegexp.exec(curThreadID);
                curThreadID = match[1];
            }
        }

        var curCookie = getCookie('DJIMarkedTids');

        if (curCookie) {
            var res = curCookie.search(curThreadID)
            if (res == -1){
                setCookie('DJIMarkedTids',curCookie + curThreadID + ",",3999);
            } else {
                setCookie('DJIMarkedTids',curCookie.replace(curThreadID + ",",""),3999);
            }
        } else {
            setCookie('DJIMarkedTids',curThreadID + ",",3999);
        }
    }
};

for (var k=0; k<arr.length; k++) {
   arr[k].addEventListener("click",postDateClick,false);
}

	if (pageURL.search("mod=viewthread")!==-1 || pageURL.search("mod=redirect")!==-1 || pageURL.match(/thread-(\d*)-\d-1\.html/gm)) {
		   curThreadID = pageURL;
			if (curThreadID.match(/-\d-1\.html/gm)) {
				myRegexp = /thread-(\d*)-\d-1\.html/gm;
				match = myRegexp.exec(curThreadID);
				curThreadID = match[1];
			} else {
				myRegexp = /tid=(\d*)\&/gm;
				match = myRegexp.exec(curThreadID);
				curThreadID = match[1];
			}
		curCookie = getCookie('DJIMarkedTids');
		if (curCookie) {
			res= curCookie.search(curThreadID)
			if (res!==-1){
				document.getElementsByClassName("thread_subject xst")[0].style.backgroundColor="lemonchiffon";
			}
		}
	}

    a = document.querySelectorAll(".h_thread_v span:nth-of-type(5)");
    len = a.length;

for (var i=0; i <= len - 1; i++) {

    spanTxt = a[i].textContent;
    x = a[i].innerHTML.match('font color="red"');
    b = a[i].parentElement.parentElement.firstElementChild.innerHTML;
    if (b.match(/-\d-1\.html/gm)) {
       myRegexp = /thread-(\d*)-\d-1\.html/gm;
       match = myRegexp.exec(b);
       threadID[i] = match[1];
    } else {
       myRegexp = /tid=(\d*)\&amp;extra/gm;
       match = myRegexp.exec(b);
       threadID[i] = match[1];
    }

    curCookie = getCookie('DJIMarkedTids');
    if (curCookie) {
        res= curCookie.search(threadID[i])
        if (res!==-1){
          a[i].parentElement.parentElement.firstElementChild.style.backgroundColor="lemonchiffon";
        }
  }


    if (x) {
        a[i].innerHTML = '<a href="https://forum.dji.com/forum.php?mod=redirect&tid=' + threadID[i]+ '&goto=lastpost#lastpost" style="color:red">' + spanTxt + ' </a>';
    } else {
        a[i].innerHTML = '<a href="https://forum.dji.com/forum.php?mod=redirect&tid=' + threadID[i]+ '&goto=lastpost#lastpost">' + spanTxt + ' </a>';
    }
}

});