Safari Embedded WebM Support

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();