maloomf9 / FA Pixel Perfect

// ==UserScript==
// @name          FA Pixel Perfect
// @namespace	  http://www.furaffinity.net/maloo
// @version       0.30
// @description   Replaces the normal "Click to resize" functionality to switch to unscaled pixels, and prevents oversized images from breaking the site layout. Tested on Firefox and both Modern and Classic themes. Updated 8/10/2020.
// @include       *://*furaffinity.net/view*
// @license       MIT
// @run-at document-start
// ==/UserScript==


function addWindowScript(fn, ident)
{
    var script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.setAttribute("id", ident);
    script.textContent = fn.toString().slice(fn.toString().indexOf("{") + 1,-2); //KLUUUUUUDGE.
    modifyHeadSafe(function () {document.head.appendChild(script)});
}

var headFunctions = [];
function modifyHeadSafe(toDo){
    if ((document.head) && (headFunctions.length == 0))
    {
        if (toDo != null) toDo();
    }
    else
    {
        //console.log("Pushing headFunction #" + headFunctions.length);
        headFunctions.push(toDo);
    }
}

var waitForHead = setInterval(function() {
    if (document.head)
    {
        while (headFunctions.length != 0)
        {
            //console.log("Executing headFunction #" + headFunctions.length);
            headFunctions.shift()();
        }
        clearInterval(waitForHead);
    }
},50);

/*function exec(fn) {
    var script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.textContent = '(' + fn + ')();';
    document.head.appendChild(script); // run the script
    document.head.removeChild(script); // clean up
}*/

/*exec(function() {
    var FAPixPer;
    FAPixPer.counter = 0;
    FAPixPer.fullsize = false;
    FAPixPer.moderntheme = false;
    FAPixPer.newDiv;
    FAPixPer.xres = 1280;
    FAPixPer.yres = 1280;
});*/

addWindowScript(function() {

    var FAPixPer = new Object();
    FAPixPer.counter = 0;
    FAPixPer.fullsize = false;
    FAPixPer.moderntheme = false;
    FAPixPer.newDiv;
    FAPixPer.xres = 1280;
    FAPixPer.yres = 1280;

function pnDebounce(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this,
            args = arguments;
        var later = function() {
            timeout = null;
            if ( !immediate ) {
                func.apply(context, args);
            }
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait || 200);
        if ( callNow ) {
            func.apply(context, args);
        }
    };
}

function imToggle()
{
    if (FAPixPer.moderntheme)
    {
        lightbox_create("submission", FAPixPer.newDiv);
        document.querySelector('html').style = 'overflow-y: hidden;';
        FAPixPer.counter = 0;
        //lightbox_create("explodeplease");
        setTimeout(resize_handle(), 50);
    }
    else if (FAPixPer.fullsize == false) {
        document.querySelector('#subornedImg').style = "width: " + (FAPixPer.xres / window.devicePixelRatio) + "px; max-width: unset;";
        FAPixPer.fullsize = true;
    }
    else
    {
        document.querySelector('#subornedImg').style = "max-width: 99%";
        FAPixPer.fullsize = false;
    }
}

function resize_handle()
{
    if (FAPixPer.fullsize || (FAPixPer.moderntheme && document.querySelector('#subornedImg'))) {
        document.querySelector('#subornedImg').style = "width: " + (FAPixPer.xres / window.devicePixelRatio) + "px; height: " + (FAPixPer.yres / window.devicePixelRatio) + "px; max-width: unset; max-height: unset; position: static;";
    }

}

var waitForSub = setInterval(function() {
   if (document.querySelector('#submissionImg'))
     {
         var temp = document.querySelector('.maintable')
         if (temp)
         {
             temp.style = 'table-layout: fixed;';
         }
         else
         {
             FAPixPer.moderntheme = true;
         }

         var resList;
         if (document.querySelector('.stats-container'))
         {
             if (document.querySelector('.info'))
                 resList = document.querySelector('.info').innerHTML.match(/\d+\s?x\s?\d+/g);
             else
                 resList = document.querySelector('.stats-container').innerHTML.match(/\d+\s?x\s?\d+/g);

             resList = resList[0].match(/\d+/g);
             //resString = resString.
         }
         else
         {
             if (++(FAPixPer.counter) == 20) { clearInterval(waitForSub); console.log("PP Timed Out waiting for Res"); }
             return;
         }
         FAPixPer.xres = parseInt(resList[0], 10);
         FAPixPer.yres = parseInt(resList[1], 10);
         window.addEventListener('resize', pnDebounce(resize_handle, 200, false));
         console.log("Res detected: " + FAPixPer.xres + ", " + FAPixPer.yres);
         if (document.querySelector('#flash_embed'))
         {
             document.querySelector('#flash_embed').style = "max-width: " + (FAPixPer.xres / window.devicePixelRatio) + "px; max-height: " + (FAPixPer.yres / window.devicePixelRatio) + "px;";//"transform: scale(" + ((1 / window.devicePixelRatio)) + ");"
             //document.querySelector('#flash_embed').style += "max-height: " + (yres / window.devicePixelRatio) + "px;";
         }
         else
         {
             var imgsrc = document.querySelector('#submissionImg').src;
             var newImg = document.createElement('img');
             FAPixPer.newDiv = document.createElement('div');
             FAPixPer.newDiv.id = "newDiv";


             newImg.src = imgsrc;
             //newImg.class = 'imgresizer';
             // + (xres / window.devicePixelRatio) + "px;";
             newImg.id = 'subornedImg';
             FAPixPer.newDiv.appendChild(newImg);
             var target = document.querySelector('#submissionImg').nextSibling;
             var targetParent = document.querySelector('#submissionImg').parentElement;
             document.querySelector('#submissionImg').remove();//"transform: scale(" + ((1 / window.devicePixelRatio)) + ");";
             if (FAPixPer.moderntheme)
             {
                 newImg.style = "width: " + (FAPixPer.xres / window.devicePixelRatio) + "px; height: " + (FAPixPer.yres / window.devicePixelRatio) + "px; max-width: unset; max-height: unset; position: static;";
                 var anotherImg = document.createElement('img');
                 anotherImg.src = imgsrc;
                 anotherImg.id = "subImg";
                 anotherImg.style = "max-width: 99%";
                 anotherImg.addEventListener("click", imToggle);
                 targetParent.appendChild(anotherImg);

                 FAPixPer.newDiv.style = 'width:100%; height:100%; overflow: auto; position: static; display: flex;';

                 FAPixPer.counter = 0;
                 var waitForLBDest = setInterval(function() {
                     if (window.lightbox_destroy)
                     {
                         var oldfunc = window.lightbox_destroy;
                         window.lightbox_destroy = function(identifier) {
                             console.log("Destroy called with id " + identifier);
                             if (identifier == "submission")
                             {
                                 FAPixPer.counter++;
                                 console.log(FAPixPer.counter);
                                 if (FAPixPer.counter > 1)
                                 {
                                     document.querySelector('html').style = '';
                                     //FAPixPer.newDiv = document.querySelector('#newDiv');
                                     oldfunc(identifier);
                                 }
                             }
                             else oldfunc(identifier);
                         }
                         clearInterval(waitForLBDest);
                     }

             if (++(FAPixPer.counter) == 20) { clearInterval(waitForSub); console.log("LBDest Timed Out"); }
         }, 50);

             }
             else
             {
                 newImg.style = "max-width: 100%";
                 newImg.addEventListener("click", imToggle);
                 FAPixPer.newDiv.style = 'width:99%; overflow-x: auto';
                 targetParent.insertBefore(FAPixPer.newDiv, target);
             }
         }
         clearInterval(waitForSub);

     }
    if (++(FAPixPer.counter) == 20) { clearInterval(waitForSub); console.log("PP Timed Out"); }
},50);
}, "FAPixPerfect");