“User:Bhsd/tool.js”的版本间差异
小 标签:移动版网页编辑 移动版编辑 |
小 (// 使用Wikiplus小工具快速编辑) 标签:移动版网页编辑 移动版编辑 |
||
(未显示同一用户的43个中间版本) | |||
第1行: | 第1行: | ||
// 由[[Special:Mypage/common.js]]调用,可以使用ES6语法 |
// 由[[Special:Mypage/common.js]]调用,可以使用ES6语法 |
||
/** |
|||
// 生成JS文件大小 |
|||
* @Description: 自用的工具函数 |
|||
* @Author: [[User:Bhsd]] |
|||
*/ |
|||
"use strict"; |
"use strict"; |
||
// jshint varstmt: true |
|||
mw.myTools = function(table) { |
|||
/** |
|||
table.find( 'th' ).first().after( '<th style="width:0">大小(KB)</th>' ); |
|||
* @Function: 批量添加文件授权协议,只能处理不超过500个文件的分类 |
|||
⚫ | |||
* @Dependencies: mediawiki.api, ext.gadget.site-lib |
|||
titles = items.children( 'a' ).map(function() { return this.title; }).toArray(); |
|||
* @Param {String} cmtitle 分类名,默认为当前所在分类页 |
|||
mw.loader.using( 'mediawiki.api' ).then(() => { |
|||
*/ |
|||
console.log( 'API request: 查询JS页面大小' ); |
|||
mw.myCopyright = (cmtitle = mw.config.get( 'wgTitle' )) => { |
|||
⚫ | |||
const cats = ['原作者保留权利的文件', '可自由使用的文件', 'LLWiki版权所有的文件', '使用CC Zero协议的文件'], |
|||
⚫ | |||
api = new mw.Api(); |
|||
.then(data => { |
|||
mw.timedQuery(api, { prop: 'categories', generator: 'categorymembers', gcmtitle: `Category:${cmtitle}`, |
|||
console.log( `End API request: 已获得JS页面大小,用时 ${mw.now() - timerStart} ms` ); |
|||
gcmtype: 'file', gcmlimit: 'max', clcategories: cats.map(ele => `Category:${ele}`).join( '|' ), cllimit: 'max' |
|||
⚫ | |||
}, '分类下无授权协议的文件').then(res => { |
|||
items.after(i => { return $('<td>').addClass( 'video-link' ).css('text-align', 'right') |
|||
res.query.pages.filter(ele => !ele.categories).forEach(ele => { |
|||
⚫ | |||
api.postWithToken('csrf', {action: 'edit', pageid: ele.pageid, minor: 1, bot: 1, |
|||
}); |
|||
prependtext: '==授权协议==\n{{copyright}}\n', summary: '使用API批量添加授权协议', |
|||
⚫ | |||
}).catch(reason => { console.error( `页面${ele.pageid}无法添加授权协议,错误原因:${reason}` ); }); |
|||
⚫ | |||
⚫ | |||
}; |
}; |
||
/** |
|||
(function() { |
|||
* @Function: 查询JS页面大小 |
|||
const timerStart = mw.now(); |
|||
* @Dependencies: mediawiki.api, ext.gadget.site-lib |
|||
⚫ | |||
*/ |
|||
⚫ | |||
mw.myJsSize = () => { |
|||
⚫ | |||
const $table = $('.jsTable'), // 总是获取当前页面中的表格 |
|||
⚫ | |||
⚫ | |||
console.log( 'Hook: wikipage.categories after ${mw.now() - timerStart} ms'); |
|||
titles = $items.find( 'a' ).toArray().map(ele => ele.title); |
|||
⚫ | |||
⚫ | |||
mw.hook( 'wikipage.diff' ).add(() => { |
|||
$table.toggleClass( 'jsTable jsTable2' ); |
|||
console.log( 'Hook: wikipage.diff after ${mw.now() - timerStart} ms'); |
|||
// 注意返回的结果可能顺序不同 |
|||
⚫ | |||
⚫ | |||
mw.hook( 'wikipage.editform' ).add(() => { |
|||
⚫ | |||
console.log( 'Hook: wikipage.editform after ${mw.now() - timerStart} ms'); |
|||
}, () => { $('#myJsSize').off( 'click' ).one('click', mw.myJsSize); }); |
|||
⚫ | |||
}; |
|||
⚫ | |||
console.log( 'Hook: structuredChangeFilters.ui.initialized after ${mw.now() - timerStart} ms'); |
|||
⚫ | |||
}); |
|||
const $btn = $content.find( '#myJsSize' ).one('click', mw.myJsSize); |
|||
if ($btn.length === 0) { return; } |
|||
console.log('Hook: wikipage.content, 开始添加JS大小查询按钮'); |
|||
⚫ | |||
(() => { |
|||
/** |
|||
* @Function: 记录所有触发的Hook |
|||
*/ |
|||
⚫ | |||
['postEdit', 'wikipage.content', 'wikipage.collapsibleContent', 'wikipage.categories', 'wikipage.diff', |
|||
⚫ | |||
'code.prettify', 'wikiplus.dialog', 'local.comments', 'hotcat.ready', |
|||
'settings.dialog', 'to.bottom' // 小工具Hook |
|||
].forEach(ele => { |
|||
⚫ | |||
⚫ | |||
if (x) { console.info(x); } |
|||
⚫ | |||
⚫ | |||
}) (); |
}) (); |
2023年3月17日 (五) 13:30的最新版本
// 由[[Special:Mypage/common.js]]调用,可以使用ES6语法 /** * @Description: 自用的工具函数 * @Author: [[User:Bhsd]] */ "use strict"; // jshint varstmt: true /** * @Function: 批量添加文件授权协议,只能处理不超过500个文件的分类 * @Dependencies: mediawiki.api, ext.gadget.site-lib * @Param {String} cmtitle 分类名,默认为当前所在分类页 */ mw.myCopyright = (cmtitle = mw.config.get( 'wgTitle' )) => { const cats = ['原作者保留权利的文件', '可自由使用的文件', 'LLWiki版权所有的文件', '使用CC Zero协议的文件'], api = new mw.Api(); mw.timedQuery(api, { prop: 'categories', generator: 'categorymembers', gcmtitle: `Category:${cmtitle}`, gcmtype: 'file', gcmlimit: 'max', clcategories: cats.map(ele => `Category:${ele}`).join( '|' ), cllimit: 'max' }, '分类下无授权协议的文件').then(res => { res.query.pages.filter(ele => !ele.categories).forEach(ele => { api.postWithToken('csrf', {action: 'edit', pageid: ele.pageid, minor: 1, bot: 1, prependtext: '==授权协议==\n{{copyright}}\n', summary: '使用API批量添加授权协议', }).catch(reason => { console.error( `页面${ele.pageid}无法添加授权协议,错误原因:${reason}` ); }); }); }, () => {}); }; /** * @Function: 查询JS页面大小 * @Dependencies: mediawiki.api, ext.gadget.site-lib */ mw.myJsSize = () => { const $table = $('.jsTable'), // 总是获取当前页面中的表格 $items = $table.find( 'tr:nth-child(n+1)' ), titles = $items.find( 'a' ).toArray().map(ele => ele.title); mw.timedQuery(new mw.Api(), {prop: 'info', titles: titles.join( '|' )}, 'JS页面大小').then(data => { $table.toggleClass( 'jsTable jsTable2' ); // 注意返回的结果可能顺序不同 const lengths = Object.fromEntries( data.query.pages.map(ele => [ele.title, ele.length]) ); $items.children( 'td:last-child' ).text(i => (lengths[ titles[i] ] / 1024).toFixed(1)); }, () => { $('#myJsSize').off( 'click' ).one('click', mw.myJsSize); }); }; mw.hook( 'wikipage.content' ).add($content => { const $btn = $content.find( '#myJsSize' ).one('click', mw.myJsSize); if ($btn.length === 0) { return; } console.log('Hook: wikipage.content, 开始添加JS大小查询按钮'); }); (() => { /** * @Function: 记录所有触发的Hook */ const start = mw.now(); ['postEdit', 'wikipage.content', 'wikipage.collapsibleContent', 'wikipage.categories', 'wikipage.diff', 'wikipage.editform', 'structuredChangeFilters.ui.initialized', 'codeEditor.configure', // MW原生Hook 'code.prettify', 'wikiplus.dialog', 'local.comments', 'hotcat.ready', 'settings.dialog', 'to.bottom' // 小工具Hook ].forEach(ele => { mw.hook( ele ).add(x => { console.log(`Hook: ${ele} after ${mw.now() - start} ms`); if (x) { console.info(x); } }); }); }) ();