FMS Image link replacer

FMS Image Link expander!

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        FMS Image link replacer
// @namespace   FMS Image link replacer
// @include     http://127.0.0.1:8080/forumviewthread.htm*
// @version     1
// @grant       none
// @description FMS Image Link expander!
// ==/UserScript==

var maxWidth = 1280
var maxHeigth = 1000
var FProxy = 'http://127.0.0.1:8888/'


function escapeRegExp(str) {
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

var imageLinkRE = new RegExp('^' + escapeRegExp(FProxy) + '(?:CHK|SSK|USK|KSK)(?:@|%40).+(?:\\.|%2E)(?:jpeg|jpg|png|gif)$', 'i');

var revealImageData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4AYNEwUuGHt4/AAABpNJREFUWMO1l3+MVNUVxz/nvjv7C2NAq1JSievM2gVEm9BUsQ0t1DYpFvbtsi4tQS1NRNK/tNZqomnXtjYpVatpamuisQapsuzOzFLFtCkS2wRbpDS0Liw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context) {
  if (!context) context = document;
  var i, arr = [], xpr = document.evaluate(p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  for (i = 0; item = xpr.snapshotItem(i); i++) arr.push(item);
  return arr;
}


function testLinkToImage(link) {
	return imageLinkRE.test(link.href);
}

function revealPost(post, revealImage) {
    revealImage.parentNode.removeChild(revealImage);
    
    $x('a | div[@class="postattachments"]/div[@class="attachment"]/a', post).filter(testLinkToImage).forEach(function(link) {
        
        img = new Image();
        img.src = link.href;
        img.alt = 'Loading '+link.innerText;

        img.addEventListener('load', function() {
            var setWidth;
            var setHeigth;

            if(this.naturalWidth > maxWidth)
                setWidth = maxWidth +"px"; 
            else
                setWidth = this.naturalWidth + "px";

            if(this.naturalHeigth > maxHeigth)
                setHeigth = maxHeigth +"px"; 
            else
                setHeigth = this.naturalHeigth + "px";

            this.style.width = setWidth;
            this.style.heigth = setHeigth;


        }, false);

        link.innerHTML = '';
        link.appendChild(img);
            
    });
}

function hookPost(post) {
    post.hooked = true;
        
    img = new Image;
    img.src = revealImageData;
    img.title = 'Show images in this post'
    img.style.cursor = 'pointer';
    img.addEventListener('click', function() {
        revealPost(this.parentElement, this);
    });

    post.insertBefore(img, post.firstChild);
}

$x('//td[@class="post"]/a', document.body).filter(testLinkToImage).forEach(function(link) {
    if(!link.parentElement.hooked)
        hookPost(link.parentElement);
});

$x('//td[@class="post"]/div[@class="postattachments"]/div[@class="attachment"]/a', document.body).filter(testLinkToImage).forEach(function(link) {
    var post = link.parentElement.parentElement.parentElement;
    
    if(!post.hooked)
        hookPost(post);
});