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

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

来自LLWiki
跳转到导航 跳转到搜索
(// 使用Wikiplus小工具快速编辑)
标签移动版网页编辑 移动版编辑
第29行: 第29行:
const timerStart = mw.now();
const timerStart = mw.now();
new mw.Api().get({ action:'query', prop:'info', titles:titles.join( '|' ), formatversion:2 }).then(data => {
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` );
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>', {class: 'video-link',
items.parent().append(i => $('<td>', {class: 'video-link',
text: (lengths[ titles[i] ] / 1024).toFixed(1)}).css('text-align', 'right'); });
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'});
$('#myJsSize').off( 'click' ).one('click', () => { mw.myJsSize( $(this).parent('p').next( 'table' ) ); });
$('#myJsSize').off( 'click' ).one('click', function() { mw.myJsSize( $(this).parent().next() ); });
});
});
});
});
第42行: 第42行:


(function() {
(function() {
$('#myJsSize').one('click', function() { mw.myJsSize( $(this).parent().next() ); });
const timerStart = mw.now();
const timerStart = mw.now();
$('#myJsSize').one('click', function() { mw.myJsSize( $(this).parent('p').next( 'table' ) ); });
mw.hook( 'wikipage.content' ).add(() => {
mw.hook( 'wikipage.content' ).add(() => {
console.log(`Hook: wikipage.content after ${mw.now() - timerStart} ms`);
console.log(`Hook: wikipage.content after ${mw.now() - timerStart} ms`);

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

// 由[[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 => $('<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 timerStart = mw.now();
    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`);
    });
}) ();