LLWiki正在建设中,欢迎加入我们

“MediaWiki:Gadget-site-shared.js”的版本间差异

来自LLWiki
跳转到导航 跳转到搜索
(// 使用Wikiplus小工具快速编辑)
 
(未显示同一用户的64个中间版本)
第3行: 第3行:
/**
/**
* @Description: 这里是桌面版和手机版通用的全局JS
* @Description: 这里是桌面版和手机版通用的全局JS
* @Functions: 1. 防止错误创建其他用户的用户页
* 2. 正确显示[[特殊:前缀索引/LLWiki:首页/当年今日/]]
* 3. 分类栏正确显示小写标题
* 4. 分类栏正确显示词曲作者的日文名
* 5. 触摸屏将packed-hover模式的gallery替换为packed
* 6. 移除Widget造成的多余<p>标签
* 7. 禁止使用InPageEdit快速重定向
* @Dependencies: mediawiki.Title, ext.gadget.site-lib
* @Author: 如无特殊说明,均为[[User:Bhsd]]
* @Author: 如无特殊说明,均为[[User:Bhsd]]
*/
*/
"use strict";
"use strict";
/*global OO, wgULS, wgUCS*/
/* global wgULS */
const pagename = mw.config.get('wgPageName'),
const pagename = mw.config.get( 'wgPageName' ),
specialPage = mw.config.get( 'wgCanonicalSpecialPageName' ),
action = mw.config.get( 'wgAction' ),
action = mw.config.get('wgAction'),
hook = mw.hook( 'wikipage.content' );
editable = mw.config.get( 'wgIsProbablyEditable' ),
username = mw.config.get('wgUserName'),
$body = $('body'),
uri = new mw.Uri();
mw.messages.set( wgUCS({'gadget-ss-sandbox': '欢迎来到沙盒!您可以在此进行编辑测试。', 'gadget-ss-request': '编辑请求_-_'},
{'gadget-ss-sandbox': '歡迎來到沙盒!您可以在此進行編輯測試。', 'gadget-ss-request': '編輯請求_-_'}) );


/**
/**
* @Function: 去掉地址栏最后"/"
* @Function: 点击其他用户主页面红链不会进入创建页面
* @Dependencies: mediawiki.util, mediawiki.Uri
* @Dependencies: mediawiki.Title
* @Author: [[wikipedia:User:Majavah]]
* @EditedBy: [[User:Bhsd]]
*/
*/
hook.add(function($content) { // 必须立即解决,否则手机版会生成drawer
if (pagename.endsWith( '/' ) && specialPage != 'Prefixindex') {
console.log('Hook: wikipage.content, 开始处理其他用户主页面的红链');
delete uri.query.title;
$content.find( '.new' ).attr('href', function(_, val) {
const query = uri.getQueryString();
const query = mw.util.getParamValue('title', val);
// 最稳健的短地址方案
if (!query) { return; } // 特殊页面
location.replace('/zh/' + mw.util.wikiUrlencode( pagename.slice(0, -1) ) + (query ? '?' + query : ''));
const title = new mw.Title( query ),
}
name = title.getMainText(); // 借助mediawiki.Title规范用户名格式
// 不处理非用户空间或用户子页面
if (title.namespace != 2 || name.includes( '/' ) || name == mw.config.get('wgUserName')) { return; }
return title.getUrl();
});
});


/**
/**
* @Function: 正确显示[[特殊:前缀索引/LLWiki:首页/当年今日/]]
* @Function: 生成沙盒顶部
* @Dependencies: mediawiki.Uri
*/
*/
// 非管理员不需要关心未创建的页面
// 即使是最新版本,在差异模式下也不会添加沙盒顶部
if (['LLWiki:沙盒', 'Template:Sandbox'].includes(pagename) && action == 'view' &&
if (pagename.startsWith( 'LLWiki:首页/当年今日/' ) && action == 'view' &&
(mw.config.get( 'wgArticleId' ) > 0 || mw.config.get( 'wgUserGroups' ).includes( 'sysop' ))) {
!(uri.query.diff || uri.query.oldid)) {
const download = function() {
$('#mw-content-text').prepend( '<table class="infobox" style="background-color:#f3f5f7;width:70%;border-left-color:#c5c5c5" cellpadding="2" align="center">' +
if ($('#mainpage-style').length) { return; } // 避免不必要的下载
'<tbody><tr>' +
console.log('Hook: wikipage.content, 开始下载主页样式表');
'<td width="15%">' +
mw.loader.load( '//fastly.jsdelivr.net/gh/bhsd-harry/LLWiki@1.4/widget/mainpage.min.css', 'text/css' );
'<img src="/mediawiki/img_auth.php/0/04/Bandeau-avertisement-3.png" width="50" height="50">' +
hook.remove( download );
'</td>' +
};
'<td><span style="color:">' + mw.msg( 'gadget-ss-sandbox' ) + '</span></td>' +
hook.add( download )
'</tr></tbody>' +
.add(function($content) { $content.children( '.mw-parser-output' ).addClass( 'mainpage-flex' ); });
'</table>' );
}
}


const lcCats = ["Μ's", 'Lily white', 'JQuery'];
/**
mw.hook( 'wikipage.categories' ).add(function($content) {
* @Function: 更复杂的快速插入
/**
* @Source: [[mw:Extension:CharInsert]]
* @Function: 分类栏显示小写标题
* @Dependencies: jquery.textSelection(已由CharInsert扩展加载)
*/
*/
console.log('Hook: wikipage.categories, 开始替换小写分类');
if (['edit', 'submit'].includes( action ) && editable) {
$body.on('click', 'span.mw-charinsert-item', function() {
lcCats.forEach(function(ele) {
const corrected = ele[0].toLowerCase() + ele.slice(1);
const $this = $(this);
$content.find('a:contains(' + ele + ')').text(function(_, text) {
$('#wpTextbox1').textSelection( 'encapsulateSelection', {
return text.replace( RegExp('^' + ele), corrected ); // jshint ignore: line
pre: $this.data( 'mw-charinsert-start' ) || $this.data( 'start' ), // undefined也没关系
});
post: $this.data( 'mw-charinsert-end' ) || $this.data( 'end' )
} );
});
}).add(function($content) {
});
/**
}
* @Function: 分类栏显示词曲作者的日文名
*/
if (mw.config.get( 'wgNamespaceNumber' ) !== 0) { return; }
console.log('Hook: wikipage.categories, 开始替换词曲作者分类');
$content.children( '#mw-normal-catlinks' ).find('a')
.filter(function() { return this.textContent.endsWith( '作' ); }).attr('lang', 'ja')
.text(function() { return this.title.slice(9); });
});


