Polytoria Image Markdown Userscript

Adds image markdown support to Polytoria forums

Stan na 04-08-2023. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Polytoria Image Markdown Userscript
// @description  Adds image markdown support to Polytoria forums
// @version      1.0.3
// @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.querySelector('a');

        if (a != null) {
            var url = a.href;
            url = url.slice(0, -1);

            const stupid = paragraph.querySelectorAll('i');
            stupid.forEach(i => {
                i.remove();
            });

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

                const before = a.previousSibling;

                a.parentElement.removeChild(before);

                a.parentElement.replaceChild(img, a);
                img.classList.add('img-fluid')
                img.classList.add('w-50')
                img.style.borderRadius = "15px";
                img.style.paddingTop = "5px";
                img.style.paddingBottom = "5px";
            }

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

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

                const before = a.previousSibling;

                a.parentElement.removeChild(before);

                a.parentElement.replaceChild(div, a);
                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=")) {
                var youtubeUrl = a.href;
                youtubeUrl = youtubeUrl.replace("/watch?v=", "/embed/");
                youtubeUrl = youtubeUrl.replace(")", '');
                

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

                const before = a.previousSibling;

                a.parentElement.removeChild(before);

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