#Snippets #JavaScript February 09, 2024

JavaScript Bookmarklet: Region URL Converter

V2

  • Uses an object literal to store site information, where each key is a site code and the value is the corresponding URL.
  • Finds the selected site URL by looking up the user-entered site code in the object.
  • Finds the current site URL by iterating over all site URLs and checking if the current URL includes any of them.
  • Replaces the current site URL with the selected site URL and opens the new URL in a new tab.
javascript: (function () {
const sites = {
    "au-": "http://au-approval.example.com/portal/au/",
    "au": "https://www.example.com.au/portal/au/",
    "br-": "http://us-approval.example.com/portal/br/",
    "br": "https://www.example.com/portal/br/",
    "es-": "http://us-approval.example.com/portal/es/",
    "es": "https://www.example.com/portal/es/",
    "fr-": "http://us-approval.example.com/portal/fr/",
    "fr": "https://www.example.com/portal/fr/",
    "in-": "http://us-approval.example.com/portal/in/",
    "in": "https://www.example.com/portal/in/",
    "id-": "http://us-approval.example.com/portal/id/",
    "id": "https://www.example.com/portal/id/",
    "it-": "http://us-approval.example.com/portal/it/",
    "it": "https://www.example.com/portal/it/",
    "nz-": "http://us-approval.example.com/portal/nz/",
    "nz": "https://www.example.com/portal/nz/",
    "th-": "http://us-approval.example.com/portal/th/",
    "th": "https://www.example.com/portal/th/",
    "vn-": "http://us-approval.example.com/portal/vn/",
    "vn": "https://www.example.com/portal/vn/",
    "cn-": "http://cn-approval.example.com/",
    "cn": "https://www.example.cn/",
    "eu-": "http://eu-approval.example.com/",
    "eu": "https://www.example.eu/",
    "de-": "http://de-approval.example.com/portal/de/",
    "de": "https://www.example.de/portal/de/",
    "hk-": "http://hk-approval.example.com/portal/hk/",
    "hk": "https://www.example.com.hk/portal/hk/",
    "jp-": "http://jp-approval.example.com/",
    "jp": "https://www.example.co.jp/",
    "mx-": "http://mx-approval.example.com/portal/mx/",
    "mx": "https://www.example.com.mx/portal/mx/",
    "sg-": "http://sg-approval.example.com/portal/sg/",
    "sg": "https://www.example.com.sg/portal/sg/",
    "uk-": "http://uk-approval.example.com/portal/uk/",
    "uk": "https://www.example.co.uk/portal/uk/",
    "us-": "http://us-approval.example.com/portal/",
    "us": "https://www.example.com/portal/",
};

const currentUrl = window.location.href;
const selectedSite = prompt("Enter the desired site:");
const selectedSiteUrl = sites[selectedSite];

if (selectedSiteUrl) {
    const currentSiteUrl = Object.values(sites).find(url => currentUrl.includes(url));
    const changedUrl = currentUrl.replace(currentSiteUrl, selectedSiteUrl);
    window.open(changedUrl, '_blank');
}

})();

V1

  • Uses an array of objects to store site information, where each object has a name and domain property.
  • Finds the selected site object by matching the name property entered by the user.
  • Finds the current site object by checking if the current URL includes any of the site domains.
  • Replaces the current site domain in the URL with the selected site domain and opens the new URL in a new tab.
javascript: (function() {
    const sites = [{
        name: "au-",
        domain: "http://au-approval.example.com/portal/au/",
    }, {
        name: "au",
        domain: "https://www.example.com.au/portal/au/",
    }, {
        name: "br-",
        domain: "http://us-approval.example.com/portal/br/",
    }, {
        name: "br",
        domain: "https://www.example.com/portal/br/",
    }, {
        name: "es-",
        domain: "http://us-approval.example.com/portal/es/",
    }, {
        name: "es",
        domain: "https://www.example.com/portal/es/",
    }, {
        name: "fr-",
        domain: "http://us-approval.example.com/portal/fr/",
    }, {
        name: "fr",
        domain: "https://www.example.com/portal/fr/",
    }, {
        name: "in-",
        domain: "http://us-approval.example.com/portal/in/",
    }, {
        name: "in",
        domain: "https://www.example.com/portal/in/",
    }, {
        name: "id-",
        domain: "http://us-approval.example.com/portal/id/",
    }, {
        name: "id",
        domain: "https://www.example.com/portal/id/",
    }, {
        name: "it-",
        domain: "http://us-approval.example.com/portal/it/",
    }, {
        name: "it",
        domain: "https://www.example.com/portal/it/",
    }, {
        name: "nz-",
        domain: "http://us-approval.example.com/portal/nz/",
    }, {
        name: "nz",
        domain: "https://www.example.com/portal/nz/",
    }, {
        name: "th-",
        domain: "http://us-approval.example.com/portal/th/",
    }, {
        name: "th",
        domain: "https://www.example.com/portal/th/",
    }, {
        name: "vn-",
        domain: "http://us-approval.example.com/portal/vn/",
    }, {
        name: "vn",
        domain: "https://www.example.com/portal/vn/",
    }, {
        name: "cn-",
        domain: "http://cn-approval.example.com/",
    }, {
        name: "cn",
        domain: "https://www.example.cn/",
    }, {
        name: "eu-",
        domain: "http://eu-approval.example.com/",
    }, {
        name: "eu",
        domain: "https://www.example.eu/",
    }, {
        name: "de-",
        domain: "http://de-approval.example.com/portal/de/",
    }, {
        name: "de",
        domain: "https://www.example.de/portal/de/",
    }, {
        name: "hk",
        domain: "https://www.example.com.hk/portal/hk/",
    }, {
        name: "hk-",
        domain: "http://hk-approval.example.com/portal/hk/",
    }, {
        name: "jp-",
        domain: "http://jp-approval.example.com/",
    }, {
        name: "jp",
        domain: "https://www.example.co.jp/",
    }, {
        name: "mx-",
        domain: "http://mx-approval.example.com/portal/mx/",
    }, {
        name: "mx",
        domain: "https://www.example.com.mx/portal/mx/",
    }, {
        name: "sg-",
        domain: "https://sg-approval.example.com/portal/sg/",
    }, {
        name: "sg",
        domain: "https://www.example.com.sg/portal/sg/",
    }, {
        name: "uk-",
        domain: "http://uk-approval.example.com/portal/uk/",
    }, {
        name: "uk",
        domain: "https://www.example.co.uk/portal/uk/",
    }, {
        name: "us-",
        domain: "http://us-approval.example.com/portal/",
    }, {
        name: "us",
        domain: "https://www.example.com/portal/",
    }, ];
    const currentUrl = window.location.href;
    const selectedSite = prompt("Enter the desired site:");
    const selectedSiteObj = sites.find(site => site.name === selectedSite);
    if (selectedSiteObj) {
        const currentSiteObj = sites.find(site => currentUrl.includes(site.domain));
        const changedUrl = currentUrl.replace(currentSiteObj.domain, selectedSiteObj.domain);
        window.open(changedUrl, '_blank');
    }
})();

Key differences

  • V1 uses an array of objects, while V2 uses an object literal.
  • V1 uses object properties for site information, while V2 uses the site code as the key.
  • V1 iterates over all sites to find the current site, while V2 uses Object.values() to get an array of all URLs and then finds a match.

SEARCH