Polytoria Image Markdown Userscript

Adds image markdown support to Polytoria forums

目前為 2023-08-06 提交的版本,檢視 最新版本

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

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

            const linkElement = a[i];
            var url = linkElement.href;

            if (url.charAt(url.length - 1) === ')') {
                url = url.slice(0, -1);
            }

            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.paddingTop = "5px";
                img.style.paddingBottom = "5px";
            }

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

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

                linkElement.previousSibling.replaceWith("")

                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/");
                youtubeUrl = youtubeUrl.replace(")", '');


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

                const before = a.previousSibling;

                linkElement.parentElement.removeChild(before);

                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/");
                youtubeUrl = youtubeUrl.replace(")", '');


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

                const before = a.previousSibling;

                linkElement.parentElement.removeChild(before);

                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);
            }
        }
    }
}