Safari Embedded WebM Support

Lets Safari play embedded WebM videos on supported imageboards: 4chan, 8chan, Gelbooru and Danbooru.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         Safari Embedded WebM Support
// @namespace    safari-embedded-webm-support
// @version      1.2
// @description  Lets Safari play embedded WebM videos on supported imageboards: 4chan, 8chan, Gelbooru and Danbooru.
// @author       Calumks
// @grant        none
// @run-at       document-end
// @match        *://boards.4chan.org/*
// @match        *://8ch.net/*
// @match        *://gelbooru.com/index.php?page=post&*
// @match        *://danbooru.donmai.us/posts/*
// ==/UserScript==

(function() {
    'use strict';

    // Create the VLC video player
    function createVLC(video) {
        let vlc = document.createElement('embed');
        vlc.setAttribute('autoplay', true);
        vlc.setAttribute('loop', true);
        vlc.setAttribute('class', video.getAttribute('class'));
        vlc.setAttribute('type', 'application/x-vlc-plugin');
        vlc.setAttribute('pluginspage', 'http://www.videolan.org');
        vlc.setAttribute('width', 855);
        vlc.setAttribute('height', 481);
        vlc.setAttribute(
            'target', video.src || video.querySelector('source').src
        );
        video.after(vlc);
        video.setAttribute(
            'style', 'opacity: 0; height: 0; width: 0; display: none'
        );
        if (document.querySelector('a[title^="4chan X"]')) {
            let thumbnail = video.parentNode.querySelector('img');
            thumbnail.setAttribute('style', 'display: none');
            video.removeAttribute('src');
        }
        return vlc;
    }

    // Destroy the VLC player
    function destroyVLC(video, embed) {
        if (document.querySelector('a[title^="4chan X"]')) {
            let thumbnail = embed.parentNode.querySelector('img');
            thumbnail.removeAttribute('style');
            video.src = embed.target;
        }
        embed.remove();
    }

    // Filter observer nodes to video elements.
    function filterNodes(nodes) {
        nodes = Array.from(nodes);
        let n1 = nodes.filter(n => n instanceof HTMLVideoElement);
        let n2 = nodes
            .filter(n => n instanceof HTMLElement)
            .map(n => n.querySelector('video'))
            .filter(n => n);
        return n1.concat(n2);
    }

    // Dynamic videos
    new MutationObserver(mutations => {
        for(let mutation of mutations) {
            filterNodes(mutation.addedNodes).map(n => createVLC(n));
            filterNodes(mutation.removedNodes).map(n =>
                destroyVLC(n, mutation.target.querySelector('embed')));
        }
    }).observe(document, {attributes: false, childList: true, subtree: true});

    // On page load
    let video = document.querySelector('video');
    ! video || createVLC(video);
})();