User:Wctaiwan/rewritePathVariant.js:修订间差异
外观
删除的内容 添加的内容
avoid ES2015 |
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(/^\/(zh- |
var matchedPath = window.location.pathname.match(/^\/(zh-(?:cn|hk|mo|my|sg|tw))\//) |
||
|| window.location.search.match(/[&?]variant=(zh- |
|| window.location.search.match(/[&?]variant=(zh-(?:cn|hk|mo|my|sg|tw))/); |
||
if (matchedPath == null) { |
if (matchedPath == null) { |
2021年12月18日 (六) 08:30的版本
/**
* 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 = 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);