raingart / Aliexpress USD

// ==UserScript==
// @name         Aliexpress USD
// @namespace    aliexpress-usd
// @description  Auto select usd
// @include      http*://*.aliexpress.*/*
// @include      http*://aliexpress.*/*
// @version      0.1
// @author       raingart
// @license      Apache-2.0
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @grant        GM_registerMenuCommand
// @require      https://cdn.jsdelivr.net/gh/kufii/My-UserScripts@c7f613292672252995cb02a0cab3b6acb18ccac5/libs/gm_config.js
// @run-at       document-end
// ==/UserScript==
/*jshint esversion: 6 */

(function () {
const config = GM_config([
   {
      key: 'locale',
      label: 'locale',
      default: 'Default',
      type: 'dropdown',
      values: ['Default', 'en_US', 'ru_RU', 'pt_BR', 'es_ES', 'fr_FR', 'pl_PL', 'iw_IL', 'it_IT', 'tr_TR', 'de_DE', 'ko_KR', 'ar_MA'],
   },
   {
      key: 'currency',
      label: 'currency',
      default: 'USD',
      type: 'dropdown',
      values: ['USD', 'AFN', 'EUR', 'LL', 'AOA', 'XCD', 'AMD', 'AWG', 'SHP', 'AUD', 'AZN', 'BSD', 'BHD', 'BDT', 'BYR', 'BZD', 'XOF', 'BMD', 'BTN', 'BAM', 'BWP', 'NOK', 'BRL', 'BND', 'BGN', 'BIF', 'KHR', 'XAF', 'CAD', 'CVE', 'KYD', 'CLP', 'KMF', 'CDF', 'NZD', 'CRC', 'CZK', 'DKK', 'DJF', 'DOP', 'EGP', 'ERN', 'XPF', 'NIO', 'NGN', 'OMR', 'PKR', 'PGK', 'PYG', 'PHP', 'PLN', 'QAR', 'RON', 'RUB', 'RWF', 'WST', 'STD', 'SAR', 'RSD', 'SCR', 'SLL', 'SGD', 'SBD', 'SOS', 'ZAR', 'KRW', 'LKR', 'SRD', 'SZL', 'SEK', 'CHF', 'TWD', 'TJS', 'TZS', 'THB', 'TOP', 'TTD', 'TND', 'TRY', 'TMT', 'UGX', 'UAH', 'AED', 'GBP', 'UYU', 'UZS', 'VUV', 'VEF', 'VND', 'YER', 'ZMW', 'ZWL', 'ETB', 'FKP', 'FJD', 'GMD', 'GEL', 'GHS', 'GIP', 'GTQ', 'GNF', 'GYD', 'HTG', 'HNL', 'HKD', 'HUF', 'ISK', 'INR', 'IDR', 'IQD', 'ILS', 'JMD', 'JPY', 'KZT', 'KES', 'KWD', 'KGS', 'LAK', 'LBP', 'LSL', 'LRD', 'LYD', 'MOP', 'MKD', 'MWK', 'MYR', 'MVR', 'MUR', 'MXN', 'MDL', 'MNT', 'MAD', 'MZN', 'MMK', 'NAD', 'NPR', 'ARS', 'PEN', 'DZD', 'JOD', 'BOB', 'COP', 'BBD', 'PAB'],
   }
]);
let conf = config.load();
config.onsave = cfg => (conf = cfg);
GM_registerMenuCommand('Manage Settings', config.setup);
// console.log('conf:', conf);

function waitForElement(selector) {
   return new Promise(function (resolve, reject) {
      var element = document.querySelector(selector);

      if (element) return resolve(element);

      var observer = new MutationObserver(function (mutations) {
         mutations.forEach(function (mutation) {
            var nodes = Array.from(mutation.addedNodes);
            for (var node of nodes) {
               if (node.matches && node.matches(selector)) {
                  observer.disconnect();
                  return resolve(node);
               }
            }
         });
      });

      observer.observe(document.documentElement, {
         childList: true,
         subtree: true
      });
   });
}

// currency
waitForElement('#switcher-info .currency:not(:empty)')
   .then(element => {
      console.log('switcher-info loaded:', element);
      if (element?.textContent.toUpperCase() === conf.currency) return;
      element.click();

      waitForElement('[data-role="switch-currency"] [class="select-item"]')
         .then(element => {
            console.log('click currency-drop-down:', element);
            element.click();
            console.debug(`[data-currency="${conf.currency}"]`);
            waitForElement(`[data-currency="${conf.currency}"]`)
               .then(element => {
                  console.log('click country-code:', element, conf.currency);
                  element.click();

                  waitForElement('[data-role="save"]').then(save => save.click());
               });
         });
   });
  /*
setTimeout(()=> {
//window.addEventListener('load', evt => {
// currency v2
waitForElement('[class*=styles_languageSwitcher] a:not(:empty)')
  .then(element => {
    console.log('styles_languageSwitcher loaded:', element);
    if (element?.textContent.split('/').pop() === conf.locale) return;
    element.click();

    waitForElement('#downshift-5-toggle-button:not(:empty)')
      .then(element => {
            console.log('click language-drop-down:', element);
            element.click();
false
            console.debug(`[data-locale="${conf.locale}"]`);
            waitForElement(`[data-locale="${conf.locale}"]`)
               .then(element => {
                  console.log('click language-code:', element, conf.locale);
                  element.click();

                  waitForElement('[data-role="save"]').then(save => save.click());
               });

         });
  });
}, 1000);
//});
*/
// language
if (conf.locale !== 'Default') {
  waitForElement('#switcher-info .language_txt:not(:empty)')
     .then(element => {
        console.log('switcher-info loaded:', element);
        if (element?.textContent === conf.locale) return;
        element.click();

        waitForElement('[data-role="language-input"]')
           .then(element => {
              console.log('click language-drop-down:', element);
              element.click();

              console.debug(`[data-locale="${conf.locale}"]`);
              waitForElement(`[data-locale="${conf.locale}"]`)
                 .then(element => {
                    console.log('click language-code:', element, conf.locale);
                    element.click();

                    waitForElement('[data-role="save"]').then(save => save.click());
                 });

           });
     });
}

})();

/*function skuClicker() {
   document.querySelectorAll('.sku-property-item span')
      .forEach(entry => {
         if ('CHINA' == entry.textContent.toLocaleLowerCase()) {
            entry.click();
         }
      })
}
document.addEventListener('DOMContentLoaded', skuClicker, true);*/