I am trying to redirect Serbian Wikipedia from Cyrillic to Latinic script.
So the problem is this, when you go to some article on the Serbian Wikipedia you will either get Cyrillic, Latinic or mixed script. I want it to be only in Latinic.
For example, default link is:
https://sr.wikipedia.org/wiki/%D0%A1%D1%80%D0%B1%D0%B8%D1%98%D0%B0
I want it to Latinic, so it will become:
https://sr.wikipedia.org/sr-el/%D0%A1%D1%80%D0%B1%D0%B8%D1%98%D0%B0
(See the difference, from /wiki/
to /sr-el/
?)
There are also two more possible link types (subpaths):
My idea is to make each (wiki, sr and sr-el) redirect to sr-el.
I tried doing it like this, but I got no result:
// ==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);
}
Can you help me?
That code tries to test a partial path against location.host
. That won't work.
Also, elseif
is not valid in javascript. It would be else if
.
Use a 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);
}