Polytoria Image Markdown Userscript

Adds image markdown support to Polytoria forums

Από την 07/08/2023. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Polytoria Image Markdown Userscript
// @description  Adds image markdown support to Polytoria forums
// @version      1.0.8
// @author       Hawli The Hawli
// @license      LGPLv2.1
// @match        https://polytoria.com/forum/post/*
// @namespace https://greasyfork.org/users/1142699
// ==/UserScript==

const classes = document.getElementsByClassName("mb-0 w-100");
for (let i = 0; i < classes.length; i++) {

    const paragraphs = classes[i].querySelectorAll('p:not([class]):is(p)');

    for (let i = 0; i < paragraphs.length; i++) {

        let paragraph = paragraphs[i];

        const a = paragraph.querySelectorAll('a')

        for (let i = 0; i < a.length; i++) {
            var linkElement = a[i];

            var url = linkElement.href;

            if (url.startsWith("https://i.imgur.com/")) {
                const img = document.createElement('img');
                img.src = url;

                linkElement.removeChild(linkElement.querySelector('i'));
                linkElement.parentNode.replaceChild(img, linkElement);
                img.previousSibling.replaceWith("")
                img.classList.add('img-fluid')
                img.classList.add('w-50')
                img.style.borderRadius = "15px";
                img.style.padding = "5px";
            }

            if (url.startsWith("https://imgur.com/a/")) {
                const img = document.createElement('img');
                const id = url.replace("https://imgur.com/a/", "");
                fetch(`https://corsproxy.io/?https://imgurapi.e94794356.repl.co/${id}`)
                    .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! Status: ${response.status}`);
                    }

                    return response.json();
                })
                    .then(data => {
                    img.src = data.image_link;
                });

                linkElement.removeChild(linkElement.querySelector('i'));
                linkElement.parentNode.replaceChild(img, linkElement);
                img.classList.add('img-fluid')
                img.classList.add('w-50')
                img.style.borderRadius = "15px";
                img.style.padding = "5px";
            }

            if (url.startsWith("https://www.youtube.com/embed/")) {
                linkElement.removeChild(linkElement.querySelector('i'));
                let youtubeUrl = linkElement.href;

                const iframe = document.createElement('iframe');
                const div = document.createElement('div');
                iframe.src = youtubeUrl;

                linkElement.parentElement.replaceChild(div, linkElement);
                div.style.cssText = "width: 640px; height: 360px;"
                iframe.style.cssText = 'height: 100%; width: 100%'
                iframe.style.borderRadius = "15px";
                div.appendChild(iframe);
            }

            if (url.startsWith("https://www.youtube.com/watch?v=")) {
                let youtubeUrl = linkElement.href;
                youtubeUrl = youtubeUrl.replace("/watch?v=", "/embed/");


                const iframe = document.createElement('iframe');
                const div = document.createElement('div');
                iframe.src = youtubeUrl;

                linkElement.parentElement.replaceChild(div, linkElement);
                div.style.cssText = "width: 640px; height: 360px;"
                iframe.style.cssText = 'height: 100%; width: 100%'
                iframe.style.borderRadius = "15px";
                div.appendChild(iframe);
            }

            if (url.startsWith("https://www.youtube.com/shorts/")) {
                var youtubeUrl = linkElement.href;
                youtubeUrl = youtubeUrl.replace("/shorts/", "/embed/");

                const iframe = document.createElement('iframe');
                const div = document.createElement('div');
                iframe.src = youtubeUrl;

                linkElement.parentElement.replaceChild(div, linkElement);
                div.style.cssText = "width: 640px; height: 360px;"
                iframe.style.cssText = 'height: 100%; width: 100%'
                iframe.style.borderRadius = "15px";
                div.appendChild(iframe);
            }
        }
    }
}