utahcon / Force AWS Region at Login

// ==UserScript==
// @name         Force AWS Region at Login
// @namespace    https://utahcon.dev/
// @version      0.2
// @license      MIT
// @description  Catch when AWS tries to signin to a non-preferred region, and redirect
// @author       utahcon
// @match        https://*.signin.aws.amazon.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=amazonaws.com
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

function build(){

    const regionSelectBar = `
    .regionSelectBar {
       background: #CCC;
       padding: 5px;
    }
    `.trim()

    const regionSelectBarChildren = `
    .regionSelectBar>* {
    padding: 5px;
    }`.trim()

    const regionSelectBarAlert = `
    .regionSelectBarAlert {
       background: #6CFF01;
       border: 1px solid #173700;
       display: none;
    }
    `.trim()

    let regionSelectStyleSheet = new CSSStyleSheet();
    regionSelectStyleSheet.insertRule(regionSelectBar);
    regionSelectStyleSheet.insertRule(regionSelectBarChildren);
    regionSelectStyleSheet.insertRule(regionSelectBarAlert);


    document.adoptedStyleSheets = [...document.adoptedStyleSheets, regionSelectStyleSheet];

    let _regionSelectDiv = document.createElement("div");
    _regionSelectDiv.setAttribute("id", "regionSelectBar");
    _regionSelectDiv.classList.add("regionSelectBar");

    let _regionSelectText = document.createElement("span");
    _regionSelectText.textContent = "Select your default region:";

    let _regionSelectDropdown = document.createElement("select");
    _regionSelectDropdown.setAttribute("id", "regionSelectDropdown");

    const regionList = ["eu-north-1","ap-south-1","eu-west-3","eu-west-2","eu-west-1","ap-northeast-3","ap-northeast-2","ap-northeast-1","sa-east-1","ca-central-1","ap-southeast-1","ap-southeast-2","eu-central-1","us-east-1","us-east-2","us-west-1","us-west-2"];
    regionList.forEach(regionName => {
        let _regionSelectDropdownOption = document.createElement("option");
        _regionSelectDropdownOption.textContent = regionName;
        _regionSelectDropdownOption.setAttribute("value", regionName);
        _regionSelectDropdown.appendChild(_regionSelectDropdownOption)
    })

    let _regionSelectSubmitButton = document.createElement("button");
    _regionSelectSubmitButton.setAttribute("id", "regionSelectSubmitButton");
    _regionSelectSubmitButton.innerHTML = "Go";

    let _regionSelectBarAlert = document.createElement("div");
    _regionSelectBarAlert.setAttribute("id", "regionSelectBarAlert");
    _regionSelectBarAlert.classList.add("regionSelectBarAlert");
    _regionSelectBarAlert.innerHTML = "REDIRECTING";

    _regionSelectDiv.appendChild(_regionSelectText);
    _regionSelectDiv.appendChild(_regionSelectDropdown);
    _regionSelectDiv.appendChild(_regionSelectSubmitButton);
    _regionSelectDiv.appendChild(_regionSelectBarAlert);
    document.body.prepend(_regionSelectDiv);
}

(function() {
    'use strict';

    build();

    const regionSelectSubmitButton = document.getElementById("regionSelectSubmitButton");
    regionSelectSubmitButton.onclick = function(){
        const newRegion = document.getElementById("regionSelectDropdown").value
        GM_setValue("region", newRegion);
        document.location.href = document.location.href.match(/^(https:\/\/)(\w*-\w*-\d*)(.*)/)[1] + GM_getValue('region') + document.location.href.match(/^(https:\/\/)(\w*-\w*-\d*)(.*)/)[3]
    }

    // check if a preference has been set
    let current_region = document.location.href.match(/^https:\/\/(?<region>.*).signin.aws.amazon.com/)[1];
    document.getElementById("regionSelectDropdown").value = current_region;

    console.log("Current Region: "+ current_region);
    console.log("Desired Region: "+ GM_getValue('region', false));

    if (current_region != GM_getValue('region', false)){
        // redirect
        let regionSelectBarAlert = document.getElementById("regionSelectBarAlert")
        regionSelectBarAlert.style.display = "block";

        let matches = location.href.match(/^(https:\/\/)(\w*-\w*-\d*)(.*)/);
        let newLocationHref = location.href.match(/^(https:\/\/)(\w*-\w*-\d*)(.*)/)[1] + GM_getValue('region') + location.href.match(/^(https:\/\/)(\w*-\w*-\d*)(.*)/)[3];
        document.location.href = newLocationHref;
    }
})();