SA Buttons

Something Awful Forums functions.

Mint 2017.10.27.. Lásd a legutóbbi verzió

// ==UserScript==
// @name         SA Buttons
// @namespace    http://uberg.nu/
// @version      0.1
// @description  Something Awful Forums functions.
// @author       Ubergnu
// @match        https://forums.somethingawful.com/*
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==


// Some UI values
var BUTTON_TEXT_COLOR = "#444444";
var BUTTON_TEXT_COLOR_ACTIVE = "#DDDDDD";
var BUTTON_BACKGROUND = "";
var BUTTON_BACKGROUND_ACTIVE = "#888888";
var ELEM_VISIBLE = 1;
var ELEM_HIDDEN = 0;
var ELEM_DIMMED = 0.3;

// This is global because GM_getValue() is borked in events
var avatarState;

// Completely hide ignored users posts.
$("a[title=\"DON'T DO IT!!\"]").parent().parent().parent().parent().hide();

// Add Menu buttons
//
$("#content #thread .profilelinks").append("<li class='liStop btnStop'>Stop</li>");
$("#content #thread .profilelinks").append("<li class='liStop btnFold'>Fold</li>");
$("#content #thread .profilelinks").append("<li class='liStop btnFade'>Avs</li>");
$("#content #thread .profilelinks").append("<li class='liStop btnCtrl'>Buts</li>");

// Insert CSS into <head> on page.
//
function addCss(cssString) {
    var head = document.getElementsByTagName('head')[0];
    //return unless head;
    var newCss = document.createElement('style');
    newCss.type = "text/css";
    newCss.innerHTML = cssString;
    head.appendChild(newCss);
}
addCss('#thread ul.profilelinks li:nth-last-of-type(-n+4) { max-height: 15px !important; padding: 0px 13px 10px 13px !important; margin: 5px 0px 10px 0px; line-height: 25px !important; }');

// Menu buttons layout
//
var li = $(".postlinks .liStop");
li.css({
    "border-radius": "11px",
    "position": "relative",
    "font-size": "11px",
    "height": "22px !important",
    "padding": "2px",
    "margin": "0px 0 0px 0 !important;",
    "cursor": "pointer",
    "color": BUTTON_TEXT_COLOR
});


li.hover(
    function(){
        //$(this).css("background-color", BUTTON_BACKGROUND_ACTIVE);
        $(this).css("color", BUTTON_TEXT_COLOR_ACTIVE);
        $(this).css("height", "20px !important;");
        $(this).css("max-height", "20px !important;");
        $(this).css("border-radius", "10px;");

        $(this).animate({backgroundColor: BUTTON_BACKGROUND_ACTIVE}, 'fast',
                        function(){
                            if (!$(this).is(":hover"))
                                li.css("background-color", BUTTON_BACKGROUND);
                        }
                       );
    }, function() {
        li.css("background-color", BUTTON_BACKGROUND);
        li.css("color", BUTTON_TEXT_COLOR);
    }
);


// Stop avatar animations
//
$(".postlinks ul .btnStop").click(function(){
    var avs = $(".title img");
    for(i=0; i<avs.length; i++)
        setFirstFrame(avs[i], i)
    gmSetValue("SA_animations", false);
});


// Toggle dim/hide/show all avatars
//
$(".postlinks ul .btnFade").click(function(){

    if (avatarState == "on"){
       $(".title img").animate({opacity: ELEM_DIMMED});
       $(".title canvas").animate({opacity: ELEM_DIMMED});
       gmSetValue("SA_avatars", "dim");
       avatarState = "dim";
    }
    else if(avatarState == "dim"){
       $(".title img").animate({opacity: ELEM_HIDDEN});
       $(".title canvas").animate({opacity: ELEM_HIDDEN});
       gmSetValue("SA_avatars", "off");
       avatarState = "off";
    }
    else if(avatarState == "off"){
       $(".title img").animate({opacity: ELEM_VISIBLE});
       $(".title canvas").animate({opacity: ELEM_VISIBLE});
       gmSetValue("SA_avatars", "on");
       avatarState = "on";
    }
});

// Remove special titles.
//
$(".special_title").slideToggle(100);

