YouTube and Google region setter

Youtube and google regularly change your region based on your IP. This userscript automatically sets it to your preferred region instead.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @run-at      document-start
// @version     1.0.1
// @name        YouTube and Google region setter
// @namespace   https://github.com/emmaexe/userscripts
// @author      emmaexe
// @description Youtube and google regularly change your region based on your IP. This userscript automatically sets it to your preferred region instead.
// @license     GPL-3.0-only
// @homepageURL https://github.com/emmaexe/userscripts
// @supportURL  https://github.com/emmaexe/userscripts/issues
// @include     *://youtube.*/*
// @include     *://*.youtube.*/*
// @include     *://www.google.com/*
// @exclude     *://www.google.com/a/*
// @noframes
// @icon        https://raw.githubusercontent.com/emmaexe/userscripts/main/youtube-region-setter/assets/youtube-ico-32.png
// @grant       GM.registerMenuCommand
// @grant       GM.getValue
// @grant       GM.setValue
// ==/UserScript==

GM.registerMenuCommand('Set preferred youtube/google region', async () => {
    region = prompt('Please enter your preferred region:', 'GB');
    await GM.setValue('region', region);
    window.location.reload();
})

async function main(){
    let region = await GM.getValue('region', 'GB');
    let oldUrl = location.href;
    let arr = oldUrl.split('?');
    if (!(oldUrl.includes(`&gl=${region}`) || oldUrl.includes(`?gl=${region}`))) {
        if (arr.length > 1 && arr[1] !== '') {
            location.replace(oldUrl+`&gl=${region}`);
        } else {
            location.replace(oldUrl+`?gl=${region}`);
        }
    }
}

main();