9gag video enhancer

try to take over the world!

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         9gag video enhancer
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  try to take over the world!
// @author       Llorx
// @match        https://9gag.com/*
// @grant        none
// ==/UserScript==

// from 0 to 100
var VOUME_LEVEL = 50;

function onFirstPlay(e) {
    e.target.removeEventListener("play", onFirstPlay);
    e.target.click(); // element click instead of setting the video muted attribute, so 9gag internal state also changes to umnmuted
    e.target.volume = VOUME_LEVEL/100;
}

(function() {
    function checkVideos(node) {
        if (node.querySelectorAll) { // Check if is an Element without the need of "instanceof Element"
            if (node.classList && node.classList.contains("length")) { // Check if is the timestamp overlay, which is added dynamically
                var v = node.parentNode.querySelector("video"); // If is a .length element with the same parent as a video, then is a timestamp overlay. Hide it
                if (v && v.parentNode === node.parentNode) {
                    node.style.display = "none";
                }
            } else {
                var videoLength = node.querySelectorAll("video"); // Get all videos and do the work
                for (var i = 0; i < videoLength.length; i++) {
                    var video = videoLength[i];

                    // Show controls
                    video.setAttribute("controls", "1");

                    // Set volume by clicking over the video, but only after the video plays automatically (managed by 9gag)
                    if (VOUME_LEVEL > 0) {
                        var audiotoggle = video.parentNode.querySelector(".sound-toggle");
                        if (audiotoggle) { // Has audio
                            video.addEventListener("play", onFirstPlay);
                        }
                    }

                    // Fix timebar drag bug
                    var drag = video.closest(".badge-track");
                    if (drag) {
                        drag.addEventListener("dragstart", function(e) {
                            e.preventDefault();
                        });
                    }

                    // Hide 9gag overlays
                    var els = video.parentNode.querySelectorAll(".sound-toggle,.length");
                    for (var ii = 0; ii < els.length; ii++) {
                        els[ii].style.display = "none";
                    }
                }
            }
        }
    }
    var observer = new MutationObserver(function(mutationsList) {
        // Check for dynamic added content to the body
        for (var i = 0; i < mutationsList.length; i++) {
            for (var ii = 0; ii < mutationsList[i].addedNodes.length; ii++) {
                checkVideos(mutationsList[i].addedNodes[ii]);
            }
        }
    });
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
    checkVideos(document.body);
})();