// Toggle visibility for report/quote buttons
//
$(".postlinks ul .btnCtrl").toggle(
    function(){
        $(".postlinks .postbuttons").children().animate({opacity: ELEM_VISIBLE});
        gmSetValue("SA_reportButtons", true);
    }, function(){
        $(".postlinks .postbuttons").children().animate({opacity: ELEM_HIDDEN});
        gmSetValue("SA_reportButtons", false);
    }
);

// Hide/show posts
//
$(".postlinks ul .btnFold").click(function () {
    $(this).parent().parent().parent().prev().children().last().slideToggle(100);
    $(this).parent().parent().parent().prev().children().first().children().first().children().slideToggle(500);

});


// Stop avatar animations if set
//
if (!GM_getValue("SA_animations", true))
{
    var avs = $(".title img");

    for(i=0; i<avs.length; i++)
    {
        setFirstFrame(avs[i])
        //console.log(avs[i]);
    }
}    

// Hide/show Report/Quote buttons depending on settings
//
if (GM_getValue("SA_reportButtons", false))
    $(".postlinks .postbuttons").children().animate({opacity: ELEM_VISIBLE});
else
    $(".postlinks .postbuttons").children().animate({opacity: ELEM_HIDDEN});


// Hide/show avatars depending on settings
//
avatarState = GM_getValue("SA_avatars");
switch(avatarState)
{
    case "on":
       $(".title img").animate({opacity: ELEM_VISIBLE});
       $(".title canvas").animate({opacity: ELEM_VISIBLE});
       GM_setValue("SA_avatars", "on");   
       break;
    case "off":
       $(".title img").animate({opacity: ELEM_HIDDEN});
       $(".title canvas").animate({opacity: ELEM_HIDDEN});
       GM_setValue("SA_avatars", "off");   
       break;
    case "dim":
       $(".title img").animate({opacity: ELEM_DIMMED});
       $(".title canvas").animate({opacity: ELEM_DIMMED});
       GM_setValue("SA_avatars", "dim");   
       break;
    default:
       $(".title img").animate({opacity: ELEM_VISIBLE});
       $(".title canvas").animate({opacity: ELEM_VISIBLE});
       GM_setValue("SA_avatars", "on");   
       break;
}

// Hide the whole user Title (avatar+text+tags)
//
$(".postlinks ul .btnText").click(function(){
    var userTitle = $(this).parent().parent().parent().parent().find(".userinfo .title");
    var opa;

    if (avatarState == "on"){ opa = ELEM_VISIBLE; }
    else if(avatarState == "dim"){ opa = ELEM_DIMMED; }
    else if(avatarState == "off"){ opa = ELEM_HIDDEN; }
   
    if (userTitle.css('opacity') == ELEM_HIDDEN) {
       userTitle.animate({ opacity: opa });
    }else{
       userTitle.animate({ opacity: ELEM_HIDDEN });
    }
   
});


// -------------------------------------------
// Misc stuff
// -------------------------------------------

// Stop avatar animations
//
function setFirstFrame (img, idx) {
    var newCanvas = document.createElement("canvas");
    newCanvas.height = img.height;
    newCanvas.width = img.width;

    newCanvas.id = "cnvAv";

    newCanvas.getContext("2d").drawImage(img, 0, 0);
    img.parentNode.replaceChild(newCanvas, img);

    
    if (avatarState == "on")
        $(newCanvas).animate({opacity: ELEM_VISIBLE});

    else if(avatarState == "dim")
        $(newCanvas).animate({opacity: ELEM_DIMMED});

    else if(avatarState == "off")
        $(newCanvas).animate({opacity: ELEM_HIDDEN});

};

function replaceImg (img, ref) {
    //console.log('REF: ' + ref);
    img.onload = ref;
    img.src += '#';
};



// Workaround for getting/setting values from inside an event
//
function gmSetValue(key, val)
{
    setTimeout(function() {
       GM_setValue(key, val);   
    }, 0);
}

function gmGetValue(key, defaultval)
{
    setTimeout(function() {
       avatarState = GM_getValue(key, defaultval);   
    }, 0);
   
    return avatarState;
}