Show48 Local Watched Indicator

Add Watched indicator to show48.com

Versión del día 01/06/2015. Echa un vistazo a la versión más reciente.

// ==UserScript==
// @name         Show48 Local Watched Indicator
// @namespace    http://sk3dsu-phantasy.com/
// @version      0.5
// @description  Add Watched indicator to show48.com
// @author       sk3dsu (Fadli)
// @match        *://*.show48.com/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js
// @grant        GM_getValue 
// @grant        GM_setValue
// @grant        GM_deleteValue
// ==/UserScript==

function GM_main ($) {
    
    // Video Page itself
    if( $('#video-toolbar').length )
    {        
        if ($(".video-toolbar-item-like-bar")[0])
        {
            var urlPath  = window.location.pathname;
            urlPath = urlPath.slice(1);
            urlPath = urlPath.slice(0, -1);
            
            var theValue = GM_getValue(urlPath, 0);

            var newDiv = document.createElement("div"); 
            newDiv.className = "like-dislike pull-left";
            
            var spanItem = createPawWatchSpanElement(false);
            spanItem.urlPath = urlPath;
            spanItem.addEventListener('click', pawClickEventListener, false);
            
            if (theValue)
            {
                spanItem.style.color = "green";
            }
            
            newDiv.appendChild(spanItem);
            
            var parentDiv = document.getElementsByClassName("video-toolbar-item-like-bar")[0]; 
            var currentDiv = parentDiv.children[0];
            currentDiv.appendChild (newDiv);

        }
    }
    // Landing Page Latest Video Area
    else if ($('.smart-box').length)
    {
            var firstSmartBox = $('.smart-box')[0];
        
            // get the second child, not the header child
            var latestVideoDiv = firstSmartBox.childNodes[1];

            var videoItems = latestVideoDiv.getElementsByClassName("video-item");
            
            for (i = 0; i < videoItems.length; i++)
            {
            
                var videoItem = videoItems[i];

                var itemThumbnail = videoItem.firstChild.nextSibling;
                var itemHref = itemThumbnail.childNodes[1];
                var urlPath = getUrlPath(itemHref);
                var theValue = GM_getValue(urlPath, 0);

                if (theValue)
                {
                    var itemOverlay = itemHref.childNodes[1];

                    var spanItem = createPawWatchSpanElement(true);

                    itemOverlay.appendChild(spanItem);
                }

            }
        
    } 
    // Any Page with video-listing and thumbnail e.g. show, drama
    else if ($('.video-listing').length)
    {                 
        var itemThumbnails = $(".item-thumbnail"); 

        for (i = 0; i < itemThumbnails.length; i++)
        {
            var itemHref = itemThumbnails[i].childNodes[1];               
            var urlPath = getUrlPath(itemHref);
            var theValue = GM_getValue(urlPath, 0);

            if (theValue)
            {
                var itemOverlay = itemHref.childNodes[1].nextElementSibling;

                var spanItem = createPawWatchSpanElement(true);

                itemOverlay.appendChild(spanItem);
            }
        }
    }
    // Pages with table
    else if ($('.tablepress').length)
    { 
        var table = $('.tablepress')[0];
        
        var totalCell = table.rows[0].cells.length;
        var lastColumn= table.rows[0].cells[totalCell - 1];
        
        // Only interested when header has 'Stream' column
        if (lastColumn.innerHTML == "Stream")
        {     
            var tblHead = table.tHead;
            var tblBody = table.tBodies[0];
            
            // Add new column to Table Header
            var newTh = document.createElement('th');
            newTh.className = "column-4 sorting_disabled";
            newTh.innerHTML = "Watched";
            tblHead.rows[0].appendChild(newTh);
            
            for (var i = 0; i < tblBody.rows.length; i++) 
            {   
                var newCell = tblBody.rows[i].insertCell(-1);
                var spanItem = createPawWatchSpanElement(true);
                spanItem.style.fontSize = "150%";
                spanItem.style.color="grey";
                
                
                lastColumn = tblBody.rows[i].cells[totalCell-1].childNodes[0];
                
                if (typeof lastColumn === 'object')
                {    
                    // last column has a link object?
                    if (lastColumn.tagName == 'A')
                    {
                        var itemHref = tblBody.rows[i].cells[totalCell-1].childNodes[0];
                        spanItem.urlPath = getUrlPath(itemHref);
                        spanItem.addEventListener('click', pawClickEventListener, false);
                        
                        var theValue = GM_getValue(spanItem.urlPath, 0);
                        
                        if (theValue) 
                        {
                            spanItem.style.color="green";
                        }

                    }
                }

                newCell.appendChild(spanItem);
                
            }
        }
        
    }
    // Pages with column-inner
    else if ($('.column-inner ').length)
    {        
        var columns = $(".column-inner"); 

        for (i = 0; i < columns.length; i++)
        {
            var itemHref = columns[i].childNodes[0];
            
            var spanItem = createPawWatchSpanElement(true);
            spanItem.style.fontSize = "100%";
            spanItem.style.color = "grey";
            spanItem.style.width = "20px";
            spanItem.urlPath = getUrlPath(itemHref);
            spanItem.addEventListener('click', pawClickEventListener, false);

            var theValue = GM_getValue(spanItem.urlPath, 0);

            if (theValue)
            {
                spanItem.style.color="green";
            }
            
            columns[i].insertBefore(spanItem, columns[i].childNodes[0]);
        }
    }
            
}

function createPawWatchSpanElement(iconOnly) {
    
    var span = document.createElement("span");
    span.id = "pawWatch";
    span.className = "fa fa-paw";
    span.style.opacity = "0.6";
    
    if (iconOnly)
    {
        span.style.color = "green";
        span.style.fontSize = "200%";
    }
    else
    {
        span.style.color = "grey";
        span.style.fontSize = "100%";
        span.innerHTML = " Watched";
    }
    
    return span;
}

function getUrlPath(itemHref) {
    var withoutProtocol = itemHref.href.slice(7);
    var urlExplode = withoutProtocol.split("/");
    var urlPath = urlExplode[1];
    
    return urlPath;
}

function pawClickEventListener(evt) {
    
    var theValue = GM_getValue(evt.target.urlPath, 0);
    
    if (theValue)
    {
        evt.target.style.color = "grey";
        GM_deleteValue(evt.target.urlPath);
    }
    else
    {
        evt.target.style.color = "green";
        GM_setValue(evt.target.urlPath, 1);
    }
}

if (typeof jQuery === "function") {
    console.log ("Running with local copy of jQuery!");
    GM_main (jQuery);
}
else {
    console.log ("fetching jQuery from some 3rd-party server.");
    add_jQuery (GM_main, "1.11.2");
}

function add_jQuery (callbackFn, jqVersion) {
    var jqVersion   = jqVersion || "1.11.2";
    var D           = document;
    var targ        = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    var scriptNode  = D.createElement ('script');
    scriptNode.src  = 'http://ajax.googleapis.com/ajax/libs/jquery/'
                    + jqVersion
                    + '/jquery.min.js'
                    ;
    scriptNode.addEventListener ("load", function () {
        var scriptNode          = D.createElement ("script");
        scriptNode.textContent  =
            'var gm_jQuery  = jQuery.noConflict (true);\n'
            + '(' + callbackFn.toString () + ')(gm_jQuery);'
        ;
        targ.appendChild (scriptNode);
    }, false);
    targ.appendChild (scriptNode);
}