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

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

来自LLWiki
跳转到导航 跳转到搜索
第6行: 第6行:
mw.loader.using( 'mediawiki.api' ).then(() => {
mw.loader.using( 'mediawiki.api' ).then(() => {
const api = new mw.Api();
const api = new mw.Api();
console.log('API request: 查询分类下的文件');
console.log('API request: 查询分类下无授权协议的文件');
let timerStart = mw.now();
const timerStart = mw.now();
api.get({action: 'query',
api.get({action: 'query', prop: 'categories', cllimit: 'max',
clcategories: 'Category:原作者保留权利的文件|Category:可自由使用的文件|Category:LLWiki版权所有的文件|使用CC Zero协议的文件',
list: 'categorymembers',
cmtitle: 'Category:' + cmtitle,
generator: 'categorymembers', gcmtitle: 'Category:' + cmtitle, gcmtype: 'file', gcmlimit: 'max',
cmprop: 'ids',
formatversion: 2}).then(res => {
console.log(`End API request: 已获得分类下无授权协议的文件,用时 ${mw.now() - timerStart} ms`);
cmtype: 'file',
res.query.pages.filter(ele => !ele.categories).forEach(ele => {
cmlimit: 'max',
api.postWithToken('csrf', {action: 'edit', pageid: ele.pageid, minor: 1, bot: 1,
formatversion: 2
prependtext: '==授权协议==\n{{copyright}}\n', summary: '使用API批量添加授权协议',
}).then(data => {
}).catch(reason => { console.error(`页面${ele.pageid}无法添加授权协议,错误原因:${reason}`); });
console.log(`End API request: 已获得分类下的文件,用时 ${mw.now() - timerStart} ms`);
});
console.log('API request: 查询无授权协议的文件');
}, reason => { mw.notify(`无法获得对应文件。错误原因:${reason}`, {type: 'error'}); });
timerStart = mw.now();
api.get({action: 'query',
prop: 'categories',
pageids: data.query.categorymembers.map(ele => ele.pageid).join('|'),
clcategories: 'Category:原作者保留权利的文件|Category:可自由使用的文件|Category:LLWiki版权所有的文件|使用CC Zero协议的文件',
cllimit: 'max',
formatversion: 2
}).then(res => {
console.log(`End API request: 已获得无授权协议的文件,用时 ${mw.now() - timerStart} ms`);
res.query.pages.filter(ele => !ele.categories).forEach(ele => {
api.postWithToken('csrf', {action: 'edit',
pageid: ele.pageid,
prependtext: '==授权协议==\n{{copyright}}\n',
summary: '使用API批量添加授权协议',
minor: 1,
bot: 1
}).catch(reason => { console.error(`页面${ele.pageid}无法添加授权协议,错误原因:${reason}`); });
});
}, reason => { mw.notify(`无法获得无授权协议的文件。错误原因:${reason}`, {type: 'error'}); });
}, reason => { mw.notify(`无法获得分类下的文件。错误原因:${reason}`, {type: 'error'}); });
});
});
};
};
第45行: 第26行:
titles = items.children( 'a' ).map(function() { return this.title; }).toArray();
titles = items.children( 'a' ).map(function() { return this.title; }).toArray();
mw.loader.using( 'mediawiki.api' ).then(() => {
mw.loader.using( 'mediawiki.api' ).then(() => {
console.log( 'API request: 查询JS页面大小' );
console.log('API request: 查询JS页面大小');
const timerStart = mw.now();
const timerStart = mw.now();
new mw.Api().get({ action:'query', prop:'info', titles:titles.join( '|' ), formatversion:2 })
new mw.Api().get({ action:'query', prop:'info', titles:titles.join( '|' ), formatversion:2 }).then(data => {
.then(data => {
console.log( `End API request: 已获得JS页面大小,用时 ${mw.now() - timerStart} ms` );
console.log( `End API request: 已获得JS页面大小,用时 ${mw.now() - timerStart} ms` );
table.find( 'th:last-child' ).show();
table.find( 'th:last-child' ).show();
const lengths = Object.fromEntries( data.query.pages.map(ele => [ele.title, ele.length]) );
const lengths = Object.fromEntries( data.query.pages.map(ele => [ele.title, ele.length]) );
items.parent().append(i => { return $('<td>').addClass( 'video-link' ).css('text-align', 'right')
items.parent().append(i => { return $('<td>', {class: 'video-link',
.text( (lengths[ titles[i] ] / 1024).toFixed(1) ); });
text: (lengths[ titles[i] ] / 1024).toFixed(1)}).css('text-align', 'right'); });
}, reason => {
}, reason => {
mw.notify(`无法获得JS页面大小。错误原因:${reason}`, {type: 'error'});
mw.notify(`无法获得JS页面大小。错误原因:${reason}`, {type: 'error'});

2020年12月11日 (五) 11:11的版本

// 由[[Special:Mypage/common.js]]调用,可以使用ES6语法
// 生成JS文件大小
"use strict";
/*global mw, $*/
mw.myCopyright = function(cmtitle = mw.config.get('wgTitle')) {
    mw.loader.using( 'mediawiki.api' ).then(() => {
        const api = new mw.Api();
        console.log('API request: 查询分类下无授权协议的文件');
        const timerStart = mw.now();
        api.get({action: 'query', prop: 'categories', cllimit: 'max',
            clcategories: 'Category:原作者保留权利的文件|Category:可自由使用的文件|Category:LLWiki版权所有的文件|使用CC Zero协议的文件',
            generator: 'categorymembers', gcmtitle: 'Category:' + cmtitle, gcmtype: 'file', gcmlimit: 'max',
            formatversion: 2}).then(res => {
            console.log(`End API request: 已获得分类下无授权协议的文件,用时 ${mw.now() - timerStart} ms`);
            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}`); });
            });
        }, reason => { mw.notify(`无法获得对应文件。错误原因:${reason}`, {type: 'error'}); });
    });
};

mw.myJsSize = function(table) {
    const items = table.find( 'td:first-child' ),
        titles = items.children( 'a' ).map(function() { return this.title; }).toArray();
    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 => { return $('<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', () => { mw.myJsSize( $(this).parent('p').next( 'table' ) ); });
        });
    });
};

(function() {
    const timerStart = mw.now();
    $('#myJsSize').one('click', function() { mw.myJsSize( $(this).parent('p').next( 'table' ) ); });
    mw.hook( 'wikipage.content' ).add(() => {
        console.log(`Hook: wikipage.content after ${mw.now() - timerStart} ms`);
    });
    mw.hook( 'wikipage.collapsibleContent' ).add(() => {
        console.log(`Hook: wikipage.collapsibleContent after ${mw.now() - timerStart} ms`);
    });
    mw.hook( 'wikipage.categories' ).add(() => {
        console.log(`Hook: wikipage.categories after ${mw.now() - timerStart} ms`);
    });
    mw.hook( 'wikipage.diff' ).add(() => {
        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`);
    });
    mw.hook( 'structuredChangeFilters.ui.initialized' ).add(() => {
        console.log(`Hook: structuredChangeFilters.ui.initialized after ${mw.now() - timerStart} ms`);
    });
    mw.hook( 'codeEditor.configure' ).add(() => {
        console.log(`Hook: codeEditor.configure after ${mw.now() - timerStart} ms`);
    });
    mw.hook( 'code.prettify' ).add($content => {
        console.log(`Hook: code.prettify after ${mw.now() - timerStart} ms`);
    });
    mw.hook( 'wikiplus.dialog' ).add($content => {
        console.log(`Hook: wikiplus.dialog after ${mw.now() - timerStart} ms`);
    });
    mw.hook( 'transclusion.preview' ).add(() => {
        console.log(`Hook: transclusion.preview after ${mw.now() - timerStart} ms`);
    });
}) ();