Repo tree: inline PNG images

Show PNG images inline on the GitHub's file list

2020-02-12 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Repo tree: inline PNG images
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Show PNG images inline on the GitHub's file list
// @author       Marcin Warpechowski
// @match        https://github.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const pluginName = 'processed-by-warpech-inline-png-images';

    let scheduledTimeout;

    function findAndApplyChanges() {
        console.log('Running... Repo tree: inline PNG images');
        Array.from(document.querySelectorAll(`table.files.js-navigation-container a.js-navigation-open:not([${pluginName}])`)).forEach((elem) => {
            elem.setAttribute(pluginName, '');
            if (elem.href.endsWith(".png") || elem.href.endsWith(".gif") || elem.href.endsWith(".jpg") || elem.href.endsWith(".jpeg")) {
                const img = document.createElement('img');
                let imgUrl = elem.href + '?raw=true';
                img.setAttribute("src", imgUrl);
                img.style.maxWidth = '100%';

                const imgLink = document.createElement('a');
                imgLink.href = imgUrl;
                imgLink.appendChild(img);
                imgLink.style.display = 'block';
                imgLink.style.padding = '20px';
                img.style.minHeight = '1px';

                const aTr = elem.parentNode.parentNode.parentNode;
                if (aTr.nodeName !== 'TR') {
                    throw new Error("Unexpected HTML structure. Link's parent TR was not found.");
                }

                const tr = document.createElement('tr');
                const td = document.createElement('td');
                td.setAttribute("colspan", aTr.children.length);
                tr.appendChild(td);
                td.appendChild(imgLink);
                tr.style.background = '#eee';

                aTr.parentNode.insertBefore(tr, aTr.nextSibling)
            }
        });
    }

    console.log('Postponing... Repo tree: inline PNG images');
    scheduledTimeout = setTimeout(findAndApplyChanges, 1000);

    const targetNode = document.querySelector("div.application-main");
    const observerOptions = {
        childList: true,
        subtree: true
    }

    const mutationCallback = (mutationList, observer) => {
        mutationList.forEach((mutation) => {
            switch(mutation.type) {
                case 'childList':
                    console.log('Postponing... Repo tree: inline PNG images');
                    clearTimeout(scheduledTimeout);
                    scheduledTimeout = setTimeout(findAndApplyChanges, 1000);
                    break;
            }
        });
    }

    const observer = new MutationObserver(mutationCallback);
    observer.observe(targetNode, observerOptions);
})();