我试图将塞尔维亚维基百科从西里尔(Cyrillic(重定向到拉丁文脚本。
因此,问题是,当您转到塞尔维亚维基百科(Serbian Wikipedia(上的一些文章时,您将获得西里尔(Cyrillic(,拉丁文或混合脚本。我希望它仅在拉丁语中。
例如,默认链接是:
  https://sr.wikipedia.org/wiki/Србиј�а
我希望它可以拉丁语,所以它将成为:
  https://sr.wikipedia.org/sr-el/Ср�биј�а
(请参阅差异,从/wiki/
到/sr-el/
?(
还有另外两种可能的链接类型(子路径(:
- https://sr.wikipedia.org/sr/...
- https://sr.wikipedia.org/sr-ec/...
我的想法是将每个(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
。
使用标准重定向模式:
// ==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);
}