/**
/**
* @Function: 生成完整编辑请求链接
* @Function: 触摸屏代替galleryhover效果
* @Dependencies: mediawiki.Uri
* @Source: mediawiki.page.gallery
*/
*/
if (window.ontouchstart !== undefined ||
const $editRequest = $('.editRequest > a');
window.DocumentTouch !== undefined && document instanceof window.DocumentTouch) {
if (['edit', 'submit'].includes(action) && !editable && username && $editRequest.length) {
$editRequest.attr('href', function(i, val) {
hook.add(function($content) {
console.debug('Hook: wikipage.content, 开始移除gallery的hover效果');
return new mw.Uri( val ).extend({ preloadtitle: mw.msg('gadget-ss-request') +
$content.find( 'ul.mw-gallery-packed-hover ' ).addClass( 'mw-gallery-packed' )
// 不能出现空格,否则会被转换为+
.removeClass( 'mw-gallery-packed-hover' );
username.replaceAll(' ', '_') + '_(' + new Date().toISOString().slice(0, 10) + ')'
});
}).toString();
});
}
}


/**
/**
* @Function: 嵌入外部站点时进行提示
* @Function: 移除Widget造成的多余<p>标签
* @Dependencies: oojs-ui-core, oojs-ui-windows, ext.gadget.site-lib
* @Source: [[moegirl:mediawiki:common.js]]
* @EditedBy: [[User:Bhsd]]
*/
*/
hook.add(function($content) {
if (top !== window || location.host !== 'llwiki.org') {
$content.find( 'p:has(script, style, link)' ).after(function() {
mw.messages.set( wgULS({
return $(this).children( 'script, style, link' );
'gadget-ss-title': '<p>LLWiki提醒您</p><p>您正在非LLWiki域名访问</p>',
}).filter( ':empty' ).remove();
'gadget-ss-label': '<p>请注意不要在此域名下输入您的用户名或密码,以策安全!</p><p>LLWiki的域名为 llwiki.org</p>',
'gadget-ss-jump': '前往LLWiki', 'gadget-ss-continue': '我知道了'
}, {
'gadget-ss-title': '<p>LLWiki提醒您</p><p>您正在非LLWiki域名訪問</p>',
'gadget-ss-label': '<p>請注意不要在此域名下輸入您的用戶名或密碼,以策安全!</p><p>LLWiki的域名為 llwiki.org</p>',
'gadget-ss-jump': '前往LLWiki', 'gadget-ss-continue': '我知道了'
}) );
const dialog = new OO.ui.MessageDialog(),
actionP = new OO.ui.ActionWidget({label: mw.msg('gadget-ss-jump'), flags: ['primary', 'progressive'],
href: top === window ? 'https://llwiki.org' : location.href}),
actionC = new OO.ui.ActionWidget({label: mw.msg('gadget-ss-continue'), flags: ['primary', 'destructive']});
mw.dialog(dialog, [actionC, actionP], mw.msg('gadget-ss-label'), mw.msg('gadget-ss-title'));
}

mw.hook( 'wikipage.content' ).add(function($content) {
/**
* @Function: 点击其他用户主页面的红链不会进入创建页面
* @Dependencies: mediawiki.util, mediawiki.Title
*/
console.log('Hook: wikipage.content, 开始处理其他用户主页面的红链');
$content.find( '.new' ).attr('href', function(i, val) {
const query = mw.util.getParamValue('title', val);
if (!query) { return; } // 特殊页面
const title = new mw.Title( query ), // 借助mediawiki.Title规范用户名格式
name = title.getMainText();
// 不处理非用户空间或用户子页面
if (title.namespace != 2 || name.includes( '/' ) || name == username) { return; }
return title.getUrl();
});
});
});


