Skip to content

Instantly share code, notes, and snippets.

@Zierk59
Created November 7, 2023 21:09
Show Gist options
  • Save Zierk59/0a3f34d16e7617731f504cef187641e1 to your computer and use it in GitHub Desktop.
Save Zierk59/0a3f34d16e7617731f504cef187641e1 to your computer and use it in GitHub Desktop.
This userscript is designed to diagnose issues related to the modification of YouTube's video player, which might interfere with other userscripts like anti anti-adblockers
// ==UserScript==
// @name YouTube: observer
// @namespace http://tampermonkey.net/
// @version 1
// @description try to take over the world!
// @author You
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// @run-at document-start
// ==/UserScript==
(function () {
'use strict';
const log = (message) => {
const template = `%c[%c{{date-time}}%c] [%cObserver%c] %c{{message}}`;
const result = template
.replace('{{date-time}}', (new Date(Date.now() + 6e5)).toISOString())
.replace('{{message}}', message);
console.log(result, 'color: black', 'color: blue', 'color: black', 'color: green', 'color: black', 'color: grey');
};
const getNodeSymbol = (node) => {
return `${node.tagName}.${[...node.classList].join('.')}`
}
const getNodeDefinitions = (node) => {
return `<${node.tagName} ${ node.getAttributeNames().map(x => `${x}="${node.getAttribute(x)}"`).join(' ') }> </ ${node.tagName}>`
}
const init = () => {
log('initializing video player observer');
// Callback function to execute when mutations are observed
const callback = (mutationsList, observer) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
for (let node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE && node.classList.contains('html5-main-video')) {
log(`HTML5 video ${getNodeDefinitions(mutation.target)} added`);
}
}
}
else if (mutation.type === 'attributes') {
if (mutation.target.classList.contains('html5-video-container')) {
log(`Attribute '${mutation.attributeName}' of ${getNodeSymbol(mutation.target)} was modified.`);
log(`after modifications it looks like ${getNodeDefinitions(mutation.target)}`);
}
else {
if (mutation.target.tagName === 'VIDEO') {
log(`Attribute '${mutation.attributeName}' of ${getNodeSymbol(mutation.target)} was modified.`);
log(`after modifications it looks like ${getNodeDefinitions(mutation.target)}`);
}
}
}
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
const config = {
childList: true,
subtree: true,
attributes: true
};
const startObserving = () => {
if (document.body) {
observer.observe(document.body, config);
log('observer has been attached and it`s working');
}
else {
requestAnimationFrame(startObserving);
}
};
// Start trying to observe
startObserving();
};
init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment