User:Wctaiwan/rewritePathVariant.js:修订间差异
外观
删除的内容 添加的内容
make regex stricter |
无编辑摘要 |
||
第10行: | 第10行: | ||
// Matches /zh-*/Foo and the variant query parameter in the current URL |
// Matches /zh-*/Foo and the variant query parameter in the current URL |
||
var matchedPath = window.location.pathname.match(/^\/ |
var matchedPath = window.location.pathname.match(/^\/zh-(cn|hk|mo|my|sg|tw)\//) |
||
|| window.location.search.match(/[&?]variant= |
|| window.location.search.match(/[&?]variant=zh-(cn|hk|mo|my|sg|tw)/); |
||
if (matchedPath == null) { |
if (matchedPath == null) { |
||
第17行: | 第17行: | ||
} |
} |
||
var variant = matchedPath[1]; |
var variant = 'zh-' + matchedPath[1]; |
||
// Rewrite /wiki/Foo links to /zh-*/Foo |
// Rewrite /wiki/Foo links to /zh-*/Foo |
2021年12月18日 (六) 09:05的版本
/**
* Work around https://phabricator.wikimedia.org/T223053 by rewriting link URLs
*
* Thanks to User:Legoktm for the idea and for technical assistance, and to
* User:Jack Phoenix for reviewing the implementation.
*/
(function ($, mw) {
'use strict';
// Matches /zh-*/Foo and the variant query parameter in the current URL
var matchedPath = window.location.pathname.match(/^\/zh-(cn|hk|mo|my|sg|tw)\//)
|| window.location.search.match(/[&?]variant=zh-(cn|hk|mo|my|sg|tw)/);
if (matchedPath == null) {
return; // Path doesn't start with /zh-*/ or have a variant query paramter
}
var variant = 'zh-' + matchedPath[1];
// Rewrite /wiki/Foo links to /zh-*/Foo
// touchstart is needed to handle "Open in Background" in Safari on iOS
$(document).on('click contextmenu touchstart', 'a[href^="/wiki/"]', function() {
$(this).attr('href', function (_, originalPath) {
return originalPath.replace(/^\/wiki\//, '/' + variant + '/');
});
});
// Append &variant=zh-* to /zhwiki/w/index.php links
$(document).on('click contextmenu touchstart', 'a[href^="/zhwiki/w/index.php?"]', function() {
$(this).attr('href', function (_, originalPath) {
if (originalPath.indexOf('variant=') !== -1) {
return originalPath; // Already has variant query parameter
}
return originalPath + '&variant=' + variant;
});
});
})(jQuery, mediaWiki);