medmedz / Kaskus TS Tagger

// ==UserScript==
// @name           Kaskus TS Tagger
// @namespace      zackad's script
// @version        0.4
// @description    Give TS kaskus a Tag
// @include        http://m.kaskus.co.id/*
// @include        http://fjb.m.kaskus.co.id/*
// @include        /^https?://(www|fjb).kaskus.co.id/(thread|lastpost|post|show_post|product|group/discussion)/*/
// @require        http://code.jquery.com/jquery-1.10.1.min.js
// @copyright      2015, zackad
// ==/UserScript==
/*
CHANGELOG
v0.4
- fjb support
v0.3
- tag color [thanks : ahmad13]
*/
$(document).ready(function(){
    var DEBUG = 0;
    var tID = $('#thread_id');
    tID = tID.attr('value');
//    clog(tID);
    
    //style edit sesuai selera
    var globalStyle = ''
        +'<style type="text/css">'
        +'.thread-starter {background-color:black; color:white!important;}'
        +'.thread-starter .fn, .thread-starter .permalink {color:white!important}'
        +'.thread-starter-desk {border:1px solid sandybrown!important;}'
        +'.thread-starter-desk .entry-head {background-color:darkorange!important;}'
        +'.thread-starter-desk .entry-body {border-left:1px solid sandybrown!important;}'
        +'.thread-starter-desk .entry-footer {border-top:1px solid sandybrown;}'
        +'</style>'
        ;
    //silahkan edit sesuka ente
    var mTS = '<span style="color:darkorange; font-weight:bold;"><b>[TS]</b></span>';
    var dTS = '<span><b style="color:#F5981D;">Thread</b><b style="color:#1998ed;"> Starter</b></span>';
    var juragan = '<span><b style="color:#1998ed;">Juragan</b></span>';
    
    var tsContainer = ''
        +'<div id="ts" class="ts" style="display:none;"></div>'
        ;
    $('body').prepend(tsContainer);
    $('head').append(globalStyle);
    var host = window.location.host;
    if (isFJB())
        var ajaxURL = 'http://'+host+'/product/'+ tID;
    else var ajaxURL = 'http://'+host+'/thread/'+ tID;
    
    //let's call the ajax
    if(window.location.href.indexOf('m.kaskus.co.id') > -1){
        $.ajax({
            url: ajaxURL, //'http://m.kaskus.co.id/thread/' + tID,
            dataType: 'html',
            success: function(response){
                $('.ts').html(jQuery(response).find('.usr .fn').html());
                var a = $('.ts').text();
                clog('ts =' + a);
                var poster = $('.author .fn');
                poster.each(function(){
                    var parent = $(this).parent().parent().parent();
                    var user = $(this);
    //                clog($(this).text());
                    if($(this).text() == a){
                        $(parent).addClass('thread-starter');
                        $(user).after(mTS);
                        }
                    });
                if(poster == a){
                    $('.author').addClass('thread-starter');
                    }
                }
            });
        }
    // desktop version
    $.ajax({
        url: ajaxURL, //'http://'+host+'/thread/' + tID,
        dataType: 'html',
        success: function(response){
            if(isFJB()) $('.ts').html(jQuery(response).find('.seller-info .seller-detail-info .username a').html());
                else $('.ts').html(jQuery(response).find('.postlist .author .user-details .nickname').html());
            var a = $('.ts').text();
            clog('ts desktop ='+ a);
            var poster = $('.postlist .author .user-details .nickname');
//            clog(poster);
            poster.each(function(){
                var parent = $(this).parent().parent().parent().parent().parent();//.parent();//..sUntil('.col-xs-12');
                var user = $(this);//.text();
                var userDetail = $(this).parent().parent();
                if(user.text() == a){
                    $(parent).addClass('thread-starter-desk');
                    $(user).append(mTS);
                    clog(userDetail);
                    if(isFJB()) dTS = juragan;
                    $(userDetail).children('.user-info').before(dTS);
                    }
                });
            }
        });
    function isFJB(){
        if(window.location.host == 'fjb.kaskus.co.id' || window.location.host == 'fjb.m.kaskus.co.id') return true;
        else return false;
    }
    
    function clog(x){
        if(!DEBUG) return;
        console && console.log && console.log(x);
    }
});