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

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

来自LLWiki
跳转到导航 跳转到搜索
标签移动版网页编辑 移动版编辑
(撤销Func讨论)的版本57255)
标签撤销
 
(未显示2个用户的6个中间版本)
第12行: 第12行:


const showhiddencats = mw.user.options.get( 'showhiddencats' ),
const showhiddencats = mw.user.options.get( 'showhiddencats' ),
categories = mw.config.get( 'wgConvertedCategories' ),
pageName = mw.config.get( 'wgPageName' ),
pageViewLang = mw.config.get( 'wgPageContentLanguage' ); // This variable was misnamed
normal = categories.normal,
if ( pageViewLang == 'en' ) { return; }
hidden = showhiddencats ? categories.hidden : [];

if (normal.length === 0 && hidden.length === 0) { return; }
function trimNamespace( title ) {
return title.slice( title.indexOf( ':' ) + 1 );
const categoryHtmlGenerator = function(array) {
return $('<ul>', {html: array.map(function(cat) {
const page = mw.msg( 'pagecategories' ) + cat;
return $('<a>', {href: mw.util.getUrl( page ), title: page, text: cat}).wrap( '<li>' ).parent();
})});
},
$catlinks = $('<div>', {class: 'catlinks', id: 'catlinks'}).insertAfter( '.printfooter' );
if (normal.length) {
const specialCategories = 'Special:' + mw.msg( 'categories' );
$('<div>', {id: 'mw-normal-catlinks', class: 'mw-normal-catlinks', html: [
$('<a>', {href: '/zh/Special:Categories', title: specialCategories, text: mw.msg( 'pagecategories' )}),
mw.msg( 'colon-separator' ),
categoryHtmlGenerator( normal )
]}).appendTo( $catlinks );
}
}
if (hidden.length) {
new mw.Api().get( {
"action": "query",
$('<div>', {id: 'mw-hidden-catlinks', class: 'mw-hidden-catlinks mw-hidden-cats-user-shown', html: [
"format": "json",
mw.msg( 'hidden-categories' ) + mw.msg( 'colon-separator' ),
"formatversion": "2",
categoryHtmlGenerator( hidden )
"generator": "categories",
]}).appendTo( $catlinks );
"gcllimit": "max",
}
"prop": "info|categoryinfo",
mw.hook( 'wikipage.categories' ).fire( $catlinks );
"inprop": "varianttitles",
"titles": pageName,
} ).then( function ( res ) {
const categories = { 'normal': [], 'hidden': [] }, convertedCats = {};
if ( !res.query ) { return; }
res.query.pages.forEach( function ( ele ) {
const type = ele.categoryinfo.hidden ? 'hidden' : 'normal',
title = trimNamespace( ele.title );
categories[ type ].push( title );
convertedCats[ title ] = trimNamespace( ele.varianttitles[ pageViewLang ] );
});
mw.config.set( 'wgCategories', [].concat( categories.normal, categories.hidden ) );
if (!categories.normal.length && !categories.hidden.length) { return; }

const categoryHtmlGenerator = function(type) {
const cats = categories[ type ];
return $('<ul>', {html: cats.map(function(cat) {
const page = 'Category:' + cat;
return $('<a>', {href: mw.util.getUrl( page ), title: page, text: convertedCats[cat]}).wrap( '<li>' ).parent();
})});
},
$catlinks = $('<div>', {class: 'catlinks', id: 'catlinks'}).insertAfter( '.printfooter' );
if (categories.normal.length) {
$('<div>', {id: 'mw-normal-catlinks', class: 'mw-normal-catlinks', html: [
$('<a>', {href: '/zh/Special:页面分类', title: 'Special:页面分类', text: mw.msg( 'pagecategories' )}),
mw.msg( 'colon-separator' ),
categoryHtmlGenerator( 'normal' )
]}).appendTo( $catlinks );
}
if (categories.hidden.length && showhiddencats) {
$('<div>', {id: 'mw-hidden-catlinks', class: 'mw-hidden-catlinks mw-hidden-cats-user-shown', html: [
mw.msg( 'hidden-categories' ) + mw.msg( 'colon-separator' ),
categoryHtmlGenerator( 'hidden' )
]}).appendTo( $catlinks );
}
mw.hook( 'wikipage.categories' ).fire( $catlinks );
} );
});
});
//</nowiki>
//</nowiki>

2023年3月30日 (四) 23:41的最新版本

//<nowiki>
// 由ResourceLoader直接调用,不可使用ES6语法
/**
 * @Function: 移动版渲染分类栏
 * @Dependencies: mediawiki.util, user.options
 * @OriginalVersion: [[wikipedia:mediawiki:gadget-MobileCategories.js]]
 * @EditedBy: [[User:Bhsd]]
 */
"use strict";
$(function() {
	if (mw.config.get( 'wgAction' ) != 'view' || mw.config.get( 'wgArticleId' ) === 0) { return; }

	const showhiddencats = mw.user.options.get( 'showhiddencats' ),
		pageName = mw.config.get( 'wgPageName' ),
		pageViewLang = mw.config.get( 'wgPageContentLanguage' ); // This variable was misnamed
	if ( pageViewLang == 'en' ) { return; }

	function trimNamespace( title ) {
		return title.slice( title.indexOf( ':' ) + 1 );
	}
	new mw.Api().get( {
		"action": "query",
		"format": "json",
		"formatversion": "2",
		"generator": "categories",
		"gcllimit": "max",
		"prop": "info|categoryinfo",
		"inprop": "varianttitles",
		"titles": pageName,
	} ).then( function ( res ) {
		const categories = { 'normal': [], 'hidden': [] }, convertedCats = {};
		if ( !res.query ) { return; }
		res.query.pages.forEach( function ( ele ) {
			const type = ele.categoryinfo.hidden ? 'hidden' : 'normal',
				title = trimNamespace( ele.title );
			categories[ type ].push( title );
			convertedCats[ title ] = trimNamespace( ele.varianttitles[ pageViewLang ] );
		});
		mw.config.set( 'wgCategories', [].concat( categories.normal, categories.hidden ) );
		if (!categories.normal.length && !categories.hidden.length) { return; }

		const categoryHtmlGenerator = function(type) {
			const cats = categories[ type ];
			return $('<ul>', {html: cats.map(function(cat) {
				const page = 'Category:' + cat;
				return $('<a>', {href: mw.util.getUrl( page ), title: page, text: convertedCats[cat]}).wrap( '<li>' ).parent();
			})});
		},
			$catlinks = $('<div>', {class: 'catlinks', id: 'catlinks'}).insertAfter( '.printfooter' );
		if (categories.normal.length) {
			$('<div>', {id: 'mw-normal-catlinks', class: 'mw-normal-catlinks', html: [
				$('<a>', {href: '/zh/Special:页面分类', title: 'Special:页面分类', text: mw.msg( 'pagecategories' )}),
				mw.msg( 'colon-separator' ),
				categoryHtmlGenerator( 'normal' )
			]}).appendTo( $catlinks );
		}
		if (categories.hidden.length && showhiddencats) {
			$('<div>', {id: 'mw-hidden-catlinks', class: 'mw-hidden-catlinks mw-hidden-cats-user-shown', html: [
				mw.msg( 'hidden-categories' ) + mw.msg( 'colon-separator' ),
				categoryHtmlGenerator( 'hidden' )
			]}).appendTo( $catlinks );
		}
		mw.hook( 'wikipage.categories' ).fire( $catlinks );
	} );
});
//</nowiki>
// [[category:手机版小工具]] [[category:分类工具]] [[category:作为模块的小工具]]
// {{DEFAULTSORT:MobileCategories.js}}