跳转到内容

User:Wctaiwan/rewritePathVariant.js:修订间差异

维基百科,自由的百科全书
删除的内容 添加的内容
Wctaiwan留言 | 贡献
无编辑摘要
Wctaiwan留言 | 贡献
switch to mousedown
第22行: 第22行:


// Rewrite /wiki/Foo links to /zh-*/Foo
// 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
// touchstart is needed to handle "Open in Background" in Safari on iOS
$(document).on('click contextmenu touchstart', 'a[href^="/wiki/"]', function() {
$(document).on('mousedown touchstart', 'a[href^="/wiki/"]', function() {
$(this).attr('href', function (_, originalPath) {
$(this).attr('href', function (_, originalPath) {
return originalPath.replace(/^\/wiki\//, '/' + variant + '/');
return originalPath.replace(/^\/wiki\//, '/' + variant + '/');
第30行: 第31行:


// Append &variant=zh-* to /zhwiki/w/index.php links
// Append &variant=zh-* to /zhwiki/w/index.php links
$(document).on('click contextmenu touchstart', 'a[href^="/zhwiki/w/index.php?"]', function() {
$(document).on('mousedown touchstart', 'a[href^="/zhwiki/w/index.php?"]', function() {
$(this).attr('href', function (_, originalPath) {
$(this).attr('href', function (_, originalPath) {
if (originalPath.indexOf('variant=') !== -1) {
if (originalPath.indexOf('variant=') !== -1) {

2022年1月11日 (二) 15:20的版本

/**
 * 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-\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);