From a7c1f8f57e90c6071b0285cd2f67a4e4eb2fad9f Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Wed, 8 Nov 2023 23:18:55 +0000 Subject: try mutation listener + formatting step --- plugins/style/style.js | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) (limited to 'plugins/style') diff --git a/plugins/style/style.js b/plugins/style/style.js index 46bbc63..0500b7e 100644 --- a/plugins/style/style.js +++ b/plugins/style/style.js @@ -89,19 +89,33 @@ if (document.domain == "twitter.com") { } `; - document.addEventListener('DOMContentLoaded', function() { - // Select all the videoPlayer elements - var videoPlayers = document.querySelectorAll('[data-testid="videoPlayer"]'); - - // Loop through the NodeList - videoPlayers.forEach(function(videoPlayer) { - // Traverse two levels up the DOM tree - var grandparentElement = videoPlayer.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement; - - // Hide the grandparent element - grandparentElement.style.display = 'none'; + // Function to hide the grandparent of video players + function hideVideoPlayerGrandparent() { + document + .querySelectorAll('[data-testid="videoPlayer"]') + .forEach(function (videoPlayer) { + var grandparentElement = videoPlayer.parentElement.parentElement; + grandparentElement.style.display = "none"; + }); + } + + // Create a new MutationObserver instance + var observer = new MutationObserver(function (mutations) { + mutations.forEach(function (mutation) { + if (mutation.addedNodes.length) { + hideVideoPlayerGrandparent(); // Call the function to hide video players + } }); }); + + // Options for the observer (which mutations to observe) + var config = { childList: true, subtree: true }; + + // Start observing the target node for configured mutations + observer.observe(document.body, config); + + // Call the function initially to hide any video players on initial load + hideVideoPlayerGrandparent(); } if (document.domain == "reddit.com" || document.domain == "old.reddit.com") { -- cgit v1.2.3