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

“User:Bhsd/tool.js”的版本间差异

来自LLWiki
跳转到导航 跳转到搜索
(// 使用Wikiplus小工具快速编辑)
标签移动版网页编辑 移动版编辑
 
(未显示同一用户的8个中间版本)
第1行: 第1行:
// 由[[Special:Mypage/common.js]]调用,可以使用ES6语法
// 由[[Special:Mypage/common.js]]调用,可以使用ES6语法
/**
// 生成JS文件大小
* @Description: 自用的工具函数
* @Author: [[User:Bhsd]]
*/
"use strict";
"use strict";
// jshint varstmt: true
/*global mw, $*/
/**
$('.fa-play').click(function() {
* @Function: 批量添加文件授权协议,只能处理不超过500个文件的分类
const audio = $(this).toggleClass('fa-play fa-pause').prev().children( 'audio' )[0];
* @Dependencies: mediawiki.api, ext.gadget.site-lib
audio[audio.paused ? 'play' : 'pause']();
* @Param {String} cmtitle 分类名,默认为当前所在分类页
});
*/
let audio;
mw.myCopyright = (cmtitle = mw.config.get( 'wgTitle' )) => {
const menu = mw.menu([ {text: '音量上调', icon: 'volume-up', data: 1, click: () => {
const cats = ['原作者保留权利的文件', '可自由使用的文件', 'LLWiki版权所有的文件', '使用CC Zero协议的文件'],
audio.volume += 0.1;
setDisabled();
api = new mw.Api();
mw.timedQuery(api, { prop: 'categories', generator: 'categorymembers', gcmtitle: `Category:${cmtitle}`,
}}, {text: '音量下调', icon: 'volume-down', data: 1, click: () => {
gcmtype: 'file', gcmlimit: 'max', clcategories: cats.map(ele => `Category:${ele}`).join( '|' ), cllimit: 'max'
audio.volume -= 0.1;
}, '分类下无授权协议的文件').then(res => {
setDisabled();
res.query.pages.filter(ele => !ele.categories).forEach(ele => {
}}, {text: '下载', icon: 'download', data: 2}
api.postWithToken('csrf', {action: 'edit', pageid: ele.pageid, minor: 1, bot: 1,
], {id: 'listen-menu'}),
prependtext: '==授权协议==\n{{copyright}}\n', summary: '使用API批量添加授权协议',
volumeup = menu.findItemFromData( 1 ),
}).catch(reason => { console.error( `页面${ele.pageid}无法添加授权协议,错误原因:${reason}` ); });
volumedown = menu.findItemFromData( 2 ),
});
setDisabled = () => {
}, () => {});
volumeup.setDisabled( audio.volume == 1 );
};
volumedown.setDisabled( audio.volume === 0 );
volumedown.$label.children( 'i' ).toggleClass('fa-volume-down', audio.volume > 0)
.toggleClass('fa-volume-mute', audio.volume === 0);
},
$download = $('<a>');
menu.$element.appendTo( document.body );
menu.findItemFromData( 3 ).$label.wrap( $download );
menu.on('toggle', visible => { if (visible) { menu.unselectItem(); } });
$('.fa-ellipsis-v').click(function() {
const $this = $(this),
$audio = $this.parent().find( 'audio' ),
src = $audio.children( 'source' ).attr( 'src' ),
parts = src.split( '/' );
audio = $audio[0];
menu.setFloatableContainer( $this );
$download.attr({href: src, download: parts[ parts.length - 1 ]});
setDisabled();
});


/**
mw.myCopyright = function(cmtitle = mw.config.get('wgTitle')) {
* @Function: 查询JS页面大小
mw.loader.using( 'mediawiki.api' ).then(() => {
* @Dependencies: mediawiki.api, ext.gadget.site-lib
const api = new mw.Api();
*/
console.log('API request: 查询分类下无授权协议的文件');
mw.myJsSize = () => {
const timerStart = mw.now();
const $table = $('.jsTable'), // 总是获取当前页面中的表格
api.get({action: 'query', prop: 'categories', clcategories: 'Category:原作者保留权利的文件', formatversion: 2,
$items = $table.find( 'tr:nth-child(n+1)' ),
generator: 'categorymembers', gcmtitle: `Category:${cmtitle}`, gcmtype: 'file', gcmlimit: 'max'
titles = $items.find( 'a' ).toArray().map(ele => ele.title);
}).then(res => {
mw.timedQuery(new mw.Api(), {prop: 'info', titles: titles.join( '|' )}, 'JS页面大小').then(data => {
console.log(`End API request: 已获得分类下无授权协议的文件,用时 ${mw.now() - timerStart} ms`);
$table.toggleClass( 'jsTable jsTable2' );
res.query.pages.filter(ele => !ele.categories).forEach(ele => {
// 注意返回的结果可能顺序不同
api.postWithToken('csrf', {action: 'edit', pageid: ele.pageid, minor: 1, bot: 1,
const lengths = Object.fromEntries( data.query.pages.map(ele => [ele.title, ele.length]) );
prependtext: '==授权协议==\n{{copyright}}\n', summary: '使用API批量添加授权协议',
$items.children( 'td:last-child' ).text(i => (lengths[ titles[i] ] / 1024).toFixed(1));
}).catch(reason => { console.error(`页面${ele.pageid}无法添加授权协议,错误原因:${reason}`); });
}, () => { $('#myJsSize').off( 'click' ).one('click', mw.myJsSize); });
});
}, reason => { mw.notify(`无法获得对应文件。错误原因:${reason}`, {type: 'error'}); });
});
};
};


mw.hook( 'wikipage.content' ).add($content => {
mw.myJsSize = function(table) {
const items = table.find( 'td:first-child' ),
const $btn = $content.find( '#myJsSize' ).one('click', mw.myJsSize);
if ($btn.length === 0) { return; }
titles = items.children( 'a' ).toArray().map(ele => ele.title);
console.log('Hook: wikipage.content, 开始添加JS大小查询按钮');
mw.loader.using( 'mediawiki.api' ).then(() => {
});
console.log('API request: 查询JS页面大小');
const timerStart = mw.now();
new mw.Api().get({ action:'query', prop:'info', titles:titles.join( '|' ), formatversion:2 }).then(data => {
console.log(`End API request: 已获得JS页面大小,用时 ${mw.now() - timerStart} ms`);
table.find( 'th:last-child' ).show();
const lengths = Object.fromEntries( data.query.pages.map(ele => [ele.title, ele.length]) );
items.parent().append(i => $('<td>', {class: 'video-link',
text: (lengths[ titles[i] ] / 1024).toFixed(1)}).css('text-align', 'right'));
}, reason => {
mw.notify(`无法获得JS页面大小。错误原因:${reason}`, {type: 'error'});
$('#myJsSize').off( 'click' ).one('click', function() { mw.myJsSize( $(this).parent().next() ); });
});
});
};


(function() {
(() => {
/**
$('#myJsSize').one('click', function() { mw.myJsSize( $(this).parent().next() ); });
* @Function: 记录所有触发的Hook
const start = mw.now();
*/
['postEdit', 'wikipage.content', 'wikipage.collapsibleContent', 'wikipage.categories', 'wikipage.diff',
const start = mw.now();
'wikipage.editform', 'structuredChangeFilters.ui.initialized', 'codeEditor.configure', // MW原生Hook
['postEdit', 'wikipage.content', 'wikipage.collapsibleContent', 'wikipage.categories', 'wikipage.diff',
'code.prettify', 'wikiplus.dialog', 'transclusion.preview' // 小工具Hook
'wikipage.editform', 'structuredChangeFilters.ui.initialized', 'codeEditor.configure', // MW原生Hook
].forEach(ele => { mw.hook(ele).add(x => {
'code.prettify', 'wikiplus.dialog', 'local.comments', 'hotcat.ready',
console.log(`Hook: ${ele} after ${mw.now() - start} ms`);
'settings.dialog', 'to.bottom' // 小工具Hook
if (x) { console.info(x); }
].forEach(ele => {
}); });
mw.hook( ele ).add(x => {
console.log(`Hook: ${ele} after ${mw.now() - start} ms`);
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); }
		});
	});
}) ();