LLWiki正在建设中,欢迎加入我们!
“MediaWiki:Gadget-HotCat-zh-core.js”的版本间差异
跳转到导航
跳转到搜索
小 (mobile) |
(test) |
||
第5行: | 第5行: | ||
*/ |
*/ |
||
$(function() { |
$(function() { |
||
function main() { |
|||
const ns = mw.config.get('wgNamespaceNumber'), |
|||
const ns = mw.config.get('wgNamespaceNumber'), |
|||
specialPageName = mw.config.get('wgCanonicalSpecialPageName'), |
|||
categories = mw.config.get('wgCategories') || []; |
|||
if ( specialPageName !== 'Upload' && |
|||
if ( specialPageName !== 'Upload' && |
|||
(ns < 0 || // Special pages |
|||
ns |
(ns < 0 || // Special pages |
||
ns === |
ns === 10 || // Templates |
||
ns === |
ns === 274 || // Widget |
||
ns === |
ns === 828 || // Module (Lua) |
||
ns === |
ns === 8 || // MediaWiki |
||
ns |
ns === 2 || // User |
||
ns % 2 === 1 || // Talk page |
|||
!mw.config.get('wgArticleId') || // Non-existing file pages |
|||
mw.config.get( |
!mw.config.get('wgArticleId') || // Non-existing file pages |
||
mw.config.get( |
mw.config.get("wgPageContentModel") !== "wikitext" || |
||
mw.config.get( |
mw.config.get('wgAction') != 'view' || |
||
mw.config.get( 'wgIsRedirect' )) ) { |
|||
return; |
|||
return; |
|||
} |
|||
if ( categories.length === 0) { |
|||
mw.loader.load('/zh?title=mediawiki:gadget-HotCat.js&action=raw&ctype=text/javascript'); |
|||
return; |
|||
} |
|||
const pageName = mw.config.get('wgPageName'), |
|||
api = new mw.Api(), |
|||
canonicalCats = categories.map(ele => 'Category:' + ele).join('|'); |
|||
var request1 = api.get({ |
|||
action: 'query', |
|||
titles: pageName, |
|||
prop: 'revisions|pageprops', |
|||
rvprop: 'content', |
|||
rvslots: 'main', |
|||
ppprop: 'defaultsort', |
|||
formatversion: 2, |
|||
}); |
|||
var request2 = 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, |
|||
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, ' ') ); |
|||
window.hotcat_preload = obj1; |
|||
mw.config.set( 'wgDefaultSortKey', pageInfo.pageprops.defaultsort ); |
|||
mw.hook( 'gadget.hotcat.preloaded' ).fire(); |
|||
obj2.forEach(function(ele) { |
|||
const catVt = ele.varianttitles; |
|||
if (!catVt) { return; } |
|||
const catHans = catVt["zh-hans"].substring(3), |
|||
catHant = catVt["zh-hant"].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'); |
|||
}); |
|||
} |
} |
||
if ( categories.length === 0) { |
|||
if ( mw.loader.getState( 'ext.gadget.MobileCategories' ) !== null ) { |
|||
mw.loader.load('/zh?title=mediawiki:gadget-HotCat.js&action=raw&ctype=text/javascript'); |
|||
mw.hook( 'wikipage.categories' ).add( main ); |
|||
return; |
|||
} else { |
|||
main(); |
|||
} |
} |
||
const pageName = mw.config.get('wgPageName'), |
|||
api = new mw.Api(), |
|||
canonicalCats = categories.map(ele => 'Category:' + ele).join('|'); |
|||
var request1 = api.get({ |
|||
action: 'query', |
|||
titles: pageName, |
|||
prop: 'revisions|pageprops', |
|||
rvprop: 'content', |
|||
rvslots: 'main', |
|||
ppprop: 'defaultsort', |
|||
formatversion: 2, |
|||
}); |
|||
var request2 = 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, |
|||
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, ' ') ); |
|||
window.hotcat_preload = obj1; |
|||
mw.config.set( 'wgDefaultSortKey', pageInfo.pageprops.defaultsort ); |
|||
mw.hook( 'gadget.hotcat.preloaded' ).fire(); |
|||
obj2.forEach(function(ele) { |
|||
const catVt = ele.varianttitles; |
|||
if (!catVt) { return; } |
|||
const catHans = catVt["zh-hans"].substring(3), |
|||
catHant = catVt["zh-hant"].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> |
//</nowiki> |
2023年3月30日 (四) 23:25的版本
//<nowiki> /** * @Function: 修改HotCat以识别繁简分类 * @Contributors: [[User:Bhsd]]; [[User:葫芦又]] */ $(function() { function main() { const ns = mw.config.get('wgNamespaceNumber'), specialPageName = mw.config.get('wgCanonicalSpecialPageName'), categories = mw.config.get('wgCategories') || []; if ( specialPageName !== '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 ( categories.length === 0) { mw.loader.load('/zh?title=mediawiki:gadget-HotCat.js&action=raw&ctype=text/javascript'); return; } const pageName = mw.config.get('wgPageName'), api = new mw.Api(), canonicalCats = categories.map(ele => 'Category:' + ele).join('|'); var request1 = api.get({ action: 'query', titles: pageName, prop: 'revisions|pageprops', rvprop: 'content', rvslots: 'main', ppprop: 'defaultsort', formatversion: 2, }); var request2 = 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, 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, ' ') ); window.hotcat_preload = obj1; mw.config.set( 'wgDefaultSortKey', pageInfo.pageprops.defaultsort ); mw.hook( 'gadget.hotcat.preloaded' ).fire(); obj2.forEach(function(ele) { const catVt = ele.varianttitles; if (!catVt) { return; } const catHans = catVt["zh-hans"].substring(3), catHant = catVt["zh-hant"].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'); }); } if ( mw.loader.getState( 'ext.gadget.MobileCategories' ) !== null ) { mw.hook( 'wikipage.categories' ).add( main ); } else { main(); } }); //</nowiki>