Fix .com to .ru "gatewayAdapt=glo2rus" aliexpress redirect for Ukraine.

After visiting aliexpress.ru it can mess up the locale settings on aliexpress.com - "com" will redirect to "ru" ("gatewayAdapt=glo2rus") and reset the location/language/currency settings. This script pins locale settings cookie, sets currency to USD, language to EN, country to UA, and redirects "ru" to "com". Please clear cookies on all "aliexpress" domains before using this userscript.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Fix .com to .ru "gatewayAdapt=glo2rus" aliexpress redirect for Ukraine.
// @name:uk      Фіксить редірект на aliexpress "gatewayAdapt=glo2rus" з .com на .ru для України.
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description After visiting aliexpress.ru it can mess up the locale settings on aliexpress.com - "com" will redirect to "ru" ("gatewayAdapt=glo2rus") and reset the location/language/currency settings. This script pins locale settings cookie, sets currency to USD, language to EN, country to UA, and redirects "ru" to "com". Please clear cookies on all "aliexpress" domains before using this userscript.
// @description:uk  Після кожного відвідування aliexpress.ru той ламає налаштування локалі на aliexpress.com, після чого "com" починає редиректити на "ru" ("gatewayAdapt=glo2rus") та скидати налаштування локації/мови/валюти. Цей скрипт фіксить куку з налаштуваннями, встановлює валюту USD, мову EN, країну UA, та редиректить всі лінки з "com" на "ru". Перед використанням раджу очистити кукі в усіх доменах, де зустрічається "aliexpress".
// @author       Зальотне дитя мемарні 🇺🇦✌️
// @match        *://www.aliexpress.ru
// @match        *://www.aliexpress.ru/*
// @match        *://*.aliexpress.ru/*
// @match        *://www.aliexpress.com
// @match        *://www.aliexpress.com/*
// @match        *://*.aliexpress.com/*
// @license      MIT
// @icon         https://www.google.com/s2/favicons?sz=64&domain=aliexpress.com
// ==/UserScript==

const FORCE_SITE = 'glo'
const FORCE_REGION = 'UA'
const FORCE_LOCALE = 'en_US'
const FORCE_CURRENCY = 'USD'
const CURRENCY_BLACKLIST = 'RUB'

function getCookie(cName) {
    var match = document.cookie.match(new RegExp('(^| )' + cName + '=([^;]+)'));
    if (match) {
        return match[2];
    } else {
        return '';
    }
}

function setCookie(cName, cValue, expDays) {
    let date = new Date();
    date.setTime(date.getTime() + (expDays * 24 * 60 * 60 * 1000));
    const expires = `expires=${date.toUTCString()}`;
    document.cookie = `${cName}=${cValue}; ${expires}; path=/`;
}

const cookie = getCookie('aep_usuc_f');
const isCookieOkay = (cookie.includes(`site=${FORCE_SITE}`)
                      && cookie.includes(`region=${FORCE_REGION}`)
                      && cookie.includes(`b_locale=${FORCE_LOCALE}`)
                      && !cookie.includes(`c_tp=${CURRENCY_BLACKLIST}`)
                     );

if (!isCookieOkay) {
    const newCookie = `site=${FORCE_SITE}&c_tp=${FORCE_CURRENCY}&region=${FORCE_REGION}&b_locale=${FORCE_LOCALE}`;
    // alert(`Replacing "${cookie}" with "${newCookie}"`);
    setCookie('aep_usuc_f', newCookie, 9999);
} else {
    // alert(`Cookie ok "${cookie}"');
}

if (window.location.href.includes('aliexpress.ru')) {
    window.stop();
    const oldLocation = window.location.href;
    const newLocation = oldLocation.replace('gatewayAdapt=glo2rus', '').replace('aliexpress.ru', "aliexpress.com")
    // alert('Redirecting to ' + newLocation);
    window.location = newLocation;
}