LLWiki正在建设中,欢迎加入我们!
“MediaWiki:Gadget-MobileCategories.js”的版本间差异
跳转到导航
跳转到搜索
小 |
标签:撤销 |
||
(未显示2个用户的31个中间版本) | |||
第1行: | 第1行: | ||
//<nowiki> |
|||
// 引自[[wikipedia:mediawiki:gadget-MobileCategories.js]] |
|||
// 由ResourceLoader直接调用,不可使用ES6语法 |
|||
mw.hook( 'wikipage.content' ).add( function() { |
|||
/** |
|||
if(mw.config.get('skin') == 'vector') { return; } |
|||
* @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' ), |
|||
api = new mw.Api(), |
|||
pageViewLang = mw.config.get( 'wgPageContentLanguage' ); // This variable was misnamed |
|||
apiRequest = api.get( { |
|||
if ( pageViewLang == 'en' ) { return; } |
|||
'action':'query', |
|||
'titles': mw.config.get('wgPageName'), |
|||
'prop': 'categories', |
|||
'clshow': (showhiddencats ? '!hidden|hidden' : '!hidden'), |
|||
'clprop': 'hidden', |
|||
'cllimit': 50, |
|||
'indexpageids': true, |
|||
'formatversion': 2 |
|||
} ), |
|||
apiRequest2 = api.loadMessagesIfMissing( [ 'pagecategories', 'colon-separator' ] ); |
|||
function trimNamespace( title ) { |
|||
$.when( apiRequest, apiRequest2 ).done( function( data ) { |
|||
return title.slice( title.indexOf( ':' ) + 1 ); |
|||
var categories = [], |
|||
} |
|||
hiddenCats = [], |
|||
new mw.Api().get( { |
|||
response1Data = data[0], |
|||
"action": "query", |
|||
response2Data = data[1]; |
|||
"format": "json", |
|||
"formatversion": "2", |
|||
if( !response1Data.query.pages[0].categories ) { return; } |
|||
"generator": "categories", |
|||
"gcllimit": "max", |
|||
function categoryHtmlGenerator(element, index, array) { |
|||
"prop": "info|categoryinfo", |
|||
var title = new mw.Title( element.title, mw.config.get( 'wgNamespaceIds' ).file ); |
|||
"inprop": "varianttitles", |
|||
if( !element.hidden ) { |
|||
"titles": pageName, |
|||
categories.push( $('<li>').append( |
|||
} ).then( function ( res ) { |
|||
$( '<a>' ).attr( 'href', mw.util.getUrl( element.title ) ) |
|||
const categories = { 'normal': [], 'hidden': [] }, convertedCats = {}; |
|||
.attr( 'title', title ) |
|||
if ( !res.query ) { return; } |
|||
.append( document.createTextNode( title.getMainText() ) ) |
|||
res.query.pages.forEach( function ( ele ) { |
|||
).get( 0 ) |
|||
const type = ele.categoryinfo.hidden ? 'hidden' : 'normal', |
|||
); |
|||
title = trimNamespace( ele.title ); |
|||
} |
|||
categories[ type ].push( title ); |
|||
else if(showhiddencats) { |
|||
convertedCats[ title ] = trimNamespace( ele.varianttitles[ pageViewLang ] ); |
|||
hiddenCats.push( $('<li>').append( |
|||
}); |
|||
$( '<a>' ).attr( 'href', mw.util.getUrl( element.title ) ) |
|||
mw.config.set( 'wgCategories', [].concat( categories.normal, categories.hidden ) ); |
|||
.attr( 'title', title ) |
|||
if (!categories.normal.length && !categories.hidden.length) { return; } |
|||
.append( document.createTextNode( title.getMainText() ) ) |
|||
).get( 0 ) |
|||
); |
|||
} |
|||
} |
|||
response1Data.query.pages[0].categories.forEach( categoryHtmlGenerator ); |
|||
const categoryHtmlGenerator = function(type) { |
|||
if( categories.length === 0 && hiddenCats.length === 0 ) { return; } |
|||
const cats = categories[ type ]; |
|||
return $('<ul>', {html: cats.map(function(cat) { |
|||
var normalCatlinks, hiddenCatlinks; |
|||
const page = 'Category:' + cat; |
|||
if(categories.length) { |
|||
return $('<a>', {href: mw.util.getUrl( page ), title: page, text: convertedCats[cat]}).wrap( '<li>' ).parent(); |
|||
normalCatlinks = $( '<div>' ) |
|||
})}); |
|||
.attr( 'id', 'mw-normal-catlinks' ) |
|||
}, |
|||
.addClass('mw-normal-catlinks') |
|||
$catlinks = $('<div>', {class: 'catlinks', id: 'catlinks'}).insertAfter( '.printfooter' ); |
|||
.append( $( '<ul>' ).append( categories ) ) |
|||
if (categories.normal.length) { |
|||
.prepend( '<a href="/zh/Special:Categories" title="Special:页面分类">' + mw.message( 'pagecategories' ).escaped() + '</a>' + mw.message( 'colon-separator' ).escaped() ); |
|||
$('<div>', {id: 'mw-normal-catlinks', class: 'mw-normal-catlinks', html: [ |
|||
} |
|||
$('<a>', {href: '/zh/Special:页面分类', title: 'Special:页面分类', text: mw.msg( 'pagecategories' )}), |
|||
if(hiddenCats.length) { |
|||
mw.msg( 'colon-separator' ), |
|||
hiddenCatlinks = $( '<div>' ) |
|||
categoryHtmlGenerator( 'normal' ) |
|||
.attr( 'id', 'mw-hidden-catlinks' ) |
|||
]}).appendTo( $catlinks ); |
|||
.addClass(['mw-hidden-catlinks', 'mw-hidden-cats-user-shown']) |
|||
} |
|||
.append( $( '<ul>' ).append( hiddenCats ) ) |
|||
if (categories.hidden.length && showhiddencats) { |
|||
.prepend( wgULS("隐藏分类", "隱藏分類") + mw.message( 'colon-separator' ).escaped() ); |
|||
$('<div>', {id: 'mw-hidden-catlinks', class: 'mw-hidden-catlinks mw-hidden-cats-user-shown', html: [ |
|||
} |
|||
mw.msg( 'hidden-categories' ) + mw.msg( 'colon-separator' ), |
|||
var catlinks = $( '<div>' ) |
|||
categoryHtmlGenerator( 'hidden' ) |
|||
.addClass( 'catlinks' ) |
|||
]}).appendTo( $catlinks ); |
|||
} |
|||
.data( 'mw', 'interface' ); |
|||
mw.hook( 'wikipage.categories' ).fire( $catlinks ); |
|||
if(normalCatlinks) { catlinks.append(normalCatlinks); } |
|||
} ); |
|||
if(hiddenCatlinks) { catlinks.append(hiddenCatlinks); } |
|||
}); |
|||
$('.printfooter').after(catlinks); |
|||
//</nowiki> |
|||
} ); |
|||
// [[category:手机版小工具]] [[category:分类工具]] [[category:作为模块的小工具]] |
|||
} ); |
|||
// {{DEFAULTSORT:MobileCategories.js}} |
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}}