Greasy Fork

Greasy Fork is available in English.

9GAG video control enabler

Enables the video controls on all video elements on 9GAG.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         9GAG video control enabler
// @version      1.3
// @description  Enables the video controls on all video elements on 9GAG.
// @author       fischly
// @match        https://9gag.com/*
// @grant        none
// @run-at       document-idle
// @namespace    http://greasyfork.icu/users/662249
// @icon         https://9gag.com/favicon.ico
// @license      CC BY-SA 4.0
// ==/UserScript==

// entry point
(function() {
    'use strict';

	// first, enable controls to all elements that are already loaded on the 9GAG site
	enableVideoControls();
	
	// next, add the observer that will enable controls on all video elements that are dynamically loaded on scrolling
	addObserver();
})();


/**
 * Enables all video controls of all elements that are already loaded.
 */
function enableVideoControls() {
    const videos = document.querySelectorAll('video');

    for (const video of videos) {
        video.controls = true;
	}
}

/**
 * Adds the observer to the site element, where all the postContainers are loaded to.
 */
function addObserver() {
	// the element that all new postContainers are loaded to is called 'list-view-2' for some reason
	const targetNode = document.getElementById('page');
	const config = { attributes: false, childList: true, subtree: true };

	const callback = function(mutationsList, observer) {
		for(const mutation of mutationsList) {
			// loop over all added elements
			for (const addedElement of mutation.addedNodes) {
                enableControlsOnPostContainer(addedElement);
			}
		}
	};

	// create the observer and observe the target
	const observer = new MutationObserver(callback);
	observer.observe(targetNode, config);
}


/**
 * Enables controls on video elements of the given container.
 */
function enableControlsOnPostContainer(container) {
	if (!container) return;
    if (!container.querySelectorAll) return;

    const videos = container.querySelectorAll('video');

	for (const video of videos) {
		video.controls = true;
	}
}