TieuThanhNhi / Google Language Switcher

// ==UserScript==
// @name         Google Language Switcher
// @name:en      Google Language Switcher
// @name:es      Cambiar idioma de Google
// @name:fr      Changeur de langue Google
// @name:de      Google Sprachumschalter
// @name:it      Cambia lingua di Google
// @name:pt      Alternador de idioma do Google
// @name:ru      Переключатель языка Google
// @name:ja      Google 言語切り替え
// @name:ko      Google 언어 전환기
// @name:zh-CN   谷歌语言切换器
// @name:zh-TW   Google 語言切換器
// @name:ar      مُغير لغة Google
// @name:hi      Google भाषा स्विचर
// @name:th      ตัวสลับภาษาของ Google
// @name:vi      Google Thay Đổi Ngôn Ngữ
// @name:id      Pengalih Bahasa Google
// @namespace    http://tampermonkey.net/
// @version      0.2
// @author       TieuThanhNhi
// @copyright 2024, TieuThanhNhi (https://openuserjs.org/users/TieuThanhNhi)
// @license      MIT
// @description  Lưu và áp dụng tùy chọn ngôn ngữ cho Google Search với nhiều ngôn ngữ phổ biến
// @description:en Save and apply language preference for Google Search with many popular languages.
// @description:es Guarda y aplica la preferencia de idioma para la Búsqueda de Google con muchos idiomas populares.
// @description:fr Enregistre et applique la préférence de langue pour la recherche Google avec de nombreuses langues populaires.
// @description:de Speichert und wendet die Spracheinstellung für die Google-Suche mit vielen gängigen Sprachen an.
// @description:it Salva e applica la preferenza di lingua per la Ricerca Google con molte lingue popolari.
// @description:pt Salva e aplica a preferência de idioma para a Pesquisa Google com muitos idiomas populares.
// @description:ru Сохраняет и применяет языковые настройки для поиска Google с множеством популярных языков.
// @description:ja 多くの一般的な言語で Google 検索の言語設定を保存して適用します。
// @description:ko 많은 인기 언어로 Google 검색의 언어 설정을 저장하고 적용합니다.
// @description:zh-CN 保存并应用多种常用语言的 Google 搜索语言偏好设置。
// @description:zh-TW 保存並套用多種常用語言的 Google 搜尋語言偏好設定。
// @description:ar احفظ لغة Google Search المفضلة لديك وقم بتطبيقها مع العديد من اللغات الشائعة.
// @description:hi लोकप्रिय भाषाओं के साथ Google खोज के लिए भाषा वरीयता सहेजें और लागू करें।
// @description:th บันทึกและปรับใช้การตั้งค่าภาษาสำหรับ Google Search ด้วยภาษาต่างๆ ที่ได้รับความนิยม
// @description:vi Lưu và áp dụng tùy chọn ngôn ngữ cho Google Tìm kiếm với nhiều ngôn ngữ phổ biến.
// @description:id Simpan dan terapkan preferensi bahasa untuk Penelusuran Google dengan banyak bahasa populer.
// @match        https://www.google.com/*
// @match        https://www.google.ad/*
// @match        https://www.google.ae/*
// @match        https://www.google.com.af/*
// @match        https://www.google.com.ag/*
// @match        https://www.google.com.ai/*
// @match        https://www.google.al/*
// @match        https://www.google.am/*
// @match        https://www.google.co.ao/*
// @match        https://www.google.com.ar/*
// @match        https://www.google.as/*
// @match        https://www.google.at/*
// @match        https://www.google.com.au/*
// @match        https://www.google.az/*
// @match        https://www.google.ba/*
// @match        https://www.google.com.bd/*
// @match        https://www.google.be/*
// @match        https://www.google.bf/*
// @match        https://www.google.bg/*
// @match        https://www.google.com.bh/*
// @match        https://www.google.bi/*
// @match        https://www.google.bj/*
// @match        https://www.google.com.bn/*
// @match        https://www.google.com.bo/*
// @match        https://www.google.com.br/*
// @match        https://www.google.bs/*
// @match        https://www.google.bt/*
// @match        https://www.google.co.bw/*
// @match        https://www.google.by/*
// @match        https://www.google.com.bz/*
// @match        https://www.google.ca/*
// @match        https://www.google.cd/*
// @match        https://www.google.cf/*
// @match        https://www.google.cg/*
// @match        https://www.google.ch/*
// @match        https://www.google.ci/*
// @match        https://www.google.co.ck/*
// @match        https://www.google.cl/*
// @match        https://www.google.cm/*
// @match        https://www.google.cn/*
// @match        https://www.google.com.co/*
// @match        https://www.google.co.cr/*
// @match        https://www.google.com.cu/*
// @match        https://www.google.cv/*
// @match        https://www.google.com.cy/*
// @match        https://www.google.cz/*
// @match        https://www.google.de/*
// @match        https://www.google.dj/*
// @match        https://www.google.dk/*
// @match        https://www.google.dm/*
// @match        https://www.google.com.do/*
// @match        https://www.google.dz/*
// @match        https://www.google.com.ec/*
// @match        https://www.google.ee/*
// @match        https://www.google.com.eg/*
// @match        https://www.google.es/*
// @match        https://www.google.com.et/*
// @match        https://www.google.fi/*
// @match        https://www.google.com.fj/*
// @match        https://www.google.fm/*
// @match        https://www.google.fr/*
// @match        https://www.google.ga/*
// @match        https://www.google.ge/*
// @match        https://www.google.gg/*
// @match        https://www.google.com.gh/*
// @match        https://www.google.com.gi/*
// @match        https://www.google.gl/*
// @match        https://www.google.gm/*
// @match        https://www.google.gr/*
// @match        https://www.google.com.gt/*
// @match        https://www.google.gy/*
// @match        https://www.google.com.hk/*
// @match        https://www.google.hn/*
// @match        https://www.google.hr/*
// @match        https://www.google.ht/*
// @match        https://www.google.hu/*
// @match        https://www.google.co.id/*
// @match        https://www.google.ie/*
// @match        https://www.google.co.il/*
// @match        https://www.google.im/*
// @match        https://www.google.co.in/*
// @match        https://www.google.iq/*
// @match        https://www.google.is/*
// @match        https://www.google.it/*
// @match        https://www.google.je/*
// @match        https://www.google.com.jm/*
// @match        https://www.google.jo/*
// @match        https://www.google.co.jp/*
// @match        https://www.google.co.ke/*
// @match        https://www.google.com.kh/*
// @match        https://www.google.ki/*
// @match        https://www.google.kg/*
// @match        https://www.google.co.kr/*
// @match        https://www.google.com.kw/*
// @match        https://www.google.kz/*
// @match        https://www.google.la/*
// @match        https://www.google.com.lb/*
// @match        https://www.google.li/*
// @match        https://www.google.lk/*
// @match        https://www.google.co.ls/*
// @match        https://www.google.lt/*
// @match        https://www.google.lu/*
// @match        https://www.google.lv/*
// @match        https://www.google.com.ly/*
// @match        https://www.google.co.ma/*
// @match        https://www.google.md/*
// @match        https://www.google.me/*
// @match        https://www.google.mg/*
// @match        https://www.google.mk/*
// @match        https://www.google.ml/*
// @match        https://www.google.com.mm/*
// @match        https://www.google.mn/*
// @match        https://www.google.ms/*
// @match        https://www.google.com.mt/*
// @match        https://www.google.mu/*
// @match        https://www.google.mv/*
// @match        https://www.google.mw/*
// @match        https://www.google.com.mx/*
// @match        https://www.google.com.my/*
// @match        https://www.google.co.mz/*
// @match        https://www.google.com.na/*
// @match        https://www.google.com.ng/*
// @match        https://www.google.com.ni/*
// @match        https://www.google.ne/*
// @match        https://www.google.nl/*
// @match        https://www.google.no/*
// @match        https://www.google.com.np/*
// @match        https://www.google.nr/*
// @match        https://www.google.nu/*
// @match        https://www.google.co.nz/*
// @match        https://www.google.com.om/*
// @match        https://www.google.com.pa/*
// @match        https://www.google.com.pe/*
// @match        https://www.google.com.pg/*
// @match        https://www.google.com.ph/*
// @match        https://www.google.com.pk/*
// @match        https://www.google.pl/*
// @match        https://www.google.pn/*
// @match        https://www.google.com.pr/*
// @match        https://www.google.ps/*
// @match        https://www.google.pt/*
// @match        https://www.google.com.py/*
// @match        https://www.google.com.qa/*
// @match        https://www.google.ro/*
// @match        https://www.google.ru/*
// @match        https://www.google.rw/*
// @match        https://www.google.com.sa/*
// @match        https://www.google.com.sb/*
// @match        https://www.google.sc/*
// @match        https://www.google.se/*
// @match        https://www.google.com.sg/*
// @match        https://www.google.sh/*
// @match        https://www.google.si/*
// @match        https://www.google.sk/*
// @match        https://www.google.com.sl/*
// @match        https://www.google.sn/*
// @match        https://www.google.so/*
// @match        https://www.google.sm/*
// @match        https://www.google.sr/*
// @match        https://www.google.st/*
// @match        https://www.google.com.sv/*
// @match        https://www.google.td/*
// @match        https://www.google.tg/*
// @match        https://www.google.co.th/*
// @match        https://www.google.com.tj/*
// @match        https://www.google.tl/*
// @match        https://www.google.tm/*
// @match        https://www.google.tn/*
// @match        https://www.google.to/*
// @match        https://www.google.com.tr/*
// @match        https://www.google.tt/*
// @match        https://www.google.com.tw/*
// @match        https://www.google.co.tz/*
// @match        https://www.google.com.ua/*
// @match        https://www.google.co.ug/*
// @match        https://www.google.co.uk/*
// @match        https://www.google.com.uy/*
// @match        https://www.google.co.uz/*
// @match        https://www.google.com.vc/*
// @match        https://www.google.co.ve/*
// @match        https://www.google.vg/*
// @match        https://www.google.co.vi/*
// @match        https://www.google.com.vn/*
// @match        https://www.google.vu/*
// @match        https://www.google.ws/*
// @match        https://www.google.rs/*
// @match        https://www.google.co.za/*
// @match        https://www.google.co.zm/*
// @match        https://www.google.co.zw/*
// @match        https://www.google.cat/*
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function () {
  'use strict';

  const LANG_PARAM = 'hl';
  const savedLang = GM_getValue('googleLang', '');

  const languages = [{
      code: 'en',
      name: 'English'
    },
    {
      code: 'es',
      name: 'Español'
    },
    {
      code: 'fr',
      name: 'Français'
    },
    {
      code: 'de',
      name: 'Deutsch'
    },
    {
      code: 'it',
      name: 'Italiano'
    },
    {
      code: 'pt',
      name: 'Português'
    },
    {
      code: 'ru',
      name: 'Русский'
    },
    {
      code: 'ja',
      name: '日本語'
    },
    {
      code: 'ko',
      name: '한국어'
    },
    {
      code: 'zh-CN',
      name: '中文 (简体)'
    },
    {
      code: 'zh-TW',
      name: '中文 (繁體)'
    },
    {
      code: 'ar',
      name: 'العربية'
    },
    {
      code: 'hi',
      name: 'हिन्दी'
    },
    {
      code: 'th',
      name: 'ไทย'
    },
    {
      code: 'vi',
      name: 'Tiếng Việt'
    },
    {
      code: 'id',
      name: 'Bahasa Indonesia'
    },
    {
      code: 'ms',
      name: 'Bahasa Melayu'
    },
    {
      code: 'tr',
      name: 'Türkçe'
    },
    {
      code: 'nl',
      name: 'Nederlands'
    },
    {
      code: 'pl',
      name: 'Polski'
    },
  ];

  function addLanguageSwitcher() {
    const switcher = document.createElement('select');
    switcher.style.position = 'fixed';
    switcher.style.top = '10px';
    switcher.style.right = '10px';
    switcher.style.zIndex = '9999';
    switcher.style.padding = '5px';
    switcher.style.borderRadius = '5px';
    switcher.style.backgroundColor = '#f8f9fa';
    switcher.style.border = '1px solid #dadce0';
    switcher.style.fontSize = '14px';
    switcher.style.color = '#202124';

    const defaultOption = document.createElement('option');
    defaultOption.value = '';
    defaultOption.textContent = 'Select Language';
    switcher.appendChild(defaultOption);

    languages.forEach(lang => {
      const option = document.createElement('option');
      option.value = lang.code;
      option.textContent = lang.name;
      if (lang.code === savedLang) {
        option.selected = true;
      }
      switcher.appendChild(option);
    });

    switcher.addEventListener('change', (e) => {
      const newLang = e.target.value;
      if (newLang) {
        GM_setValue('googleLang', newLang);
        applyLanguage(newLang);
      }
    });

    document.body.appendChild(switcher);
  }

  function applyLanguage(lang) {
    const url = new URL(window.location.href);
    url.searchParams.set(LANG_PARAM, lang);
    window.location.href = url.toString();
  }

  // Áp dụng ngôn ngữ đã lưu khi tải trang
  if (savedLang && !window.location.search.includes(LANG_PARAM)) {
    applyLanguage(savedLang);
  }

  // Thêm language switcher vào trang
  if (window.location.pathname === '/search' || window.location.pathname === '/') {
    addLanguageSwitcher();
  }
})();