Image Proxier

Replaces images from certain websites with a proxied version.

Verzia zo dňa 16.08.2018. Pozri najnovšiu verziu.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name         Image Proxier
// @namespace    https://github.com/GrumpyCrouton/Userscripts
// @version      1.0
// @description  Replaces images from certain websites with a proxied version.
// @author       GrumpyCrouton
// @match        *://*/*
// @grant        none
// ==/UserScript==
(function() {
    'use strict';

    var replace_from = [
        'stack.imgur.com',
        'i.stack.imgur.com',
        'graph.facebook.com',
    ];

    var proxy_link = "//grumpycrouton.com/other/image_proxy/?url=";

    var $ = window.jQuery;

    $(function() {
        //runProxier();
    });

    function runProxier() {
        $('img').each(function() {
            var image = $(this);
            if(!image.data('image_proxier_converted')) {
                image.attr('src', getProxyLink(image.attr('src')));
                image.attr('data-image_proxier_converted', 1);
            }
        });
        $('a').each(function() {
            var link = $(this);
            if(!link.data('image_proxier_converted')) {
                link.attr('href', getProxyLink(link.attr('href')));
                link.attr('data-image_proxier_converted', 1);
            }
        });
    }

    function getProxyLink(givenLink) {
        if (givenLink) {
            if (replace_from.some(function(v) {
                    return givenLink.indexOf(v) >= 0;
                })) {
                var new_link = proxy_link + encodeURIComponent(givenLink);
                console.log(new_link);
                return new_link;
            } else {
                return givenLink;
            }
        }
    }

    (function(window) {
        var last = +new Date();
        var delay = 100; // default delay

        // Manage event queue
        var stack = [];

        function callback() {
            var now = +new Date();
            if (now - last > delay) {
                for (var i = 0; i < stack.length; i++) {
                    stack[i]();
                }
                last = now;
            }
        }

        // Public interface
        var onDomChange = function(fn, newdelay) {
            if (newdelay) delay = newdelay;
            stack.push(fn);
        };

        // Naive approach for compatibility
        function naive() {

            var last = document.getElementsByTagName('*');
            var lastlen = last.length;
            var timer = setTimeout(function check() {

                // get current state of the document
                var current = document.getElementsByTagName('*');
                var len = current.length;

                // if the length is different
                // it's fairly obvious
                if (len != lastlen) {
                    // just make sure the loop finishes early
                    last = [];
                }

                // go check every element in order
                for (var i = 0; i < len; i++) {
                    if (current[i] !== last[i]) {
                        callback();
                        last = current;
                        lastlen = len;
                        break;
                    }
                }

                // over, and over, and over again
                setTimeout(check, delay);

            }, delay);
        }

        //
        //  Check for mutation events support
        //

        var support = {};

        var el = document.documentElement;
        var remain = 3;

        // callback for the tests
        function decide() {
            if (support.DOMNodeInserted) {
                window.addEventListener("DOMContentLoaded", function() {
                    if (support.DOMSubtreeModified) { // for FF 3+, Chrome
                        el.addEventListener('DOMSubtreeModified', callback, false);
                    } else { // for FF 2, Safari, Opera 9.6+
                        el.addEventListener('DOMNodeInserted', callback, false);
                        el.addEventListener('DOMNodeRemoved', callback, false);
                    }
                }, false);
            } else if (document.onpropertychange) { // for IE 5.5+
                document.onpropertychange = callback;
            } else { // fallback
                naive();
            }
        }

        // checks a particular event
        function test(event) {
            el.addEventListener(event, function fn() {
                support[event] = true;
                el.removeEventListener(event, fn, false);
                if (--remain === 0) decide();
            }, false);
        }

        // attach test events
        if (window.addEventListener) {
            test('DOMSubtreeModified');
            test('DOMNodeInserted');
            test('DOMNodeRemoved');
        } else {
            decide();
        }

        // do the dummy test
        var dummy = document.createElement("div");
        el.appendChild(dummy);
        el.removeChild(dummy);

        // expose
        window.onDomChange = onDomChange;
    })(window);

    onDomChange(function() {
        runProxier();
    });

})();