如何通过用户脚本从多个可能的子路径重定向(Switch Wikipedia language/Script)?

How to Redirect from multiple possible subpaths via Userscript (Switch Wikipedia language/Script)?

我正在尝试将塞尔维亚维基百科从西里尔文重定向到拉丁文。

所以问题是,当您访问塞尔维亚维基百科上的某篇文章时,您会看到西里尔文、拉丁文或混合文字。我希望它只使用拉丁语。

例如,默认link是:
https://sr.wikipedia.org/wiki/%D0%A1%D1%80%D0%B1%D0%B8%D1%98%D0%B0

我想把它变成拉丁语,所以它会变成:
https://sr.wikipedia.org/sr-el/%D0%A1%D1%80%D0%B1%D0%B8%D1%98%D0%B0

(看到区别了,从/wiki//sr-el/?)

还有两种可能的 link 类型(子路径):

我的想法是让每个(wiki、sr 和 sr-el)都重定向到 sr-el。

我试过这样做,但没有结果:

// ==UserScript==
// @name     sr wiki latin
// @version  1
// @include     https://sr.wikipedia.org*
// @include     http://sr.wikipedia.org*
// @grant    none
// ==/UserScript==

var url = window.location.host;

if (url.match("sr.wikipedia.org/sr-el") === null) {
    url = window.location.href;
    if  (url.match("//sr.wikipedia.org/wiki") !== null){
        url = url.replace("//sr.wikipedia.org/wiki", "//sr.wikipedia.org/sr-el");
    } elseif (url.match("//sr.wikipedia.org/sr-ec") !== null){
        url = url.replace("//sr.wikipedia.org/sr-ec", "//sr.wikipedia.org/sr-el");
    } elseif (url.match("//sr.wikipedia.org/sr") !== null){
        url = url.replace("//sr.wikipedia.org/sr", "//sr.wikipedia.org/sr-el");
    } else
    {
        return;
    }

    console.log(url);
    window.location.replace(url);
}

你能帮帮我吗?

该代码尝试针对 location.host 测试部分路径。那不行。
此外,elseif 在 javascript 中无效。它将是 else if.

使用 standard redirect pattern:

// ==UserScript==
// @name        Wikipedia Serbian, always switch to latinic script
// @match       *://sr.wikipedia.org/*
// @run-at      document-start
// @grant       none
// ==/UserScript==
/* eslint-disable no-multi-spaces */

var oldUrlPath  = location.pathname;
if ( ! oldUrlPath.includes ("/sr-el/") ) {
    //-- Get and test path prefix...
    var pathParts = oldUrlPath.split ("/");
    if (pathParts.length > 1) {
        switch (pathParts[1]) {
            case "sr":
            case "sr-ec":
            case "wiki":
                pathParts[1] = "sr-el";
                break;
            default:
                // No action needed.
                break;
        }
    }
    var newPath = pathParts.join ("/");
    var newURL  = location.protocol + "//"
                + location.host
                + newPath
                + location.search
                + location.hash
                ;
    console.log ("Redirecting to: ", newURL);
    location.replace (newURL);
}