User:Wctaiwan/rewritePathVariant.js:修订间差异
外观
删除的内容 添加的内容
exclude logged in users |
小无编辑摘要 标签:已被回退 |
||
第9行: | 第9行: | ||
'use strict'; |
'use strict'; |
||
if (mw. |
if (!mw.user.isAnon()) { |
||
return; // Don't run this on logged-in users |
return; // Don't run this on logged-in users |
||
} |
} |
2022年2月14日 (一) 00:24的版本
/**
* 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';
if (!mw.user.isAnon()) {
return; // Don't run this on logged-in users
}
// Matches /zh-*/Foo and the variant query parameter in the current URL
var matchedPath = window.location.pathname.match(/^\/(zh-\w+)\//)
|| window.location.search.match(/[&?]variant=(zh-\w+)/);
if (matchedPath == null) {
return; // Path doesn't start with /zh-*/ or have a variant query paramter
}
var variant = matchedPath[1];
if (variant !== mw.config.get('wgUserVariant')) {
return; // Path variant doesn't match actual variant
}
// Rewrite /wiki/Foo links to /zh-*/Foo
// mousedown covers all click events (left/middle/right)
// touchstart is needed to handle "Open in Background" in Safari on iOS
$(document).on('mousedown 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('mousedown 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);