How to make a switch out based on location.href?

93 Views Asked by At

I've got like 25 "if"s in my userscript for greasemonkey and now I would like to make a switch out of these.

It looks like this:

if (document.location.href.search("de.xyz.com") != -1) {
    var lng = "de";
    var gamepage = ".de.xyz.com";
    var reg = /http:\/\/s(\d+)\.de\.xyz\.com\/(.*?)\.php(.*)/i;
}

Times 25, the only difference is the language of the page, for example not "de" but "en"

Now, how to make a switch out of that? I thought of something like this?

var lng

switch(document.location.href.search((lng) + ".xyz.com")  != -1) {
    case lng = "de":
        var gamepage = ".de.xyz.com";
        var reg = /http:\/\/s(\d+)\.de\.xyz\.com\/(.*?)\.php(.*)/i;
}
1

There are 1 best solutions below

0
On

Use an array of objects and a for. Something like

var nlTranslationInformation = {
    key: "nl.xyz.com",
    lng: "uk",
    gamepage: ".nl.xyz.com",
    reg: /http:\/\/s(\d+)\.nl\.xyz\.com\/(.*?)\.php(.*)/i
};
var translationsInformation = [nlTranslationInformation, ...];
for (i = 0; i < translationsInformation.length; i++) {
    var currentTranslation = translationsInformation[i];
    if (document.location.href.search(currentTranslation.key) != -1) {
        var lng = currentTranslation.lng;
        var gamepage = currentTranslation.gamepage;
        var reg = currentTranslation.reg;
    }
}