LLWiki正在建设中,欢迎加入我们!
“MediaWiki:Gadget-HotCat-zh-core.js”的版本间差异
跳转到导航
跳转到搜索
小 |
小 (prefer zh-cn instead of zh-hans etc.) |
||
(未显示2个用户的24个中间版本) | |||
第1行: | 第1行: | ||
//<nowiki> |
|||
(function($, mw) { |
|||
/** |
|||
$(function() { |
|||
* @Function: 修改HotCat以识别繁简分类 |
|||
const ns = mw.config.get('wgNamespaceNumber'); |
|||
* @Contributors: [[User:Bhsd]]; [[User:葫芦又]] |
|||
if( mw.config.get('wgCanonicalSpecialPageName') === 'Upload' ) { |
|||
*/ |
|||
mw.loader.load('/zh?title=mediawiki:gadget-HotCat.js&action=raw&ctype=text/javascript'); |
|||
$(function() { |
|||
return; |
|||
const ns = mw.config.get('wgNamespaceNumber'), |
|||
} |
|||
mobileMode = mw.loader.getState( 'ext.gadget.MobileCategories' ) !== null; |
|||
if( ns < 0 && // Special pages; Special:Upload is handled differently |
|||
if ( mw.config.get('wgCanonicalSpecialPageName') !== 'Upload' && |
|||
ns === 10 || // Templates |
|||
(ns < 0 || // Special pages |
|||
ns === 10 || // Templates |
|||
ns === 274 || // Widget |
|||
ns === 828 || // Module (Lua) |
|||
ns === 6 && !mw.config.get('wgArticleId') || // Non-existing file pages |
|||
ns === 8 || // MediaWiki |
|||
ns === 2 || // User |
|||
ns % 2 === 1 || // Talk page |
|||
mw.config.get("wgPageContentModel") !== "wikitext" || |
|||
!mw.config.get('wgArticleId') || // Non-existing file pages |
|||
mw.config.get("wgPageContentModel") !== "wikitext" || |
|||
return; |
|||
mw.config.get('wgAction') != 'view' || |
|||
} |
|||
mw.config.get( 'wgIsRedirect' )) ) { |
|||
return; |
|||
var api = new mw.Api(), |
|||
} |
|||
request1 = api.get({action:'parse', page:pageName, prop:'wikitext'}), |
|||
if ( mobileMode ) { |
|||
request2 = api.get({action:'query', titles:pageName, prop:'categories'}); |
|||
mw.hook( 'wikipage.categories' ).add( main ); |
|||
$.when(request1, request2).then(function(data1, data2) { |
|||
} else { |
|||
const obj1 = data1[0].parse.wikitext["*"], |
|||
main(); |
|||
obj2 = data2[0].query.pages[mw.config.get( 'wgArticleId' )].categories; |
|||
} |
|||
var cats = [...obj1.matchAll(/\[\[(?:category|分类|分類):(.*?)(?:\|.*)?]]/ig)].map((ele) => ele[1]), |
|||
canonicalCats = ( obj2 ? Object.keys(obj2).map((key) => obj2[key].title) : [] ); |
|||
function main() { |
|||
if(canonicalCats.length === 0) { |
|||
const api = new mw.Api(), |
|||
mw.loader.load('/zh?title=mediawiki:gadget-HotCat.js&action=raw&ctype=text/javascript'); |
|||
categories = mw.config.get('wgCategories'), |
|||
return; |
|||
noVariants = categories.length === 0 || mobileMode, |
|||
} |
|||
canonicalCats = categories.map(ele => 'Category:' + ele).join('|'); |
|||
api.get({action:'query', |
|||
var request1 = api.get({ |
|||
prop:'info', |
|||
action: 'query', |
|||
inprop:'varianttitles', |
|||
titles: mw.config.get('wgPageName'), |
|||
indexpageids:1, |
|||
prop: 'revisions|pageprops', |
|||
titles:canonicalCats.join("|")}) |
|||
rvprop: 'content', |
|||
.then(function(data) { |
|||
rvslots: 'main', |
|||
const ids = data.query.pageids; |
|||
ppprop: 'defaultsort', |
|||
ids.forEach(function(ele) { |
|||
formatversion: 2, |
|||
var catlink = $('#catlinks a[title="' + data.query.pages[ele].title + '"]'); |
|||
}), request2 = noVariants && $.Deferred().resolve() || api.get({ |
|||
const catVt = data.query.pages[ele].varianttitles; |
|||
action:'query', |
|||
if(!catVt) { return; } |
|||
prop: 'info', |
|||
const catHans = catVt["zh-hans"].substring(3), |
|||
inprop: 'varianttitles', |
|||
catHant = catVt["zh-hant"].substring(3); |
|||
formatversion: 2, |
|||
if(cats.includes( catHans.replace(/ /g,'_') )) { |
|||
titles: canonicalCats, |
|||
catlink.attr('title','Category:' + catHans); |
|||
converttitles: 1 |
|||
catlink.attr('href','/zh/Category:' + catHans); |
|||
}); |
|||
} |
|||
$.when(request1, request2).then(function(data1, data2) { |
|||
else { |
|||
const pageInfo = data1[0].query.pages[0], |
|||
catlink.attr('title','Category:' + catHant); |
|||
obj1 = pageInfo.revisions[0].slots.main.content; |
|||
catlink.attr('href','/zh/Category:' + catHant); |
|||
window.hotcat_preload = obj1; |
|||
} |
|||
mw.config.set( 'wgDefaultSortKey', (pageInfo.pageprops || {}).defaultsort || '' ); |
|||
}); |
|||
mw.hook( 'gadget.hotcat.preloaded' ).fire(); |
|||
mw.loader.load('/zh?title=mediawiki:gadget-HotCat.js&action=raw&ctype=text/javascript'); |
|||
if ( noVariants ) { |
|||
}); |
|||
mw.loader.load('/zh?title=mediawiki:gadget-HotCat.js&action=raw&ctype=text/javascript'); |
|||
}); |
|||
return; |
|||
}); |
|||
} |
|||
}) (jQuery, mediaWiki); |
|||
const obj2 = data2[0].query.pages, |
|||
cats = [...obj1.matchAll(/\[\[\s*(?:category|分类|分類)\s*:\s*(.*?)(?:\|.*)?\s*]]/ig)] |
|||
.map( ele => ele[1].charAt(0).toUpperCase() + ele[1].slice(1).replace(/_/g, ' ') ); |
|||
obj2.forEach(function(ele) { |
|||
const catVt = ele.varianttitles; |
|||
if (!catVt) { return; } |
|||
const catHans = catVt['zh-cn'].substring(3), |
|||
catHant = catVt['zh-tw'].substring(3), |
|||
catlink = $('#catlinks a[title="Category:' + catHans + '"], #catlinks a[title="Category:' + catHant + '"]'); |
|||
if (catlink.length === 0) { return; } |
|||
if (cats.includes( catHans )) { |
|||
catlink.attr('title','Category:' + catHans); |
|||
catlink.attr('href','/zh/Category:' + catHans); |
|||
} else { |
|||
catlink.attr('title','Category:' + catHant); |
|||
catlink.attr('href','/zh/Category:' + catHant); |
|||
} |
|||
}); |
|||
mw.loader.load('/zh?title=mediawiki:gadget-HotCat.js&action=raw&ctype=text/javascript'); |
|||
}); |
|||
} |
|||
}); |
|||
//</nowiki> |
2023年4月4日 (二) 20:36的最新版本
//<nowiki> /** * @Function: 修改HotCat以识别繁简分类 * @Contributors: [[User:Bhsd]]; [[User:葫芦又]] */ $(function() { const ns = mw.config.get('wgNamespaceNumber'), mobileMode = mw.loader.getState( 'ext.gadget.MobileCategories' ) !== null; if ( mw.config.get('wgCanonicalSpecialPageName') !== 'Upload' && (ns < 0 || // Special pages ns === 10 || // Templates ns === 274 || // Widget ns === 828 || // Module (Lua) ns === 8 || // MediaWiki ns === 2 || // User ns % 2 === 1 || // Talk page !mw.config.get('wgArticleId') || // Non-existing file pages mw.config.get("wgPageContentModel") !== "wikitext" || mw.config.get('wgAction') != 'view' || mw.config.get( 'wgIsRedirect' )) ) { return; } if ( mobileMode ) { mw.hook( 'wikipage.categories' ).add( main ); } else { main(); } function main() { const api = new mw.Api(), categories = mw.config.get('wgCategories'), noVariants = categories.length === 0 || mobileMode, canonicalCats = categories.map(ele => 'Category:' + ele).join('|'); var request1 = api.get({ action: 'query', titles: mw.config.get('wgPageName'), prop: 'revisions|pageprops', rvprop: 'content', rvslots: 'main', ppprop: 'defaultsort', formatversion: 2, }), request2 = noVariants && $.Deferred().resolve() || api.get({ action:'query', prop: 'info', inprop: 'varianttitles', formatversion: 2, titles: canonicalCats, converttitles: 1 }); $.when(request1, request2).then(function(data1, data2) { const pageInfo = data1[0].query.pages[0], obj1 = pageInfo.revisions[0].slots.main.content; window.hotcat_preload = obj1; mw.config.set( 'wgDefaultSortKey', (pageInfo.pageprops || {}).defaultsort || '' ); mw.hook( 'gadget.hotcat.preloaded' ).fire(); if ( noVariants ) { mw.loader.load('/zh?title=mediawiki:gadget-HotCat.js&action=raw&ctype=text/javascript'); return; } const obj2 = data2[0].query.pages, cats = [...obj1.matchAll(/\[\[\s*(?:category|分类|分類)\s*:\s*(.*?)(?:\|.*)?\s*]]/ig)] .map( ele => ele[1].charAt(0).toUpperCase() + ele[1].slice(1).replace(/_/g, ' ') ); obj2.forEach(function(ele) { const catVt = ele.varianttitles; if (!catVt) { return; } const catHans = catVt['zh-cn'].substring(3), catHant = catVt['zh-tw'].substring(3), catlink = $('#catlinks a[title="Category:' + catHans + '"], #catlinks a[title="Category:' + catHant + '"]'); if (catlink.length === 0) { return; } if (cats.includes( catHans )) { catlink.attr('title','Category:' + catHans); catlink.attr('href','/zh/Category:' + catHans); } else { catlink.attr('title','Category:' + catHant); catlink.attr('href','/zh/Category:' + catHant); } }); mw.loader.load('/zh?title=mediawiki:gadget-HotCat.js&action=raw&ctype=text/javascript'); }); } }); //</nowiki>