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

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

来自LLWiki
跳转到导航 跳转到搜索
(// 使用Wikiplus小工具快速编辑)
标签移动版网页编辑 移动版编辑
第8行: 第8行:
console.log('API request: 查询分类下无授权协议的文件');
console.log('API request: 查询分类下无授权协议的文件');
const timerStart = mw.now();
const timerStart = mw.now();
api.get({action: 'query', prop: 'categories', cllimit: 'max',
api.get({action: 'query', prop: 'categories', clcategories: 'Category:原作者保留权利的文件', formatversion: 2,
generator: 'categorymembers', gcmtitle: `Category:${cmtitle}`, gcmtype: 'file', gcmlimit: 'max'
clcategories: 'Category:原作者保留权利的文件|Category:可自由使用的文件|Category:LLWiki版权所有的文件|使用CC Zero协议的文件',
}).then(res => {
generator: 'categorymembers', gcmtitle: 'Category:' + cmtitle, gcmtype: 'file', gcmlimit: 'max',
formatversion: 2}).then(res => {
console.log(`End API request: 已获得分类下无授权协议的文件,用时 ${mw.now() - timerStart} ms`);
console.log(`End API request: 已获得分类下无授权协议的文件,用时 ${mw.now() - timerStart} ms`);
res.query.pages.filter(ele => !ele.categories).forEach(ele => {
res.query.pages.filter(ele => !ele.categories).forEach(ele => {
第24行: 第23行:
mw.myJsSize = function(table) {
mw.myJsSize = function(table) {
const items = table.find( 'td:first-child' ),
const items = table.find( 'td:first-child' ),
titles = items.children( 'a' ).map(function() { return this.title; }).toArray();
titles = items.children( 'a' ).toArray().map(ele => ele.title);
mw.loader.using( 'mediawiki.api' ).then(() => {
mw.loader.using( 'mediawiki.api' ).then(() => {
console.log('API request: 查询JS页面大小');
console.log('API request: 查询JS页面大小');
第43行: 第42行:
(function() {
(function() {
$('#myJsSize').one('click', function() { mw.myJsSize( $(this).parent().next() ); });
$('#myJsSize').one('click', function() { mw.myJsSize( $(this).parent().next() ); });
const timerStart = mw.now();
const start = mw.now();
mw.hook( 'wikipage.content' ).add(() => {
['wikipage.content', 'wikipage.collapsibleContent', 'wikipage.categories', 'wikipage.diff',
'wikipage.editform', 'structuredChangeFilters.ui.initialized', 'codeEditor.configure', // MW原生Hook
console.log(`Hook: wikipage.content after ${mw.now() - timerStart} ms`);
'code.prettify', 'wikiplus.dialog', 'transclusion.preview' // 小工具Hook
});
].forEach(ele => { mw.hook(ele).add(() => { console.log(`Hook: ${ele} after ${mw.now() - start} 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`);
});
}) ();
}) ();

2020年12月13日 (日) 04:42的版本

// 由[[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', clcategories: 'Category:原作者保留权利的文件', formatversion: 2,
            generator: 'categorymembers', gcmtitle: `Category:${cmtitle}`, gcmtype: 'file', gcmlimit: 'max'
        }).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' ).toArray().map(ele => ele.title);
    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() ); });
    const start = mw.now();
    ['wikipage.content', 'wikipage.collapsibleContent', 'wikipage.categories', 'wikipage.diff',
        'wikipage.editform', 'structuredChangeFilters.ui.initialized', 'codeEditor.configure', // MW原生Hook
        'code.prettify', 'wikiplus.dialog', 'transclusion.preview' // 小工具Hook
    ].forEach(ele => { mw.hook(ele).add(() => { console.log(`Hook: ${ele} after ${mw.now() - start} ms`); }); });
}) ();