/**
/**
* @Function: 分类栏显示小写标题
* @Function: 禁止使用InPageEdit快速重定向
* @Dependecies: ext.gadget.site-lib
*/
*/
mw.messages.set('gadget-ipe-warn', wgULS(
mw.hook( 'wikipage.categories' ).add(function($content) {
'由于InPageEdit小工具无法正确判别页面是否已存在,LLWiki暂时限制该小工具的使用。',
console.log('Hook: wikipage.categories, 开始替换小写分类');
'由於InPageEdit小工具無法正確判別頁面是否已存在,LLWiki暫時限制該小工具的使用。'
$content.find( 'a:contains("Μ\'s")' ).text(function(i, text) { return text.replace("Μ's", "μ's"); });
));
$content.find( 'a:contains("Lily white")' ).text(function(i, text) { return text.replace('Lily', 'lily'); });
const noIPE = function() {
});
mw.notify( mw.msg( 'gadget-ipe-warn' ), {type: 'warn', autoHide: false, tag: 'InPageEdit'} );
};
mw.hook( 'InPageEdit.quickRedirect' ).add( noIPE );
mw.hook( 'InPageEdit.quickRename' ).add( noIPE );

//</nowiki>
//</nowiki>
// [[category:桌面版小工具]] [[category:手机版小工具]] [[category:作为模块的小工具]] [[category:系统工具]] [[category:不可关闭的小工具]]
// [[category:桌面版小工具]] [[category:手机版小工具]] [[category:作为模块的小工具]] [[category:系统工具]] [[category:不可关闭的小工具]]

2022年5月18日 (三) 09:44的最新版本

//<nowiki>
// 由ResourceLoader直接调用,不可使用ES6语法
/**
 * @Description: 这里是桌面版和手机版通用的全局JS
 * @Functions: 1. 防止错误创建其他用户的用户页
 *			 2. 正确显示[[特殊:前缀索引/LLWiki:首页/当年今日/]]
 *			 3. 分类栏正确显示小写标题
 *			 4. 分类栏正确显示词曲作者的日文名
 *			 5. 触摸屏将packed-hover模式的gallery替换为packed
 *			 6. 移除Widget造成的多余<p>标签
 *			 7. 禁止使用InPageEdit快速重定向
 * @Dependencies: mediawiki.Title, ext.gadget.site-lib
 * @Author: 如无特殊说明,均为[[User:Bhsd]]
 */
"use strict";
/* global wgULS */
const pagename = mw.config.get( 'wgPageName' ),
	action = mw.config.get( 'wgAction' ),
	hook = mw.hook( 'wikipage.content' );

/**
 * @Function: 点击其他用户主页面的红链不会进入创建页面
 * @Dependencies: mediawiki.Title
 */
