Skip to content

Instantly share code, notes, and snippets.

@apfelchips
Last active July 7, 2024 07:39
Show Gist options
  • Save apfelchips/d8a6e08c1aeb79df4372096fd11c1fc1 to your computer and use it in GitHub Desktop.
Save apfelchips/d8a6e08c1aeb79df4372096fd11c1fc1 to your computer and use it in GitHub Desktop.
userscript that forces aliexpress.com to be in english
// ==UserScript==
// @name aliexpress_en
// @version 0.1
// @description always redirect to aliexpress.com and reset the location/language/currency settings. This script pins locale settings cookie. Please clear cookies on all "aliexpress" domains before using this userscript.
// @author @attero@mastodon.social
// @match *://www.aliexpress.com
// @match *://www.aliexpress.com/*
// @match *://*.aliexpress.com/*
// @match *://www.aliexpress.ru
// @match *://www.aliexpress.ru/*
// @match *://*.aliexpress.ru/*
// @noframes
// @license MIT
// @icon https://www.google.com/s2/favicons?sz=64&domain=aliexpress.com
// @supportURL https://gist.githubusercontent.com/apfelchips/d8a6e08c1aeb79df4372096fd11c1fc1/
// ==/UserScript==
// adapted from: https://greasyfork.org/en/scripts/447465-fix-com-to-ru-gatewayadapt-glo2rus-aliexpress-redirect-for-ukraine/code
// doc: https://www.tampermonkey.net/documentation.php / https://violentmonkey.github.io/api/metadata-block/
const FORCE_SITE = 'glo'
const FORCE_REGION = 'DE'
const FORCE_LOCALE = 'en_US'
const FORCE_CURRENCY = 'EUR'
const Style = {
base: [
"color: #fff",
"background-color: #444",
"padding: 2px 4px",
"border-radius: 2px",
"font-weight: bold"
],
green: [
"background-color: green"
],
red: [
"color: #eee",
"background-color: red"
],
blue: [
"background-color: blue"
],
yellow: [
"color: #444",
"background-color: yellow"
],
orange: [
"background-color: orange"
]
}
function log(input, extra = []) {
let style = Style.base.join(';') + ';';
style += extra.join(';');
let logmsg = input
if ( typeof(input) == 'object'){
logmsg = JSON.stringify(input, null, 2).replace(/^[\t ]*"[^:\n\r]+(?<!\\)":/gm, (x) => x.replace(/"/g, '')) // json without quoted properties
}
console.log(`%c${logmsg}`, style);
}
function getCookie(cName) {
var match = document.cookie.match(new RegExp('(^| )' + cName + '=([^;]+)'))
if (match) {
log(`found cookie: ${cName}: ${match[2]}`, Style.green)
return match[2]
} else {
log(`empty/unset cookie: ${cName}: ''`, Style.yellow)
return ''
}
}
function setCookie(cName, cValue, expDays) {
let date = new Date()
date.setTime(date.getTime() + (expDays * 24 * 60 * 60 * 1000))
const expires = `expires=${date.toUTCString()}`
log(`setting cookie: ${cName}: ${cValue}`, Style.orange)
document.cookie = `${cName}=${cValue}; ${expires}; path=/`
}
const aepCookie = getCookie('aep_usuc_f')
const intlCookie = getCookie('intl_locale')
const xmanCookie = getCookie('xman_us_f')
if (!(aepCookie.includes(`site=${FORCE_SITE}`)
&& aepCookie.includes(`region=${FORCE_REGION}`)
&& aepCookie.includes(`b_locale=${FORCE_LOCALE}`)
&& aepCookie.includes(`c_tp=${FORCE_CURRENCY}`)
)) {
// site=deu&c_tp=EUR&ups_d=&ups_u_t=&region=DE&b_locale=de_DE&ae_u_p_s=1
const newaepCookie = `site=${FORCE_SITE}&c_tp=${FORCE_CURRENCY}&ups_d=&ups_u_t=&region=${FORCE_REGION}&b_locale=${FORCE_LOCALE}&ae_u_p_s=1`
setCookie('aep_usuc_f', newaepCookie, 9999)
}
if ( ! intlCookie.includes(`${FORCE_LOCALE}`)) {
setCookie('intl_locale', FORCE_LOCALE, 9999)
}
if ( ! xmanCookie.includes(`x_locale=${FORCE_LOCALE}`)) {
const newxmanCookie = xmanCookie.replace(/x_locale=\w{2}_\w{2}/i, `x_locale=${FORCE_LOCALE}`)
setCookie('xman_us_f', newxmanCookie, 9999)
}
if (window.location.host.match(/^\w{2}\.aliexpress\.com/i)) {
await new Promise(r => setTimeout(r, 800))
window.stop()
log("redirecting to www.aliexpress.com", Style.red)
const oldLocation = window.location.href
const newLocation = oldLocation.replace(/\gatewayAdapt=glo2\w{3}/i, '').replace(/\w{2}\.aliexpress\.com/, "www.aliexpress.com")
window.location = newLocation;
}
if (window.location.host.includes('aliexpress.ru')) {
await new Promise(r => setTimeout(r, 800))
window.stop()
log("redirecting to www.aliexpress.com", Style.red)
const oldLocation = window.location.href
const newLocation = oldLocation.replace('gatewayAdapt=glo2rus', '').replace('aliexpress.ru', "aliexpress.com")
window.location = newLocation;
}
@gene-pavlovsky
Copy link

gene-pavlovsky commented Jul 25, 2023

This script gives me errors because await is used outside of an async function.
To fix this, I wrapped most of the code (starting from line 82) in

(async function() {
    ...
})();

Also, these patterns:

// @match        *://www.aliexpress.com
// @match        *://www.aliexpress.ru

are giving errors (at least when used with ViolentMonkey):

Bad pattern: missing "/" for path in *://www.aliexpress.com
Bad pattern: missing "/" for path in *://www.aliexpress.ru

They seem to be unnecessary, I just removed them

@gene-pavlovsky
Copy link

Hmmm, it seemed to work on my first attempt, but now it seems to be stuck in a redirect loop, once redirected to aliexpress.ru

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment