YouTube Custom Country Code

Custom country code on YouTube

// ==UserScript==
// @name         YouTube Custom Country Code
// @version      0.1
// @description  Custom country code on YouTube
// @author       donotprikel231
// @license      MIT
// @match        https://www.youtube.com/*
// @grant        none
// @namespace    http://tampermonkey.net/
// ==/UserScript==

(function() {
    'use strict';

    function setCustomCountryCode(customCode) {
        const countryCodeElement = document.getElementById('country-code');
        if (countryCodeElement) {
            countryCodeElement.textContent = customCode;
        }
        localStorage.setItem('customCountryCode', customCode);
    }

    function getStoredCountryCode() {
        return localStorage.getItem('customCountryCode') || 'US';
    }

    function createContextMenu(event) {
        event.preventDefault();

        const countries = ['US', 'CA', 'GB', 'AU', 'JP', 'DE', 'FR', 'IT', 'ES', 'BR', 'IN', 'RU', 'KR', 'CN', 'MX', 'ID', 'TR', 'SE', 'SG', 'NL', 'BE', 'CH', 'AT', 'DK', 'FI', 'NO', 'PL', 'CZ', 'PT', 'HU', 'GR', 'IE', 'NZ', 'ZA'];

        const menuContainer = document.createElement('div');
        menuContainer.style.position = 'fixed';
        menuContainer.style.top = `${event.clientY}px`;
        menuContainer.style.left = `${event.clientX}px`;
        menuContainer.style.background = '#ffffff';
        menuContainer.style.border = '1px solid #000000';
        menuContainer.style.padding = '12px';
        menuContainer.style.zIndex = '9999';

        const menuItemStyle = `
            cursor: pointer;
            font-size: 14px;
            margin-bottom: 5px;
        `;

        countries.forEach(country => {
            const menuItem = document.createElement('div');
            menuItem.textContent = country;
            menuItem.style = menuItemStyle;
            menuItem.addEventListener('click', () => {
                setCustomCountryCode(country);
                menuContainer.remove();
            });
            menuContainer.appendChild(menuItem);
        });

        document.body.appendChild(menuContainer);

        document.addEventListener('click', () => {
            menuContainer.remove();
        }, { once: true });
    }

    const initialCountryCode = getStoredCountryCode();
    setCustomCountryCode(initialCountryCode);

    const logoElement = document.querySelector('#logo-icon');
    logoElement.addEventListener('contextmenu', createContextMenu);
})();