hook.add(function($content) { // 必须立即解决,否则手机版会生成drawer
	console.log('Hook: wikipage.content, 开始处理其他用户主页面的红链');
	$content.find( '.new' ).attr('href', function(_, val) {
		const query = mw.util.getParamValue('title', val);
		if (!query) { return; } // 特殊页面
		const title = new mw.Title( query ),
			name = title.getMainText(); // 借助mediawiki.Title规范用户名格式
		// 不处理非用户空间或用户子页面
		if (title.namespace != 2 || name.includes( '/' ) || name == mw.config.get('wgUserName')) { return; }
		return title.getUrl();
	});
});

/**
 * @Function: 正确显示[[特殊:前缀索引/LLWiki:首页/当年今日/]]
 */
// 非管理员不需要关心未创建的页面
if (pagename.startsWith( 'LLWiki:首页/当年今日/' ) && action == 'view' &&
	(mw.config.get( 'wgArticleId' ) > 0 || mw.config.get( 'wgUserGroups' ).includes( 'sysop' ))) {
	const download = function() {
		if ($('#mainpage-style').length) { return; } // 避免不必要的下载
		console.log('Hook: wikipage.content, 开始下载主页样式表');
		mw.loader.load( '//fastly.jsdelivr.net/gh/bhsd-harry/LLWiki@1.4/widget/mainpage.min.css', 'text/css' );
		hook.remove( download );
	};
	hook.add( download )
		.add(function($content) { $content.children( '.mw-parser-output' ).addClass( 'mainpage-flex' ); });
}

const lcCats = ["Μ's", 'Lily white', 'JQuery'];
mw.hook( 'wikipage.categories' ).add(function($content) {
	/**
	 * @Function: 分类栏显示小写标题
	 */
	console.log('Hook: wikipage.categories, 开始替换小写分类');
	lcCats.forEach(function(ele) {
		const corrected = ele[0].toLowerCase() + ele.slice(1);
		$content.find('a:contains(' + ele + ')').text(function(_, text) {
			return text.replace( RegExp('^' + ele), corrected ); // jshint ignore: line
		});
	});
}).add(function($content) {
	/**
	 * @Function: 分类栏显示词曲作者的日文名
	 */
	if (mw.config.get( 'wgNamespaceNumber' ) !== 0) { return; }
	console.log('Hook: wikipage.categories, 开始替换词曲作者分类');
	$content.children( '#mw-normal-catlinks' ).find('a')
		.filter(function() { return this.textContent.endsWith( '作' ); }).attr('lang', 'ja')
		.text(function() { return this.title.slice(9); });
});

/**
 * @Function: 触摸屏代替gallery的hover效果
 * @Source: mediawiki.page.gallery
 */
if (window.ontouchstart !== undefined ||
	window.DocumentTouch !== undefined && document instanceof window.DocumentTouch) {
	hook.add(function($content) {
		console.debug('Hook: wikipage.content, 开始移除gallery的hover效果');
		$content.find( 'ul.mw-gallery-packed-hover ' ).addClass( 'mw-gallery-packed' )
			.removeClass( 'mw-gallery-packed-hover' );
	});
}

/**
 * @Function: 移除Widget造成的多余<p>标签
 */
hook.add(function($content) {
	$content.find( 'p:has(script, style, link)' ).after(function() {
		return $(this).children( 'script, style, link' );
	}).filter( ':empty' ).remove();
});

/**
 * @Function: 禁止使用InPageEdit快速重定向
 * @Dependecies: ext.gadget.site-lib
 */
mw.messages.set('gadget-ipe-warn', wgULS(
	'由于InPageEdit小工具无法正确判别页面是否已存在,LLWiki暂时限制该小工具的使用。',
	'由於InPageEdit小工具無法正確判別頁面是否已存在,LLWiki暫時限制該小工具的使用。'
));
const noIPE = function() {
	mw.notify( mw.msg( 'gadget-ipe-warn' ), {type: 'warn', autoHide: false, tag: 'InPageEdit'} );
};
mw.hook( 'InPageEdit.quickRedirect' ).add( noIPE );
mw.hook( 'InPageEdit.quickRename' ).add( noIPE );

//</nowiki>
// [[category:桌面版小工具]] [[category:手机版小工具]] [[category:作为模块的小工具]] [[category:系统工具]] [[category:不可关闭的小工具]]
// {{DEFAULTSORT:site-shared.js}}