YouTube Flag Tracker

Track changes in YouTube's experiment flags

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        YouTube Flag Tracker
// @namespace   Violentmonkey Scripts
// @version     1.2.2
// @description Track changes in YouTube's experiment flags
// @include     http*://www.youtube.com/*
// @grant       unsafeWindow
// @grant       GM_setValue
// @grant       GM_getValue
// @run-at      document-end
// ==/UserScript==

function save(key, data) {

    if (!Object.keys(data).length) {
        return;
    }

    if (key === "changes") {
        const changes = GM_getValue("changes", []);
        changes.unshift(data);
        data = changes;
    }

    GM_setValue(key, data);
}

function isEqual(a, b) {

    if (Array.isArray(a) && Array.isArray(b)) {
        return (
            a.length === b.length &&
            JSON.stringify(a) === JSON.stringify(b)
        );
    }

    return a === b;
}

function diffChecker(prev = {}, curr = {}) {

    let changes = {};

    const allKeys = new Set([...Object.keys(prev), ...Object.keys(curr)]);

    allKeys.forEach(key => {

        const prevVal = prev[key];
        const currVal = curr[key];

        if (prevVal === undefined) {
            changes[key] = { type: "added", value: currVal };
        }

        else if (currVal === undefined) {
            changes[key] = { type: "removed" };
        }

        else if (!isEqual(prevVal, currVal)) {
            changes[key] = { type: "modified", value: currVal };
        }
    });

    return changes;
}

function updateStorage(currentFlags) {

    const previousFlags = GM_getValue("flags", {});
    const changes = diffChecker(previousFlags, currentFlags);

    const numChanges = Object.keys(changes).length;
    const maxChanges = Object.keys(previousFlags).length / 2;

    if (!maxChanges || numChanges <= maxChanges) {
        save("changes", changes);
        save("flags", currentFlags);
    }
}

function observeFlags() {

    const observer = new MutationObserver(() => {

        const flags = unsafeWindow.yt?.config_?.EXPERIMENT_FLAGS;

        if (flags) {
            observer.disconnect();
            updateStorage(flags);
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });
}


observeFlags();